# frozen_string_literal: true
require File.expand_path('../../spec_helper', __FILE__)
module Danger
describe Danger::DangerWCC do
before do
@dangerfile = testing_dangerfile
@my_plugin = @dangerfile.wcc
@git = @dangerfile.git
@github = @dangerfile.github
allow(@github).to receive(:html_link) do |text|
"#{text}"
end
end
describe 'rubocop exceptions' do
it 'Warns when Rubocop exception added without context' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'exception_reenabled'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:warnings])
.to eq([Violation.new('Rubocop rule Rails/SomeRule disabled in '\
'spec/fixtures/rubocop_exception.rb'\
" \nPlease provide an explanation why this rule was disabled.",
false,
'spec/fixtures/rubocop_exception.rb',
5)])
end
it 'Errors when rubocop exception not reenabled' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'exception_misspelled'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:errors])
.to eq([Violation.new('Rubocop rule Lint/SomeLint disabled in '\
'spec/fixtures/rubocop_exception.rb'\
" \nPlease provide an explanation why this rule was disabled."\
"\n\nThe rule was not reenabled!\n"\
'Please add a `rubocop:enable` comment so the rule is disabled '\
'for the minimal scope.',
false,
'spec/fixtures/rubocop_exception.rb',
10)])
end
it 'Messages when exception added with context' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'exception_context'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:messages])
.to eq([Violation.new('Rubocop rule Metrics/LineLength disabled in '\
'spec/fixtures/rubocop_exception.rb'\
" explanation:\n\t> Disabling because I want to.",
false,
'spec/fixtures/rubocop_exception.rb',
15)])
end
it 'Messages when exception added with multiline context' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'exception_multiline_context'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:messages])
.to eq([Violation.new('Rubocop rule Layout/AlignHash disabled in '\
'spec/fixtures/rubocop_exception.rb'\
' explanation:'\
"\n\t> I want to disable this because.."\
"\n\t> I forgot the reasons.",
false,
'spec/fixtures/rubocop_exception.rb',
30)])
end
it 'Messages when exception added in between existing comments' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'exception_insert_context'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:messages])
.to eq([Violation.new('Rubocop rule Metrics/ABC disabled in '\
'spec/fixtures/rubocop_exception.rb'\
' explanation:'\
"\n\t> here is my comment why we are disabling ABC",
false,
'spec/fixtures/rubocop_exception.rb',
21)])
end
it 'Does not message when no exception added' do
allow(@git).to receive(:diff)
.and_return([load_diff(
'spec/fixtures/rubocop_exception.rb',
'no_exception'
)])
# act
@my_plugin.rubocop_exceptions
expect(@dangerfile.violation_report[:messages]).to eq([])
expect(@dangerfile.violation_report[:warnings]).to eq([])
expect(@dangerfile.violation_report[:errors]).to eq([])
end
end
end
end