Skip to content

Commit 238fd9b

Browse files
committed
Cover with tests GitlabProjects
1 parent 55818a4 commit 238fd9b

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

lib/gitlab_projects.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
require 'fileutils'
33

44
class GitlabProjects
5-
attr_reader :project_name, :full_path
5+
# Project name is a directory name for repository with .git at the end
6+
# It may be namespaced or not. Like repo.git or gitlab/repo.git
7+
attr_reader :project_name
8+
9+
# Absolute path to directory where repositories stored
10+
# By default it is /home/git/repositories
11+
attr_reader :repos_path
12+
13+
# Full path is an absolute path to the repository
14+
# Ex /home/git/repositories/test.git
15+
attr_reader :full_path
616

717
def initialize
818
@command = ARGV.shift
@@ -42,7 +52,7 @@ def rm_project
4252

4353
def import_project
4454
@source = ARGV.shift
45-
cmd = "cd #{@repos_path} && git clone --bare #{@source} #{@project_name} && #{create_hooks_cmd}"
55+
cmd = "cd #{repos_path} && git clone --bare #{@source} #{project_name} && #{create_hooks_cmd}"
4656
system(cmd)
4757
end
4858
end

spec/gitlab_projects_spec.rb

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,89 @@
22
require_relative '../lib/gitlab_projects'
33

44
describe GitlabProjects do
5+
before do
6+
FileUtils.mkdir_p(tmp_repos_path)
7+
end
8+
9+
after do
10+
FileUtils.rm_rf(tmp_repos_path)
11+
end
12+
513
describe :initialize do
614
before do
7-
argv('add-project', 'gitlab-ci.git')
15+
argv('add-project', repo_name)
816
@gl_projects = GitlabProjects.new
917
end
1018

11-
it { @gl_projects.project_name.should == 'gitlab-ci.git' }
19+
it { @gl_projects.project_name.should == repo_name }
1220
it { @gl_projects.instance_variable_get(:@command).should == 'add-project' }
1321
it { @gl_projects.instance_variable_get(:@full_path).should == '/home/git/repositories/gitlab-ci.git' }
1422
end
1523

1624
describe :add_project do
17-
before do
18-
argv('add-project', 'gitlab-ci.git')
19-
@gl_projects = GitlabProjects.new
20-
@gl_projects.stub(full_path: tmp_repo_path)
21-
end
22-
23-
after do
24-
FileUtils.rm_rf(tmp_repo_path)
25-
end
25+
let(:gl_projects) { build_gitlab_projects('add-project', repo_name) }
2626

2727
it "should create a directory" do
28-
@gl_projects.stub(system: true)
29-
@gl_projects.send :add_project
28+
gl_projects.stub(system: true)
29+
gl_projects.exec
3030
File.exists?(tmp_repo_path).should be_true
3131
end
3232

3333
it "should receive valid cmd" do
3434
valid_cmd = "cd #{tmp_repo_path} && git init --bare"
3535
valid_cmd << " && ln -s #{ROOT_PATH}/hooks/post-receive #{tmp_repo_path}/hooks/post-receive"
3636
valid_cmd << " && ln -s #{ROOT_PATH}/hooks/update #{tmp_repo_path}/hooks/update"
37-
@gl_projects.should_receive(:system).with(valid_cmd)
38-
@gl_projects.send :add_project
37+
gl_projects.should_receive(:system).with(valid_cmd)
38+
gl_projects.exec
3939
end
4040
end
4141

42+
describe :rm_project do
43+
let(:gl_projects) { build_gitlab_projects('rm-project', repo_name) }
44+
45+
before do
46+
FileUtils.mkdir_p(tmp_repo_path)
47+
end
48+
49+
it "should remove a repo directory" do
50+
File.exists?(tmp_repo_path).should be_true
51+
gl_projects.exec
52+
File.exists?(tmp_repo_path).should be_false
53+
end
54+
end
55+
56+
describe :import_project do
57+
let(:gl_projects) { build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git') }
58+
59+
it "should import a repo" do
60+
gl_projects.exec
61+
File.exists?(File.join(tmp_repo_path, 'HEAD')).should be_true
62+
end
63+
end
64+
65+
def build_gitlab_projects(*args)
66+
argv(*args)
67+
gl_projects = GitlabProjects.new
68+
gl_projects.stub(repos_path: tmp_repos_path)
69+
gl_projects.stub(full_path: tmp_repo_path)
70+
gl_projects
71+
end
72+
4273
def argv(*args)
4374
args.each_with_index do |arg, i|
4475
ARGV[i] = arg
4576
end
4677
end
4778

79+
def tmp_repos_path
80+
File.join(ROOT_PATH, 'tmp', 'repositories')
81+
end
82+
4883
def tmp_repo_path
49-
File.join(ROOT_PATH, 'tmp', 'gitlab-ci.git')
84+
File.join(tmp_repos_path, repo_name)
85+
end
86+
87+
def repo_name
88+
'gitlab-ci.git'
5089
end
5190
end

0 commit comments

Comments
 (0)