Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a short circuit check #237

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
"(false\s*\&\&)|(true\s*\\|\\|)"
end

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

end
end
end
25 changes: 25 additions & 0 deletions test/files/shortcircuit_file.rb
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions test/unit/plugins/pre_commit/checks/short_circuit_test.rb
Original file line number Diff line number Diff line change
@@ -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