summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/danger/base_linter_spec.rb
blob: 0136a0278ae4a3e777bbe9fa67e82ce1a49686e3 (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
# frozen_string_literal: true

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

require 'gitlab/danger/base_linter'

RSpec.describe Gitlab::Danger::BaseLinter do
  let(:commit_class) do
    Struct.new(:message, :sha, :diff_parent)
  end

  let(:commit_message) { 'A commit message' }
  let(:commit) { commit_class.new(commit_message, anything, anything) }

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

  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(:subject_too_long, described_class.subject_description)
      end

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

  describe '#add_problem' do
    it 'stores messages in #failures' do
      commit_linter.add_problem(:subject_too_long, '%s')

      expect(commit_linter.problems).to eq({ subject_too_long: described_class.problems_mapping[:subject_too_long] })
    end
  end

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

      expect(commit_linter.problems).to be_empty
    end
  end

  describe '#lint_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.subject_description)

        commit_linter.lint_subject
      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.subject_description)

        commit_linter.lint_subject
      end
    end

    context 'when ignoring length issues for subject having not-ready wording' do
      using RSpec::Parameterized::TableSyntax

      let(:final_message) { 'A B C' }

      context 'when used as prefix' do
        where(prefix: [
          'WIP: ',
          'WIP:',
          'wIp:',
          '[WIP] ',
          '[WIP]',
          '[draft]',
          '[draft] ',
          '(draft)',
          '(draft) ',
          'draft - ',
          'draft: ',
          'draft:',
          'DRAFT:'
        ])

        with_them do
          it 'does not have any problems' do
            commit_message = prefix + final_message + 'D' * (described_class::MAX_LINE_LENGTH - final_message.size)
            commit = commit_class.new(commit_message, anything, anything)

            linter = described_class.new(commit).lint_subject

            expect(linter.problems).to be_empty
          end
        end
      end

      context 'when used as suffix' do
        where(suffix: %w[WIP draft])

        with_them do
          it 'does not have any problems' do
            commit_message = final_message + 'D' * (described_class::MAX_LINE_LENGTH - final_message.size) + suffix
            commit = commit_class.new(commit_message, anything, anything)

            linter = described_class.new(commit).lint_subject

            expect(linter.problems).to be_empty
          end
        end
      end
    end

    context 'when subject does not have enough words and is 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.subject_description)
        expect(commit_linter).to receive(:add_problem).with(:subject_too_long, described_class.subject_description)

        commit_linter.lint_subject
      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.subject_description)

        commit_linter.lint_subject
      end
    end

    [
      '[ci skip] A commit message',
      '[Ci skip] A commit message',
      '[API] A commit message',
      'api: A commit message',
      'API: A commit message',
      'API: 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_subject
        end
      end
    end

    [
      '[ci skip]A commit message',
      '[Ci skip]  A commit message',
      '[ci skip] a commit message',
      'api: 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.subject_description)

          commit_linter.lint_subject
        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.subject_description)

        commit_linter.lint_subject
      end
    end
  end
end