merge_request_linter_spec.rb 1.44 KB
Newer Older
Doug Stull's avatar
Doug Stull committed
1 2 3 4 5
# frozen_string_literal: true

require 'rspec-parameterized'
require_relative 'danger_spec_helper'

6
require_relative '../../../tooling/danger/merge_request_linter'
Doug Stull's avatar
Doug Stull committed
7

8
RSpec.describe Tooling::Danger::MergeRequestLinter do
Doug Stull's avatar
Doug Stull committed
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
  using RSpec::Parameterized::TableSyntax

  let(:mr_class) do
    Struct.new(:message, :sha, :diff_parent)
  end

  let(:mr_title) { 'A B ' + 'C' }
  let(:merge_request) { mr_class.new(mr_title, anything, anything) }

  describe '#lint_subject' do
    subject(:mr_linter) { described_class.new(merge_request) }

    shared_examples 'a valid mr title' do
      it 'does not have any problem' do
        mr_linter.lint

        expect(mr_linter.problems).to be_empty
      end
    end

    context 'when subject valid' do
      it_behaves_like 'a valid mr title'
    end

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

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

        mr_linter.lint
      end
    end

    describe 'using magic mr run options' do
      where(run_option: described_class.mr_run_options_regex.split('|') +
        described_class.mr_run_options_regex.split('|').map! { |x| "[#{x}]" })

      with_them do
        let(:mr_title) { run_option + ' A B ' + 'C' * (described_class::MAX_LINE_LENGTH - 5) }

        it_behaves_like 'a valid mr title'
      end
    end
  end
end