diff --git a/lib/plugins/pre_commit/checks/short_circuit.rb b/lib/plugins/pre_commit/checks/short_circuit.rb new file mode 100644 index 0000000..bc0a404 --- /dev/null +++ b/lib/plugins/pre_commit/checks/short_circuit.rb @@ -0,0 +1,20 @@ +require 'pre-commit/checks/grep' + +module PreCommit + module Checks + class ShortCircuit < Grep + def message + "Logical short circuit found:" + end + + def pattern + "(false\s*\&\&)|(true\s*\\|\\|)" + end + + def self.description + "Finds files with a logical short circuit like 'if false &&' or 'true ||'" + end + + end + end +end diff --git a/test/files/shortcircuit_file.rb b/test/files/shortcircuit_file.rb new file mode 100644 index 0000000..f1849ca --- /dev/null +++ b/test/files/shortcircuit_file.rb @@ -0,0 +1,25 @@ +class ShortCircuitFile + def blam + if false && something + puts 'this will never print' + end + + if true || something + puts 'this will always print' + end + + # These are daft but ok + if false || something + puts 'daft but valid' + end + + if true && something + puts 'daft but valid' + end + + if true + puts "this will always print" + end + + end +end diff --git a/test/unit/plugins/pre_commit/checks/short_circuit_test.rb b/test/unit/plugins/pre_commit/checks/short_circuit_test.rb new file mode 100644 index 0000000..776bdea --- /dev/null +++ b/test/unit/plugins/pre_commit/checks/short_circuit_test.rb @@ -0,0 +1,57 @@ +require 'minitest_helper' +require 'plugins/pre_commit/checks/short_circuit' + +describe PreCommit::Checks::ShortCircuit do + subject{ PreCommit::Checks::ShortCircuit.new(nil, nil, []) } + + it "succeeds if nothing changed" do + subject.call([]).must_equal nil + end + + it "succeeds if only good changes" do + subject.call([fixture_file('valid_file.rb')]).must_equal nil + end + + it "fails if file contains true ||" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.must_include( + "test/files/shortcircuit_file.rb:3: if false && something" + ) + end + + it "fails if file contains true ||" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.must_include( + "test/files/shortcircuit_file.rb:7: if true || something" + ) + end + + it "fails if file contains false &&" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.must_include( + "test/files/shortcircuit_file.rb:3: if false && something" + ) + end + + it "includes the error description" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.must_include( + "Logical short circuit found:" + ) + end + + # Daft but not 'wrong' + it "succeeds if file contains false ||" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.wont_include( + "test/files/shortcircuit_file.rb:12: if false || something" + ) + end + + it "succeeds if file contains true &&" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.wont_include( + "test/files/shortcircuit_file.rb:16: if true && something" + ) + end + + it "succeeds if file contains true" do + subject.call([fixture_file('shortcircuit_file.rb')]).to_a.wont_include( + "test/files/shortcircuit_file.rb:20: if true" + ) + end +end