diff options
-rw-r--r-- | lib/gitlab_projects.rb | 14 | ||||
-rw-r--r-- | spec/gitlab_projects_spec.rb | 71 |
2 files changed, 67 insertions, 18 deletions
diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index b9eb36a..220ecb1 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -2,7 +2,17 @@ require 'open3' require 'fileutils' class GitlabProjects - attr_reader :project_name, :full_path + # Project name is a directory name for repository with .git at the end + # It may be namespaced or not. Like repo.git or gitlab/repo.git + attr_reader :project_name + + # Absolute path to directory where repositories stored + # By default it is /home/git/repositories + attr_reader :repos_path + + # Full path is an absolute path to the repository + # Ex /home/git/repositories/test.git + attr_reader :full_path def initialize @command = ARGV.shift @@ -42,7 +52,7 @@ class GitlabProjects def import_project @source = ARGV.shift - cmd = "cd #{@repos_path} && git clone --bare #{@source} #{@project_name} && #{create_hooks_cmd}" + cmd = "cd #{repos_path} && git clone --bare #{@source} #{project_name} && #{create_hooks_cmd}" system(cmd) end end diff --git a/spec/gitlab_projects_spec.rb b/spec/gitlab_projects_spec.rb index 38af894..87c64b7 100644 --- a/spec/gitlab_projects_spec.rb +++ b/spec/gitlab_projects_spec.rb @@ -2,31 +2,31 @@ require_relative 'spec_helper' require_relative '../lib/gitlab_projects' describe GitlabProjects do + before do + FileUtils.mkdir_p(tmp_repos_path) + end + + after do + FileUtils.rm_rf(tmp_repos_path) + end + describe :initialize do before do - argv('add-project', 'gitlab-ci.git') + argv('add-project', repo_name) @gl_projects = GitlabProjects.new end - it { @gl_projects.project_name.should == 'gitlab-ci.git' } + it { @gl_projects.project_name.should == repo_name } it { @gl_projects.instance_variable_get(:@command).should == 'add-project' } it { @gl_projects.instance_variable_get(:@full_path).should == '/home/git/repositories/gitlab-ci.git' } end describe :add_project do - before do - argv('add-project', 'gitlab-ci.git') - @gl_projects = GitlabProjects.new - @gl_projects.stub(full_path: tmp_repo_path) - end - - after do - FileUtils.rm_rf(tmp_repo_path) - end + let(:gl_projects) { build_gitlab_projects('add-project', repo_name) } it "should create a directory" do - @gl_projects.stub(system: true) - @gl_projects.send :add_project + gl_projects.stub(system: true) + gl_projects.exec File.exists?(tmp_repo_path).should be_true end @@ -34,18 +34,57 @@ describe GitlabProjects do valid_cmd = "cd #{tmp_repo_path} && git init --bare" valid_cmd << " && ln -s #{ROOT_PATH}/hooks/post-receive #{tmp_repo_path}/hooks/post-receive" valid_cmd << " && ln -s #{ROOT_PATH}/hooks/update #{tmp_repo_path}/hooks/update" - @gl_projects.should_receive(:system).with(valid_cmd) - @gl_projects.send :add_project + gl_projects.should_receive(:system).with(valid_cmd) + gl_projects.exec end end + describe :rm_project do + let(:gl_projects) { build_gitlab_projects('rm-project', repo_name) } + + before do + FileUtils.mkdir_p(tmp_repo_path) + end + + it "should remove a repo directory" do + File.exists?(tmp_repo_path).should be_true + gl_projects.exec + File.exists?(tmp_repo_path).should be_false + end + end + + describe :import_project do + let(:gl_projects) { build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git') } + + it "should import a repo" do + gl_projects.exec + File.exists?(File.join(tmp_repo_path, 'HEAD')).should be_true + end + end + + def build_gitlab_projects(*args) + argv(*args) + gl_projects = GitlabProjects.new + gl_projects.stub(repos_path: tmp_repos_path) + gl_projects.stub(full_path: tmp_repo_path) + gl_projects + end + def argv(*args) args.each_with_index do |arg, i| ARGV[i] = arg end end + def tmp_repos_path + File.join(ROOT_PATH, 'tmp', 'repositories') + end + def tmp_repo_path - File.join(ROOT_PATH, 'tmp', 'gitlab-ci.git') + File.join(tmp_repos_path, repo_name) + end + + def repo_name + 'gitlab-ci.git' end end |