# frozen_string_literal: true # Copied and modified from # https://github.com/jonallured/danger-commit_lint # which is licensed under the MIT License require File.expand_path('../spec_helper', __dir__) TEST_MESSAGES = { subject_cap: 'this subject needs a capital', subject_words: 'Fixed', subject_length: 'This is a really long subject line and should result in an'\ ' error', subject_period: 'This subject line ends in a period.', empty_line: "This subject line is fine\nBut then I forgot the empty line '\ 'separating the subject and the body.", all_errors: "this is a really long subject and it even ends in a period.\n'\ 'Not to mention the missing empty line!", valid: "This is a valid message\n\nYou can tell because it meets all the '\ 'criteria and the linter does not complain." }.freeze NOOP_MESSAGE = Danger::DangerWCC::CommitLint::NOOP_MESSAGE SUBJECT_LENGTH_CHECK_MESSAGE = 'Please limit commit subject line to 50 '\ 'characters.' def report_counts(status_report) status_report.values.flatten.count end module Danger describe Danger::DangerWCC do before do @dangerfile = testing_dangerfile @my_plugin = @dangerfile.wcc @git = @dangerfile.git end def message_with_sha(message) [message, sha].join "\n" end def commit_lint(options = {}) Danger::DangerWCC::CommitLint.new(@my_plugin, options).perform end describe 'commit lint' do describe 'check without configuration' do let(:sha) { '1234567' } let(:commit) { double(:commit, message: message, sha: sha) } context 'with invalid messages' do it 'fails those checks' do checks = { subject_cap: SubjectCapCheck::MESSAGE, subject_words: SubjectWordsCheck::MESSAGE, subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, warning| commit = double(:commit, message: TEST_MESSAGES[check], sha: sha) allow(@git).to receive(:commits).and_return([commit]) expect { commit_lint }.to change { @my_plugin.status_report[:errors].count }.by(1) status_report = @my_plugin.status_report expect(status_report[:errors].last) .to eq(message_with_sha(warning)) end end end context 'with all errors' do let(:message) { TEST_MESSAGES[:all_errors] } it 'fails every check' do allow(@git).to receive(:commits).and_return([commit]) commit_lint status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 4 expect(status_report[:errors]).to eq [ message_with_sha(SubjectCapCheck::MESSAGE), message_with_sha(SUBJECT_LENGTH_CHECK_MESSAGE), message_with_sha(SubjectPeriodCheck::MESSAGE), message_with_sha(EmptyLineCheck::MESSAGE) ] end end context 'with valid messages' do let(:message) { TEST_MESSAGES[:valid] } it 'does nothing' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do allow(@git).to receive(:commits).and_return([commit]) commit_lint status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end describe 'disable configuration' do let(:sha) { '1234567' } let(:commit) { double(:commit, message: message, sha: sha) } context 'with individual checks' do context 'with invalid messages' do it 'does nothing' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, _| commit = double(:commit, message: TEST_MESSAGES[check], sha: sha) allow(@git).to receive(:commits).and_return([commit]) commit_lint disable: [check] status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end context 'with all checks, implicitly' do let(:message) { TEST_MESSAGES[:all_errors] } it 'warns that nothing was checked' do allow(@git).to receive(:commits).and_return([commit]) all_checks = %i[ subject_cap subject_words subject_length subject_period empty_line ] commit_lint disable: all_checks status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 1 expect(status_report[:warnings]).to eq [NOOP_MESSAGE] end end context 'with all checks, explicitly' do let(:message) { TEST_MESSAGES[:all_errors] } it 'warns that nothing was checked' do allow(@git).to receive(:commits).and_return([commit]) commit_lint disable: :all status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 1 expect(status_report[:warnings]).to eq [NOOP_MESSAGE] end end end describe 'warn configuration' do let(:sha) { '1234567' } let(:commit) { double(:commit, message: message, sha: sha) } context 'with individual checks' do context 'with invalid messages' do it 'warns instead of failing' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, warning| commit = double(:commit, message: TEST_MESSAGES[check], sha: sha) allow(@git).to receive(:commits).and_return([commit]) expect { commit_lint warn: [check] }.to change { @my_plugin.status_report[:warnings].count }.by(1) status_report = @my_plugin.status_report expect(status_report[:warnings].last) .to eq(message_with_sha(warning)) end end end context 'with valid messages' do let(:message) { TEST_MESSAGES[:valid] } it 'does nothing' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, _| allow(@git).to receive(:commits).and_return([commit]) commit_lint warn: [check] status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end context 'with all checks' do context 'with all errors' do let(:message) { TEST_MESSAGES[:all_errors] } it 'warns instead of failing' do allow(@git).to receive(:commits).and_return([commit]) commit_lint warn: :all status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 4 expect(status_report[:warnings]).to eq [ message_with_sha(SubjectCapCheck::MESSAGE), message_with_sha(SUBJECT_LENGTH_CHECK_MESSAGE), message_with_sha(SubjectPeriodCheck::MESSAGE), message_with_sha(EmptyLineCheck::MESSAGE) ] end end context 'with a valid message' do let(:message) { TEST_MESSAGES[:valid] } it 'does nothing' do allow(@git).to receive(:commits).and_return([commit]) commit_lint warn: :all status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end describe 'fail configuration' do let(:sha) { '1234567' } let(:commit) { double(:commit, message: message, sha: sha) } context 'with individual checks' do context 'with invalid messages' do it 'fails those checks' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, warning| commit = double(:commit, message: TEST_MESSAGES[check], sha: sha) allow(@git).to receive(:commits).and_return([commit]) expect { commit_lint fail: [check] }.to change { @my_plugin.status_report[:errors].count }.by(1) status_report = @my_plugin.status_report expect(status_report[:errors].last) .to eq(message_with_sha(warning)) end end end context 'with valid messages' do let(:message) { TEST_MESSAGES[:valid] } it 'does nothing' do checks = { subject_length: SUBJECT_LENGTH_CHECK_MESSAGE, subject_period: SubjectPeriodCheck::MESSAGE, empty_line: EmptyLineCheck::MESSAGE } checks.each do |check, _| allow(@git).to receive(:commits).and_return([commit]) commit_lint fail: [check] status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end context 'with all checks' do context 'with all errors' do let(:message) { TEST_MESSAGES[:all_errors] } it 'fails those checks' do allow(@git).to receive(:commits).and_return([commit]) commit_lint fail: :all status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 4 expect(status_report[:errors]).to eq [ message_with_sha(SubjectCapCheck::MESSAGE), message_with_sha(SUBJECT_LENGTH_CHECK_MESSAGE), message_with_sha(SubjectPeriodCheck::MESSAGE), message_with_sha(EmptyLineCheck::MESSAGE) ] end end context 'with a valid message' do let(:message) { TEST_MESSAGES[:valid] } it 'does nothing' do allow(@git).to receive(:commits).and_return([commit]) commit_lint fail: :all status_report = @my_plugin.status_report expect(report_counts(status_report)).to eq 0 end end end end describe 'individual check options' do let(:sha) { '1234567' } context 'with invalid messages for given options' do it 'fails those checks' do options = [ [:subject_length, { max: 10 }], [:subject_length, { min: 100, max: 110 }] ] messages = [ 'Please limit commit subject line to 10 characters.', 'Please write a commit subject line of at least 100 characters.' ] options.each_with_index do |(check, opts), i| commit = double(:commit, message: TEST_MESSAGES[:valid], sha: sha) allow(@git).to receive(:commits).and_return([commit]) arg = {} arg[check] = opts expect { commit_lint arg }.to change { @my_plugin.status_report[:errors].count }.by(1) status_report = @my_plugin.status_report expect(status_report[:errors].last) .to eq(message_with_sha(messages[i])) end end end end end end end