# frozen_string_literal: true require File.expand_path('../spec_helper', __dir__) 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 'all' do it 'runs all default and passes default options' do %i[ rubocop_exceptions todos brakeman dependencies yarn_deduplicate ].each do |check| expect(@my_plugin).to receive(check) .with({}) end %i[commit_lint flay reek jshint].each do |check| expect(@my_plugin).to_not receive(check) end # act @my_plugin.all end it 'errors if all checks disabled' do disabled = Danger::DangerWCC::DEFAULT_OPTIONS.keys options = disabled.each_with_object({}) { |v, h| h[v] = false; } # expect disabled.each do |check| expect(@my_plugin).to_not receive(check) end # act expect { @my_plugin.all(options) }.to raise_error(ArgumentError) end it 'runs only enabled checks' do options = { rubocop_exceptions: false, yarn_deduplicate: false, todos: false, brakeman: false, reek: true } # expect expect(@my_plugin).to receive(:reek) expect(@my_plugin).to receive(:dependencies) expect(@my_plugin).to_not receive(:rubocop_exceptions) expect(@my_plugin).to_not receive(:commit_lint) # act @my_plugin.all options end it 'passes options' do options = Danger::DangerWCC::DEFAULT_OPTIONS.keys.each_with_object({}) do |v, h| h[v] = false end options[:commit_lint] = { test: { options: 1 } } # expect expect(@my_plugin).to receive(:commit_lint) .with({ test: { options: 1 } }) # act @my_plugin.all options end end describe 'brakeman' do it 'runs brakeman and parses diff' do allow(@my_plugin).to receive(:run_and_diff) .with(/^bundle exec brakeman/) .and_return(load_fixture('brakeman/brakeman.diff')) allow(@my_plugin).to receive(:run) .with(/^bundle exec brakeman/) .and_return(load_fixture('brakeman/brakeman.out')) # act @my_plugin.brakeman # expect warnings = @dangerfile.violation_report[:errors] expect(warnings.length).to eq(2) expect(warnings[0].message) .to match(/SSL Verification Bypass/) expect(warnings[0].file) .to eq('app/controllers/wmoauth_controller.rb') expect(warnings[0].line).to eq(78) expect(warnings[1].message) .to match(/Unescaped model attribute near line/) expect(warnings[1].file) .to eq('app/views/sections/_discussion_questions.html.erb') expect(warnings[1].line).to eq(4) end end describe 'flay' do it 'runs flay and reports only where changed' do allow(@my_plugin).to receive(:run) .with(/flay/) .and_return(load_fixture('flay.txt')) allow(@git).to receive(:diff) .and_return([load_diff( 'app/models/discourse.rb', 'flay' )]) # act @my_plugin.flay # assert warnings = @dangerfile.violation_report[:warnings] expect(warnings.length).to eq(1) expect(warnings[0].message) .to eq("IDENTICAL code found in :if (mass*2 = 152) at:\n"\ ' app/models/discourse.rb#L177') end end end end