Skip to content

Commit ef36c65

Browse files
committed
Add basic support of rails_best_practices pre_commit hook
1 parent c06d2af commit ef36c65

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

config/default.yml

+6
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ PreCommit:
388388
install_command: 'pip install flake8'
389389
include: '**/*.py'
390390

391+
RailsBestPractices:
392+
enabled: false
393+
description: 'Analyzing with RailsBestPractices'
394+
required_executable: 'rails_best_practices'
395+
install_command: 'gem install rails_best_practices'
396+
391397
RailsSchemaUpToDate:
392398
enabled: false
393399
description: 'Checking if database schema is up to date'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Overcommit
2+
module Hook
3+
module PreCommit
4+
# Runs `rails_best_practices` against Ruby files
5+
#
6+
# @see https://github.com/railsbp/rails_best_practices
7+
class RailsBestPractices < Base
8+
ERROR_REGEXP = /^(?<file>(?:\w:)?[^:]+):(?<line>\d+)\s-\s(?<type>.+)/
9+
10+
def run
11+
result = execute(command)
12+
13+
return :pass if result.success?
14+
return [:fail, result.stderr] unless result.stderr.empty?
15+
16+
extract_messages(
17+
filter_output(result.stdout),
18+
ERROR_REGEXP
19+
)
20+
end
21+
22+
private
23+
24+
def filter_output(stdout)
25+
stdout.split("\n").select do |message|
26+
message.match ERROR_REGEXP
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::RailsBestPractices do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
context 'when rails_best_practices exits successfully' do
9+
before do
10+
result = double('result')
11+
result.stub(:success?).and_return(true)
12+
subject.stub(:execute).and_return(result)
13+
end
14+
15+
it { should pass }
16+
end
17+
18+
context 'when rails_best_practices exits unsucessfully' do
19+
let(:result) { double('result') }
20+
21+
before do
22+
result.stub(:success?).and_return(false)
23+
subject.stub(:execute).and_return(result)
24+
end
25+
26+
context 'and it reports an error' do
27+
before do
28+
result.stub(:stdout).and_return([
29+
'file1.rb:7 - simplify render in controllers',
30+
].join("\n"))
31+
result.stub(:stderr).and_return('')
32+
end
33+
34+
it { should fail_hook }
35+
end
36+
37+
context 'when there is an error running rails_best_practices' do
38+
before do
39+
result.stub(:stdout).and_return('')
40+
result.stub(:stderr).and_return([
41+
'Something went wrong with rails_best_practices'
42+
].join("\n"))
43+
end
44+
45+
it { should fail_hook }
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)