summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/danger/commit_linter_spec.rb
blob: 882cede759be03aafe9bcb40f396d835157dbcd7 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require_relative 'danger_spec_helper'

require 'gitlab/danger/commit_linter'

RSpec.describe Gitlab::Danger::CommitLinter do
  using RSpec::Parameterized::TableSyntax

  let(:total_files_changed) { 2 }
  let(:total_lines_changed) { 10 }
  let(:stats) { { total: { files: total_files_changed, lines: total_lines_changed } } }
  let(:diff_parent) { Struct.new(:stats).new(stats) }
  let(:commit_class) do
    Struct.new(:message, :sha, :diff_parent)
  end

  let(:commit_message) { 'A commit message' }
  let(:commit_sha) { 'abcd1234' }
  let(:commit) { commit_class.new(commit_message, commit_sha, diff_parent) }

  subject(:commit_linter) { described_class.new(commit) }

  describe '#fixup?' do
    where(:commit_message, :is_fixup) do
      'A commit message' | false
      'fixup!' | true
      'fixup! A commit message' | true
      'squash!' | true
      'squash! A commit message' | true
    end

    with_them do
      it 'is true when commit message starts with "fixup!" or "squash!"' do
        expect(commit_linter.fixup?).to be(is_fixup)
      end
    end
  end

  describe '#suggestion?' do
    where(:commit_message, :is_suggestion) do
      'A commit message' | false
      'Apply suggestion to' | true
      'Apply suggestion to "A commit message"' | true
    end

    with_them do
      it 'is true when commit message starts with "Apply suggestion to"' do
        expect(commit_linter.suggestion?).to be(is_suggestion)
      end
    end
  end

  describe '#merge?' do
    where(:commit_message, :is_merge) do
      'A commit message' | false
      'Merge branch' | true
      'Merge branch "A commit message"' | true
    end

    with_them do
      it 'is true when commit message starts with "Merge branch"' do
        expect(commit_linter.merge?).to be(is_merge)
      end
    end
  end

  describe '#revert?' do
    where(:commit_message, :is_revert) do
      'A commit message' | false
      'Revert' | false
      'Revert "' | true
      'Revert "A commit message"' | true
    end

    with_them do
      it 'is true when commit message starts with "Revert \""' do
        expect(commit_linter.revert?).to be(is_revert)
      end
    end
  end

  describe '#multi_line?' do
    where(:commit_message, :is_multi_line) do
      "A commit message" | false
      "A commit message\n" | false
      "A commit message\n\n" | false
      "A commit message\n\nSigned-off-by: User Name <user@name.me>" | false
      "A commit message\n\nWith details" | true
    end

    with_them do
      it 'is true when commit message contains details' do
        expect(commit_linter.multi_line?).to be(is_multi_line)
      end
    end
  end

  describe '#failed?' do
    context 'with no failures' do
      it { expect(commit_linter).not_to be_failed }
    end

    context 'with failures' do
      before do
        commit_linter.add_problem(:details_line_too_long)
      end

      it { expect(commit_linter).to be_failed }
    end
  end

  describe '#add_problem' do
    it 'stores messages in #failures' do
      commit_linter.add_problem(:details_line_too_long)

      expect(commit_linter.problems).to eq({ details_line_too_long: described_class::PROBLEMS[:details_line_too_long] })
    end
  end

  shared_examples 'a valid commit' do
    it 'does not have any problem' do
      commit_linter.lint

      expect(commit_linter.problems).to be_empty
    end
  end

  describe '#lint' do
    describe 'subject' do
      context 'when subject valid' do
        it_behaves_like 'a valid commit'
      end

      context 'when subject is too short' do
        let(:commit_message) { 'A B' }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:subject_too_short, described_class::DEFAULT_SUBJECT_DESCRIPTION)

          commit_linter.lint
        end
      end

      context 'when subject is too long' do
        let(:commit_message) { 'A B ' + 'C' * described_class::MAX_LINE_LENGTH }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:subject_too_long, described_class::DEFAULT_SUBJECT_DESCRIPTION)

          commit_linter.lint
        end
      end

      context 'when subject is a WIP' do
        let(:final_message) { 'A B C' }
        # commit message with prefix will be over max length. commit message without prefix will be of maximum size
        let(:commit_message) { described_class::WIP_PREFIX + final_message + 'D' * (described_class::MAX_LINE_LENGTH - final_message.size) }

        it 'does not have any problems' do
          commit_linter.lint

          expect(commit_linter.problems).to be_empty
        end
      end

      context 'when subject is too short and too long' do
        let(:commit_message) { 'A ' + 'B' * described_class::MAX_LINE_LENGTH }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:subject_too_short, described_class::DEFAULT_SUBJECT_DESCRIPTION)
          expect(commit_linter).to receive(:add_problem).with(:subject_too_long, described_class::DEFAULT_SUBJECT_DESCRIPTION)

          commit_linter.lint
        end
      end

      context 'when subject starts with lowercase' do
        let(:commit_message) { 'a B C' }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:subject_starts_with_lowercase, described_class::DEFAULT_SUBJECT_DESCRIPTION)

          commit_linter.lint
        end
      end

      [
        '[ci skip] A commit message',
        '[Ci skip] A commit message',
        '[API] A commit message'
      ].each do |message|
        context "when subject is '#{message}'" do
          let(:commit_message) { message }

          it 'does not add a problem' do
            expect(commit_linter).not_to receive(:add_problem)

            commit_linter.lint
          end
        end
      end

      [
        '[ci skip]A commit message',
        '[Ci skip]  A commit message',
        '[ci skip] a commit message',
        '! A commit message'
      ].each do |message|
        context "when subject is '#{message}'" do
          let(:commit_message) { message }

          it 'adds a problem' do
            expect(commit_linter).to receive(:add_problem).with(:subject_starts_with_lowercase, described_class::DEFAULT_SUBJECT_DESCRIPTION)

            commit_linter.lint
          end
        end
      end

      context 'when subject ends with a period' do
        let(:commit_message) { 'A B C.' }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:subject_ends_with_a_period, described_class::DEFAULT_SUBJECT_DESCRIPTION)

          commit_linter.lint
        end
      end
    end

    describe 'separator' do
      context 'when separator is missing' do
        let(:commit_message) { "A B C\n" }

        it_behaves_like 'a valid commit'
      end

      context 'when separator is a blank line' do
        let(:commit_message) { "A B C\n\nMore details." }

        it_behaves_like 'a valid commit'
      end

      context 'when separator is missing' do
        let(:commit_message) { "A B C\nMore details." }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:separator_missing)

          commit_linter.lint
        end
      end
    end

    describe 'details' do
      context 'when details are valid' do
        let(:commit_message) { "A B C\n\nMore details." }

        it_behaves_like 'a valid commit'
      end

      context 'when no details are given and many files are changed' do
        let(:total_files_changed) { described_class::MAX_CHANGED_FILES_IN_COMMIT + 1 }

        it_behaves_like 'a valid commit'
      end

      context 'when no details are given and many lines are changed' do
        let(:total_lines_changed) { described_class::MAX_CHANGED_LINES_IN_COMMIT + 1 }

        it_behaves_like 'a valid commit'
      end

      context 'when no details are given and many files and lines are changed' do
        let(:total_files_changed) { described_class::MAX_CHANGED_FILES_IN_COMMIT + 1 }
        let(:total_lines_changed) { described_class::MAX_CHANGED_LINES_IN_COMMIT + 1 }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:details_too_many_changes)

          commit_linter.lint
        end
      end

      context 'when details exceeds the max line length' do
        let(:commit_message) { "A B C\n\n" + 'D' * (described_class::MAX_LINE_LENGTH + 1) }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:details_line_too_long)

          commit_linter.lint
        end
      end

      context 'when details exceeds the max line length including a URL' do
        let(:commit_message) { "A B C\n\nhttps://gitlab.com" + 'D' * described_class::MAX_LINE_LENGTH }

        it_behaves_like 'a valid commit'
      end
    end

    describe 'message' do
      context 'when message includes a text emoji' do
        let(:commit_message) { "A commit message :+1:" }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:message_contains_text_emoji)

          commit_linter.lint
        end
      end

      context 'when message includes a unicode emoji' do
        let(:commit_message) { "A commit message 🚀" }

        it 'adds a problem' do
          expect(commit_linter).to receive(:add_problem).with(:message_contains_unicode_emoji)

          commit_linter.lint
        end
      end

      context 'when message includes a value that is surrounded by backticks' do
        let(:commit_message) { "A commit message `%20`" }

        it 'does not add a problem' do
          expect(commit_linter).not_to receive(:add_problem)

          commit_linter.lint
        end
      end

      context 'when message includes a short reference' do
        [
          'A commit message to fix #1234',
          'A commit message to fix !1234',
          'A commit message to fix &1234',
          'A commit message to fix %1234',
          'A commit message to fix gitlab#1234',
          'A commit message to fix gitlab!1234',
          'A commit message to fix gitlab&1234',
          'A commit message to fix gitlab%1234',
          'A commit message to fix gitlab-org/gitlab#1234',
          'A commit message to fix gitlab-org/gitlab!1234',
          'A commit message to fix gitlab-org/gitlab&1234',
          'A commit message to fix gitlab-org/gitlab%1234',
          'A commit message to fix "gitlab-org/gitlab%1234"',
          'A commit message to fix `gitlab-org/gitlab%1234'
        ].each do |message|
          let(:commit_message) { message }

          it 'adds a problem' do
            expect(commit_linter).to receive(:add_problem).with(:message_contains_short_reference)

            commit_linter.lint
          end
        end
      end
    end
  end
end