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

[FEATURE] Pre-commit hook: check for structural similarities with flay #585

Merged
merged 5 commits into from
Jul 17, 2018
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
* [ExecutePermissions](lib/overcommit/hook/pre_commit/execute_permissions.rb)
* [Fasterer](lib/overcommit/hook/pre_commit/fasterer.rb)
* [FixMe](lib/overcommit/hook/pre_commit/fix_me.rb)
* [Flay](lib/overcommit/hook/pre_commit/flay.rb)
* [Foodcritic](lib/overcommit/hook/pre_commit/foodcritic.rb)
* [ForbiddenBranches](lib/overcommit/hook/pre_commit/forbidden_branches.rb)
* [GoLint](lib/overcommit/hook/pre_commit/go_lint.rb)
Expand Down
10 changes: 10 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ PreCommit:
flags: ['-IEHnw']
keywords: ['BROKEN', 'BUG', 'ERROR', 'FIXME', 'HACK', 'NOTE', 'OPTIMIZE', 'REVIEW', 'TODO', 'WTF', 'XXX']

Flay:
enabled: false
description: 'Analyze ruby code for structural similarities with Flay'
required_executable: 'flay'
install_command: 'gem install flay'
mass_threshold: 16
fuzzy: 1
liberal: false
include: '**/*.rb'

Foodcritic:
enabled: false
description: 'Analyze with Foodcritic'
Expand Down
36 changes: 36 additions & 0 deletions lib/overcommit/hook/pre_commit/flay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Overcommit::Hook::PreCommit
# Runs `flay` against any modified files.
#
# @see https://github.com/seattlerb/flay
class Flay < Base
# Flay prints two kinds of messages:
#
# 1) IDENTICAL code found in :defn (mass*2 = MASS)
# file_path_1.rb:LINE_1
# file_path_2.rb:LINE_2
#
# 2) Similar code found in :defn (mass = MASS)
# file_path_1.rb:LINE_1
# file_path_2.rb:LINE_2
#

def run
command = ['flay', '--mass', @config['mass_threshold'].to_s, '--fuzzy', @config['fuzzy'].to_s]
# Use a more liberal detection method
command += ['--liberal'] if @config['liberal']
messages = []
# Run the command for each file
applicable_files.each do |file|
result = execute(command, args: [file])
results = result.stdout.split("\n\n")
results.shift
unless results.empty?
error_message = results.join("\n").gsub(/^\d+\)\s*/, '')
message = Overcommit::Hook::Message.new(:error, nil, nil, error_message)
messages << message
end
end
messages
end
end
end
58 changes: 58 additions & 0 deletions spec/overcommit/hook/pre_commit/flay_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Flay do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
let(:applicable_files) { %w[file1.rb] }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(applicable_files)
end

around do |example|
repo do
example.run
end
end

before do
command = %w[flay --mass 16 --fuzzy 1]
subject.stub(:execute).with(command, args: applicable_files).and_return(result)
end

context 'flay discovered two issues' do
let(:result) do
double(
success?: false,
stdout: <<-MSG
Total score (lower is better) = 268

1) IDENTICAL code found in :defn (mass*2 = 148)
app/whatever11.rb:105
app/whatever12.rb:76

2) Similar code found in :defn (mass = 120)
app/whatever21.rb:105
app/whatever22.rb:76

MSG
)
end

it { should fail_hook }
end

context 'flay discovered no issues' do
let(:result) do
double(
success?: false,
stdout: <<-MSG
Total score (lower is better) = 0
MSG
)
end

it { should pass }
end
end