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

require 'fast_spec_helper'
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 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_subject

        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.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