2016-09-28 06:45:46 -04:00
|
|
|
require 'spec_helper'
|
2016-11-29 13:21:25 -05:00
|
|
|
require 'tasks/gitlab/task_helpers'
|
2016-09-28 06:45:46 -04:00
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
class TestHelpersTest
|
|
|
|
include Gitlab::TaskHelpers
|
|
|
|
end
|
2016-09-28 06:45:46 -04:00
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
describe Gitlab::TaskHelpers do
|
|
|
|
subject { TestHelpersTest.new }
|
|
|
|
|
|
|
|
let(:repo) { 'https://gitlab.com/gitlab-org/gitlab-test.git' }
|
|
|
|
let(:clone_path) { Rails.root.join('tmp/tests/task_helpers_tests').to_s }
|
|
|
|
let(:tag) { 'v1.1.0' }
|
2016-09-28 06:45:46 -04:00
|
|
|
|
|
|
|
describe '#checkout_or_clone_tag' do
|
|
|
|
before do
|
2016-11-29 13:21:25 -05:00
|
|
|
allow(subject).to receive(:run_command!)
|
|
|
|
expect(subject).to receive(:reset_to_tag).with(tag, clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'target_dir does not exist' do
|
|
|
|
it 'clones the repo, retrieve the tag from origin, and checkout the tag' do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).to receive(:clone_repo).with(repo, clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
subject.checkout_or_clone_tag(tag: tag, repo: repo, target_dir: clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'target_dir exists' do
|
|
|
|
before do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(Dir).to receive(:exist?).and_return(true)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fetch and checkout the tag' do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).to receive(:checkout_tag).with(tag, clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
subject.checkout_or_clone_tag(tag: tag, repo: repo, target_dir: clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
describe '#clone_repo' do
|
|
|
|
it 'clones the repo in the target dir' do
|
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} clone -- #{repo} #{clone_path}])
|
|
|
|
|
|
|
|
subject.clone_repo(repo, clone_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#checkout_tag' do
|
|
|
|
it 'clones the repo in the target dir' do
|
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} fetch --tags --quiet])
|
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} checkout --quiet #{tag}])
|
|
|
|
|
|
|
|
subject.checkout_tag(tag, clone_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-28 06:45:46 -04:00
|
|
|
describe '#reset_to_tag' do
|
|
|
|
let(:tag) { 'v1.1.0' }
|
|
|
|
before do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} reset --hard #{tag}])
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the tag is not checked out locally' do
|
|
|
|
before do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} describe -- #{tag}]).and_raise(Gitlab::TaskFailedError)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fetch origin, ensure the tag exists, and resets --hard to the given tag' do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} fetch origin])
|
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} describe -- origin/#{tag}]).and_return(tag)
|
2016-09-28 06:45:46 -04:00
|
|
|
|
2016-11-29 13:21:25 -05:00
|
|
|
subject.reset_to_tag(tag, clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the tag is checked out locally' do
|
|
|
|
before do
|
2016-11-29 13:21:25 -05:00
|
|
|
expect(subject).
|
|
|
|
to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} describe -- #{tag}]).and_return(tag)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets --hard to the given tag' do
|
2016-11-29 13:21:25 -05:00
|
|
|
subject.reset_to_tag(tag, clone_path)
|
2016-09-28 06:45:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|