summaryrefslogtreecommitdiff
path: root/spec/tasks/gitlab/task_helpers_spec.rb
blob: 86e42d845cea48de85c8c290d8be9e0b6335c420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'spec_helper'
require 'tasks/gitlab/task_helpers'

class TestHelpersTest
  include Gitlab::TaskHelpers
end

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' }

  describe '#checkout_or_clone_tag' do
    before do
      allow(subject).to receive(:run_command!)
      expect(subject).to receive(:reset_to_tag).with(tag, clone_path)
    end

    context 'target_dir does not exist' do
      it 'clones the repo, retrieve the tag from origin, and checkout the tag' do
        expect(subject).to receive(:clone_repo).with(repo, clone_path)

        subject.checkout_or_clone_tag(tag: tag, repo: repo, target_dir: clone_path)
      end
    end

    context 'target_dir exists' do
      before do
        expect(Dir).to receive(:exist?).and_return(true)
      end

      it 'fetch and checkout the tag' do
        expect(subject).to receive(:checkout_tag).with(tag, clone_path)

        subject.checkout_or_clone_tag(tag: tag, repo: repo, target_dir: clone_path)
      end
    end
  end

  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

  describe '#reset_to_tag' do
    let(:tag) { 'v1.1.0' }
    before do
      expect(subject).
        to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} reset --hard #{tag}])
    end

    context 'when the tag is not checked out locally' do
      before do
        expect(subject).
          to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} describe -- #{tag}]).and_raise(Gitlab::TaskFailedError)
      end

      it 'fetch origin, ensure the tag exists, and resets --hard to the given tag' do
        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)

        subject.reset_to_tag(tag, clone_path)
      end
    end

    context 'when the tag is checked out locally' do
      before do
        expect(subject).
          to receive(:run_command!).with(%W[#{Gitlab.config.git.bin_path} -C #{clone_path} describe -- #{tag}]).and_return(tag)
      end

      it 'resets --hard to the given tag' do
        subject.reset_to_tag(tag, clone_path)
      end
    end
  end
end