summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/danger/helper_spec.rb
blob: 793cac593a2be33b21545e8785a5bb1fbe494548 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require 'webmock/rspec'

require 'gitlab/danger/helper'

describe Gitlab::Danger::Helper do
  using RSpec::Parameterized::TableSyntax

  class FakeDanger
    include Gitlab::Danger::Helper

    attr_reader :git

    def initialize(git:)
      @git = git
    end
  end

  let(:teammate_json) do
    <<~JSON
    [
      {
        "username": "in-gitlab-ce",
        "name": "CE maintainer",
        "projects":{ "gitlab-ce": "maintainer backend" }
      },
      {
        "username": "in-gitlab-ee",
        "name": "EE reviewer",
        "projects":{ "gitlab-ee": "reviewer frontend" }
      }
    ]
    JSON
  end

  let(:ce_teammate_matcher) do
    satisfy do |teammate|
      teammate.username == 'in-gitlab-ce' &&
        teammate.name == 'CE maintainer' &&
        teammate.projects == { 'gitlab-ce' => 'maintainer backend' }
    end
  end

  let(:ee_teammate_matcher) do
    satisfy do |teammate|
      teammate.username == 'in-gitlab-ee' &&
        teammate.name == 'EE reviewer' &&
        teammate.projects == { 'gitlab-ee' => 'reviewer frontend' }
    end
  end

  let(:fake_git) { double('fake-git') }

  subject(:helper) { FakeDanger.new(git: fake_git) }

  describe '#all_changed_files' do
    subject { helper.all_changed_files }

    it 'interprets a list of changes from the danger git plugin' do
      expect(fake_git).to receive(:added_files) { %w[a b c.old] }
      expect(fake_git).to receive(:modified_files) { %w[d e] }
      expect(fake_git)
        .to receive(:renamed_files)
        .at_least(:once)
        .and_return([{ before: 'c.old', after: 'c.new' }])

      is_expected.to contain_exactly('a', 'b', 'c.new', 'd', 'e')
    end
  end

  describe '#ee?' do
    subject { helper.ee? }

    it 'returns true if CI_PROJECT_NAME if set to gitlab-ee' do
      stub_env('CI_PROJECT_NAME', 'gitlab-ee')
      expect(File).not_to receive(:exist?)

      is_expected.to be_truthy
    end

    it 'delegates to CHANGELOG-EE.md existence if CI_PROJECT_NAME is set to something else' do
      stub_env('CI_PROJECT_NAME', 'something else')
      expect(File).to receive(:exist?).with('../../CHANGELOG-EE.md') { true }

      is_expected.to be_truthy
    end

    it 'returns true if CHANGELOG-EE.md exists' do
      stub_env('CI_PROJECT_NAME', nil)
      expect(File).to receive(:exist?).with('../../CHANGELOG-EE.md') { true }

      is_expected.to be_truthy
    end

    it "returns false if CHANGELOG-EE.md doesn't exist" do
      stub_env('CI_PROJECT_NAME', nil)
      expect(File).to receive(:exist?).with('../../CHANGELOG-EE.md') { false }

      is_expected.to be_falsy
    end
  end

  describe '#project_name' do
    subject { helper.project_name }

    it 'returns gitlab-ee if ee? returns true' do
      expect(helper).to receive(:ee?) { true }

      is_expected.to eq('gitlab-ee')
    end

    it 'returns gitlab-ce if ee? returns false' do
      expect(helper).to receive(:ee?) { false }

      is_expected.to eq('gitlab-ce')
    end
  end

  describe '#team' do
    subject(:team) { helper.team }

    context 'HTTP failure' do
      before do
        WebMock
          .stub_request(:get, 'https://about.gitlab.com/roulette.json')
          .to_return(status: 404)
      end

      it 'raises a pretty error' do
        expect { team }.to raise_error(/Failed to read/)
      end
    end

    context 'JSON failure' do
      before do
        WebMock
          .stub_request(:get, 'https://about.gitlab.com/roulette.json')
          .to_return(body: 'INVALID JSON')
      end

      it 'raises a pretty error' do
        expect { team }.to raise_error(/Failed to parse/)
      end
    end

    context 'success' do
      before do
        WebMock
          .stub_request(:get, 'https://about.gitlab.com/roulette.json')
          .to_return(body: teammate_json)
      end

      it 'returns an array of teammates' do
        is_expected.to contain_exactly(ce_teammate_matcher, ee_teammate_matcher)
      end

      it 'memoizes the result' do
        expect(team.object_id).to eq(helper.team.object_id)
      end
    end
  end

  describe '#project_team' do
    subject { helper.project_team }

    before do
      WebMock
        .stub_request(:get, 'https://about.gitlab.com/roulette.json')
        .to_return(body: teammate_json)
    end

    it 'filters team by project_name' do
      expect(helper)
        .to receive(:project_name)
        .at_least(:once)
        .and_return('gitlab-ce')

      is_expected.to contain_exactly(ce_teammate_matcher)
    end
  end

  describe '#changes_by_category' do
    it 'categorizes changed files' do
      expect(fake_git).to receive(:added_files) { %w[foo foo.md foo.rb foo.js db/foo qa/foo ee/changelogs/foo.yml] }
      allow(fake_git).to receive(:modified_files) { [] }
      allow(fake_git).to receive(:renamed_files) { [] }

      expect(helper.changes_by_category).to eq(
        backend: %w[foo.rb],
        database: %w[db/foo],
        docs: %w[foo.md],
        frontend: %w[foo.js],
        none: %w[ee/changelogs/foo.yml],
        qa: %w[qa/foo],
        unknown: %w[foo]
      )
    end
  end

  describe '#category_for_file' do
    where(:path, :expected_category) do
      'doc/foo'         | :docs
      'CONTRIBUTING.md' | :docs
      'LICENSE'         | :docs
      'MAINTENANCE.md'  | :docs
      'PHILOSOPHY.md'   | :docs
      'PROCESS.md'      | :docs
      'README.md'       | :docs

      'ee/doc/foo'      | :unknown
      'ee/README'       | :unknown

      'app/assets/foo'       | :frontend
      'app/views/foo'        | :frontend
      'public/foo'           | :frontend
      'spec/javascripts/foo' | :frontend
      'vendor/assets/foo'    | :frontend
      'jest.config.js'       | :frontend
      'package.json'         | :frontend
      'yarn.lock'            | :frontend

      'ee/app/assets/foo'       | :frontend
      'ee/app/views/foo'        | :frontend
      'ee/spec/javascripts/foo' | :frontend

      'app/models/foo' | :backend
      'bin/foo'        | :backend
      'config/foo'     | :backend
      'danger/foo'     | :backend
      'lib/foo'        | :backend
      'rubocop/foo'    | :backend
      'scripts/foo'    | :backend
      'spec/foo'       | :backend
      'spec/foo/bar'   | :backend

      'ee/app/foo'      | :backend
      'ee/bin/foo'      | :backend
      'ee/spec/foo'     | :backend
      'ee/spec/foo/bar' | :backend

      'generator_templates/foo' | :backend
      'vendor/languages.yml'    | :backend
      'vendor/licenses.csv'     | :backend

      'Dangerfile'     | :backend
      'Gemfile'        | :backend
      'Gemfile.lock'   | :backend
      'Procfile'       | :backend
      'Rakefile'       | :backend
      '.gitlab-ci.yml' | :backend
      'FOO_VERSION'    | :backend

      'ee/FOO_VERSION' | :unknown

      'db/foo' | :database
      'qa/foo' | :qa

      'ee/db/foo' | :database
      'ee/qa/foo' | :qa

      'changelogs/foo'    | :none
      'ee/changelogs/foo' | :none

      'FOO'          | :unknown
      'foo'          | :unknown

      'foo/bar.rb'  | :backend
      'foo/bar.js'  | :frontend
      'foo/bar.txt' | :docs
      'foo/bar.md'  | :docs
    end

    with_them do
      subject { helper.category_for_file(path) }

      it { is_expected.to eq(expected_category) }
    end
  end

  describe '#label_for_category' do
    where(:category, :expected_label) do
      :backend   | '~backend'
      :database  | '~database'
      :docs      | '~Documentation'
      :foo       | '~foo'
      :frontend  | '~frontend'
      :none      | ''
      :qa        | '~QA'
    end

    with_them do
      subject { helper.label_for_category(category) }

      it { is_expected.to eq(expected_label) }
    end
  end
end