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
sds
merged 1 commit into
sds:master
from
morizyun:feature/add_pre_commit_hook_for_fasterer
Aug 18, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!