Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added short circuit check
Based on the 'debugger' check, this checks for accidental short-circuits often used in debugging.

```
if true && something
if false && something
```
  • Loading branch information
ideasasylum committed Apr 20, 2016
commit ae4316adb1de463e8b641550d79f1be39b52b2ef
20 changes: 20 additions & 0 deletions lib/plugins/pre_commit/checks/short_circuit.rb
Original file line number Diff line number Diff line change
@@ -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
"^.*([true|false] \&\&)"
end

def self.description
"Finds files with a logical short circuit like 'if true &&'"
end

end
end
end
11 changes: 11 additions & 0 deletions test/files/shortcircuit_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ShortCircuitFile
def blam
if true && something
puts 'this will always print'
end

if false && something
puts 'this will never print'
end
end
end
22 changes: 22 additions & 0 deletions test/unit/plugins/pre_commit/checks/shortcircuit_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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_equal([
"Logical short circuit found:",
"test/files/shortcircuit_file.rb:3: if true && something",
"test/files/shortcircuit_file.rb:7: if false && something"
])
end
end