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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre commit hook for fasterer #414

Merged
merged 1 commit into from Aug 18, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/default.yml
Expand Up @@ -247,6 +247,11 @@ PreCommit:
description: 'Check for file execute permissions'
quiet: true

Fasterer:
enabled: false
description: 'Suggest some speed improvements'
required_executable: 'fasterer'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should have include: '**/*.rb' since it's Ruby-specific and doesn't require a full repo context like Brakeman does.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching! Addressed in 4d9402b.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!


ForbiddenBranches:
enabled: false
description: 'Check for commit to forbidden branch'
Expand Down
23 changes: 23 additions & 0 deletions lib/overcommit/hook/pre_commit/fasterer.rb
@@ -0,0 +1,23 @@
module Overcommit::Hook::PreCommit
# Runs `fasterer` against any modified Ruby files.
#
# @see https://github.com/DamirSvrtan/fasterer
class Fasterer < Base
def run
result = execute(command)
output = result.stdout

if extract_offense_num(output) == 0
:pass
else
return [:warn, output]
end
end

private

def extract_offense_num(raw_output)
raw_output.scan(/(\d+) offense detected/).flatten.map(&:to_i).inject(0, :+)
end
end
end
69 changes: 69 additions & 0 deletions spec/overcommit/hook/pre_commit/fasterer_spec.rb
@@ -0,0 +1,69 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Fasterer do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.js file2.js])
end

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

before do
subject.stub(:execute).with(%w[fasterer]).and_return(result)
end

context 'and has 2 suggestions for speed improvement' do
let(:result) do
double(
success?: false,
stdout: <<-EOF
spec/models/product_spec.rb
Using each_with_index is slower than while loop. Occurred at lines: 52.

1 files inspected, 1 offense detected
spec/models/book_spec.rb
Using each_with_index is slower than while loop. Occurred at lines: 32.

1 files inspected, 1 offense detected
spec/models/blog_spec.rb
Using each_with_index is slower than while loop. Occurred at lines: 12.

2 files inspected, 0 offense detected
EOF
)
end

it { should warn }
end

context 'and has single suggestion for speed improvement' do
let(:result) do
double(
success?: false,
stdout: <<-EOF
spec/models/product_spec.rb
Using each_with_index is slower than while loop. Occurred at lines: 52.

1 files inspected, 1 offense detected
EOF
)
end

it { should warn }
end

context 'and does not have any suggestion' do
let(:result) do
double(success?: true, stdout: '55 files inspected, 0 offenses detected')
end

it { should pass }
end
end