Skip to content

Commit 8928d1d

Browse files
committed
Add pre commit hook for fasterer
1 parent ceb99d7 commit 8928d1d

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

config/default.yml

+5
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ PreCommit:
247247
description: 'Check for file execute permissions'
248248
quiet: true
249249

250+
Fasterer:
251+
enabled: false
252+
description: 'Suggest some speed improvements'
253+
required_executable: 'fasterer'
254+
250255
ForbiddenBranches:
251256
enabled: false
252257
description: 'Check for commit to forbidden branch'
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs `fasterer` against any modified Ruby files.
3+
#
4+
# @see https://github.com/DamirSvrtan/fasterer
5+
class Fasterer < Base
6+
def run
7+
result = execute(command)
8+
output = result.stdout
9+
10+
if extract_offense_num(output) == 0
11+
:pass
12+
else
13+
return [:warn, output]
14+
end
15+
end
16+
17+
private
18+
19+
def extract_offense_num(raw_output)
20+
raw_output.scan(/(\d+) offense detected/).flatten.map(&:to_i).inject(0, :+)
21+
end
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::Fasterer do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:applicable_files).and_return(%w[file1.js file2.js])
10+
end
11+
12+
around do |example|
13+
repo do
14+
example.run
15+
end
16+
end
17+
18+
before do
19+
subject.stub(:execute).with(%w[fasterer]).and_return(result)
20+
end
21+
22+
context 'and has 2 suggestions for speed improvement' do
23+
let(:result) do
24+
double(
25+
success?: false,
26+
stdout: <<-EOF
27+
spec/models/product_spec.rb
28+
Using each_with_index is slower than while loop. Occurred at lines: 52.
29+
30+
1 files inspected, 1 offense detected
31+
spec/models/book_spec.rb
32+
Using each_with_index is slower than while loop. Occurred at lines: 32.
33+
34+
1 files inspected, 1 offense detected
35+
spec/models/blog_spec.rb
36+
Using each_with_index is slower than while loop. Occurred at lines: 12.
37+
38+
2 files inspected, 0 offense detected
39+
EOF
40+
)
41+
end
42+
43+
it { should warn }
44+
end
45+
46+
context 'and has single suggestion for speed improvement' do
47+
let(:result) do
48+
double(
49+
success?: false,
50+
stdout: <<-EOF
51+
spec/models/product_spec.rb
52+
Using each_with_index is slower than while loop. Occurred at lines: 52.
53+
54+
1 files inspected, 1 offense detected
55+
EOF
56+
)
57+
end
58+
59+
it { should warn }
60+
end
61+
62+
context 'and does not have any suggestion' do
63+
let(:result) do
64+
double(success?: true, stdout: '55 files inspected, 0 offenses detected')
65+
end
66+
67+
it { should pass }
68+
end
69+
end

0 commit comments

Comments
 (0)