2019-10-15 09:06:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-28 10:45:46 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-29 18:21:25 +00:00
|
|
|
class TestHelpersTest
|
|
|
|
include Gitlab::TaskHelpers
|
|
|
|
end
|
2016-09-28 10:45:46 +00:00
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe Gitlab::TaskHelpers do
|
2016-11-29 18:21:25 +00:00
|
|
|
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 }
|
2017-04-10 23:15:48 +00:00
|
|
|
let(:version) { '1.1.0' }
|
2016-11-29 18:21:25 +00:00
|
|
|
let(:tag) { 'v1.1.0' }
|
2016-09-28 10:45:46 +00:00
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
describe '#checkout_or_clone_version' do
|
2016-09-28 10:45:46 +00:00
|
|
|
before do
|
2016-11-29 18:21:25 +00:00
|
|
|
allow(subject).to receive(:run_command!)
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
it 'checkout the version and reset to it' do
|
2019-10-28 21:06:24 +00:00
|
|
|
expect(subject).to receive(:get_version).with(version).and_call_original
|
2017-04-10 23:15:48 +00:00
|
|
|
expect(subject).to receive(:checkout_version).with(tag, clone_path)
|
|
|
|
|
|
|
|
subject.checkout_or_clone_version(version: version, repo: repo, target_dir: clone_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "target_dir doesn't exist" do
|
|
|
|
it 'clones the repo' do
|
2020-05-14 15:08:14 +00:00
|
|
|
expect(subject).to receive(:clone_repo).with(repo, clone_path, clone_opts: [])
|
2016-09-28 10:45:46 +00:00
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
subject.checkout_or_clone_version(version: version, repo: repo, target_dir: clone_path)
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'target_dir exists' do
|
|
|
|
before do
|
2016-11-29 18:21:25 +00:00
|
|
|
expect(Dir).to receive(:exist?).and_return(true)
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
it "doesn't clone the repository" do
|
|
|
|
expect(subject).not_to receive(:clone_repo)
|
2016-09-28 10:45:46 +00:00
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
subject.checkout_or_clone_version(version: version, repo: repo, target_dir: clone_path)
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
2020-05-14 15:08:14 +00:00
|
|
|
|
|
|
|
it 'accepts clone_opts' do
|
|
|
|
expect(subject).to receive(:clone_repo).with(repo, clone_path, clone_opts: %w[--depth 1])
|
|
|
|
|
|
|
|
subject.checkout_or_clone_version(version: version, repo: repo, target_dir: clone_path, clone_opts: %w[--depth 1])
|
|
|
|
end
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|
|
|
|
|
2016-11-29 18:21:25 +00:00
|
|
|
describe '#clone_repo' do
|
|
|
|
it 'clones the repo in the target dir' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(subject)
|
|
|
|
.to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} clone -- #{repo} #{clone_path}])
|
2016-11-29 18:21:25 +00:00
|
|
|
|
|
|
|
subject.clone_repo(repo, clone_path)
|
|
|
|
end
|
2020-05-14 15:08:14 +00:00
|
|
|
|
|
|
|
it 'accepts clone_opts' do
|
|
|
|
expect(subject)
|
|
|
|
.to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} clone --depth 1 -- #{repo} #{clone_path}])
|
|
|
|
|
|
|
|
subject.clone_repo(repo, clone_path, clone_opts: %w[--depth 1])
|
|
|
|
end
|
2016-11-29 18:21:25 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
describe '#checkout_version' do
|
2016-11-29 18:21:25 +00:00
|
|
|
it 'clones the repo in the target dir' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(subject)
|
2017-07-20 21:30:29 +00:00
|
|
|
.to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} fetch --quiet origin #{tag}])
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(subject)
|
2017-07-20 21:30:29 +00:00
|
|
|
.to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} checkout -f --quiet FETCH_HEAD --])
|
2016-11-29 18:21:25 +00:00
|
|
|
|
2017-04-10 23:15:48 +00:00
|
|
|
subject.checkout_version(tag, clone_path)
|
2016-11-29 18:21:25 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-24 22:30:53 +00:00
|
|
|
|
|
|
|
describe '#run_command' do
|
|
|
|
it 'runs command and return the output' do
|
|
|
|
expect(subject.run_command(%w(echo it works!))).to eq("it works!\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty string when command doesnt exist' do
|
|
|
|
expect(subject.run_command(%w(nonexistentcommand with arguments))).to eq('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#run_command!' do
|
|
|
|
it 'runs command and return the output' do
|
|
|
|
expect(subject.run_command!(%w(echo it works!))).to eq("it works!\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns and exception when command exit with non zero code' do
|
|
|
|
expect { subject.run_command!(['bash', '-c', 'exit 1']) }.to raise_error Gitlab::TaskFailedError
|
|
|
|
end
|
|
|
|
end
|
2019-10-28 21:06:24 +00:00
|
|
|
|
|
|
|
describe '#get_version' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:version, :result) do
|
|
|
|
'1.1.1' | 'v1.1.1'
|
|
|
|
'master' | 'master'
|
|
|
|
'12.4.0-rc7' | 'v12.4.0-rc7'
|
|
|
|
'594c3ea3e0e5540e5915bd1c49713a0381459dd6' | '594c3ea3e0e5540e5915bd1c49713a0381459dd6'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it { expect(subject.get_version(version)).to eq(result) }
|
|
|
|
end
|
|
|
|
end
|
2016-09-28 10:45:46 +00:00
|
|
|
end
|