summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/push_spec.rb
blob: 8ba43b2967c456015e018a455fc48cfe35cf76c3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Git::Push do
  let_it_be(:project) { create(:project, :repository) }
  let(:oldrev) { project.commit('HEAD~2').id }
  let(:newrev) { project.commit.id }
  let(:ref) { 'refs/heads/some-branch' }

  subject { described_class.new(project, oldrev, newrev, ref) }

  describe '#branch_name' do
    context 'when it is a branch push' do
      let(:ref) { 'refs/heads/my-branch' }

      it 'returns branch name' do
        expect(subject.branch_name).to eq 'my-branch'
      end
    end

    context 'when it is a tag push' do
      let(:ref) { 'refs/tags/my-branch' }

      it 'returns nil' do
        expect(subject.branch_name).to be_nil
      end
    end
  end

  describe '#branch_push?' do
    context 'when pushing a branch ref' do
      let(:ref) { 'refs/heads/my-branch' }

      it { is_expected.to be_branch_push }
    end

    context 'when it is a tag push' do
      let(:ref) { 'refs/tags/my-tag' }

      it { is_expected.not_to be_branch_push }
    end
  end

  describe '#branch_updated?' do
    context 'when it is a branch push with correct old and new revisions' do
      it { is_expected.to be_branch_updated }
    end

    context 'when it is not a branch push' do
      let(:ref) { 'refs/tags/my-tag' }

      it { is_expected.not_to be_branch_updated }
    end

    context 'when old revision is blank' do
      let(:oldrev) { Gitlab::Git::BLANK_SHA }

      it { is_expected.not_to be_branch_updated }
    end

    context 'when it is not a branch push' do
      let(:newrev) { Gitlab::Git::BLANK_SHA }

      it { is_expected.not_to be_branch_updated }
    end

    context 'when oldrev is nil' do
      let(:oldrev) { nil }

      it { is_expected.not_to be_branch_updated }
    end
  end

  describe '#force_push?' do
    context 'when old revision is an ancestor of the new revision' do
      let(:oldrev) { 'HEAD~3' }
      let(:newrev) { 'HEAD~1' }

      it { is_expected.not_to be_force_push }
    end

    context 'when old revision is not an ancestor of the new revision' do
      let(:oldrev) { 'HEAD~3' }
      let(:newrev) { '123456' }

      it { is_expected.to be_force_push }
    end

    context 'when called muiltiple times' do
      it 'does not make make multiple calls to the force push check' do
        expect(Gitlab::Checks::ForcePush).to receive(:force_push?).once

        2.times do
          subject.force_push?
        end
      end
    end
  end

  describe '#branch_added?' do
    context 'when old revision is defined' do
      it { is_expected.not_to be_branch_added }
    end

    context 'when old revision is not defined' do
      let(:oldrev) { Gitlab::Git::BLANK_SHA }

      it { is_expected.to be_branch_added }
    end
  end

  describe '#branch_removed?' do
    context 'when new revision is defined' do
      it { is_expected.not_to be_branch_removed }
    end

    context 'when new revision is not defined' do
      let(:newrev) { Gitlab::Git::BLANK_SHA }

      it { is_expected.to be_branch_removed }
    end
  end

  describe '#modified_paths' do
    context 'when a push is a branch update' do
      let(:newrev) { '498214d' }
      let(:oldrev) { '281d3a7' }

      it 'returns modified paths' do
        expect(subject.modified_paths).to eq ['bar/branch-test.txt',
                                              'files/js/commit.coffee',
                                              'with space/README.md']
      end
    end

    context 'when a push is not a branch update' do
      let(:oldrev) { Gitlab::Git::BLANK_SHA }

      it 'raises an error' do
        expect { subject.modified_paths }.to raise_error(ArgumentError)
      end
    end
  end

  describe '#oldrev' do
    context 'when a valid oldrev is provided' do
      it 'returns oldrev' do
        expect(subject.oldrev).to eq oldrev
      end
    end

    context 'when a nil valud is provided' do
      let(:oldrev) { nil }

      it 'returns blank SHA' do
        expect(subject.oldrev).to eq Gitlab::Git::BLANK_SHA
      end
    end
  end

  describe '#newrev' do
    context 'when valid newrev is provided' do
      it 'returns newrev' do
        expect(subject.newrev).to eq newrev
      end
    end

    context 'when a nil valud is provided' do
      let(:newrev) { nil }

      it 'returns blank SHA' do
        expect(subject.newrev).to eq Gitlab::Git::BLANK_SHA
      end
    end
  end
end