Skip to content

Commit db1b681

Browse files
vinzenttrotzig
authored andcommitted
Add RakeTarget pre-commit and pre-push hook (sds#456)
Allow to run rake targets as pre-commit and pre-push hooks. The first unsuccessfull exit code of a specified rake task will fail the hook.
1 parent cfab3cc commit db1b681

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
518518
* [Pyflakes](lib/overcommit/hook/pre_commit/pyflakes.rb)
519519
* [Pylint](lib/overcommit/hook/pre_commit/pylint.rb)
520520
* [PythonFlake8](lib/overcommit/hook/pre_commit/python_flake8.rb)
521+
* [RakeTarget](lib/overcommit/hook/pre_commit/rake_target.rb)
521522
* [RailsBestPractices](lib/overcommit/hook/pre_commit/rails_best_practices.rb)
522523
* [RailsSchemaUpToDate](lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb)
523524
* [Reek](lib/overcommit/hook/pre_commit/reek.rb)
@@ -551,6 +552,7 @@ aborted.
551552
* [Brakeman](lib/overcommit/hook/pre_push/brakeman.rb)
552553
* [Minitest](lib/overcommit/hook/pre_push/minitest.rb)
553554
* [ProtectedBranches](lib/overcommit/hook/pre_push/protected_branches.rb)
555+
* [RakeTarget](lib/overcommit/hook/pre_push/rake_target.rb)
554556
* [RSpec](lib/overcommit/hook/pre_push/r_spec.rb)
555557
* [TestUnit](lib/overcommit/hook/pre_push/test_unit.rb)
556558

config/default.yml

+20
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,16 @@ PreCommit:
458458
install_command: 'pip install flake8'
459459
include: '**/*.py'
460460

461+
RakeTarget:
462+
enabled: false
463+
description: 'Run rake targets'
464+
# targets:
465+
# - 'lint'
466+
# - 'validate'
467+
# - '...'
468+
required_executable: 'rake'
469+
install_command: 'gem install rake'
470+
461471
RailsBestPractices:
462472
enabled: false
463473
description: 'Analyze with RailsBestPractices'
@@ -895,6 +905,16 @@ PrePush:
895905
description: 'Run RSpec test suite'
896906
required_executable: 'rspec'
897907

908+
RakeTarget:
909+
enabled: false
910+
description: 'Run rake targets'
911+
# targets:
912+
# - 'lint'
913+
# - 'validate'
914+
# - '...'
915+
required_executable: 'rake'
916+
install_command: 'gem install rake'
917+
898918
Minitest:
899919
enabled: false
900920
description: 'Run Minitest test suite'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'overcommit/hook/shared/rake_target'
2+
3+
module Overcommit::Hook::PreCommit
4+
# Runs rake targets
5+
#
6+
# @see {Overcommit::Hook::Shared::RakeTarget}
7+
class RakeTarget < Base
8+
include Overcommit::Hook::Shared::RakeTarget
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'overcommit/hook/shared/rake_target'
2+
3+
module Overcommit::Hook::PrePush
4+
# Runs rake targets
5+
#
6+
# @see {Overcommit::Hook::Shared::RakeTarget}
7+
class RakeTarget < Base
8+
include Overcommit::Hook::Shared::RakeTarget
9+
end
10+
end
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Overcommit::Hook::Shared
2+
# runs specified rake targets. It fails on the first non-
3+
# successfull exit.
4+
#
5+
module RakeTarget
6+
def run
7+
targets = config['targets']
8+
9+
if Array(targets).empty?
10+
raise 'RakeTarget: targets parameter is empty. Add at least one task to ' \
11+
'the targets parameter. Valid: Array of target names or String of ' \
12+
'target names'
13+
end
14+
15+
targets.each do |task|
16+
result = execute(command + [task])
17+
unless result.success?
18+
return :fail, "Rake target #{task}:\n#{result.stdout}"
19+
end
20+
end
21+
:pass
22+
end
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::RakeTarget do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
context 'without targets parameters' do
9+
let(:result) { double('result') }
10+
it 'raises' do
11+
expect { subject.run }.to raise_error(
12+
RuntimeError, /RakeTarget: targets parameter is empty.*/
13+
)
14+
end
15+
end
16+
17+
context 'with targets parameter set' do
18+
let(:config) do
19+
super().merge(Overcommit::Configuration.new(
20+
'PreCommit' => {
21+
'RakeTarget' => {
22+
'targets' => ['test'],
23+
}
24+
}
25+
))
26+
end
27+
let(:result) { double('result') }
28+
29+
context 'when rake exits successfully' do
30+
before do
31+
result.stub(:success?).and_return(true)
32+
subject.stub(:execute).and_return(result)
33+
result.stub(:stdout).and_return('ANYTHING')
34+
end
35+
36+
it { should pass }
37+
end
38+
39+
context 'when rake exits unsuccessfully' do
40+
before do
41+
result.stub(:success?).and_return(false)
42+
subject.stub(:execute).and_return(result)
43+
result.stub(:stdout).and_return('ANYTHING')
44+
end
45+
46+
it { should fail_hook }
47+
end
48+
end
49+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PrePush::RakeTarget do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
context 'without targets parameters' do
9+
let(:result) { double('result') }
10+
it 'raises' do
11+
expect { subject.run }.to raise_error(
12+
RuntimeError, /RakeTarget: targets parameter is empty.*/
13+
)
14+
end
15+
end
16+
17+
context 'with targets parameter set' do
18+
let(:config) do
19+
super().merge(Overcommit::Configuration.new(
20+
'PrePush' => {
21+
'RakeTarget' => {
22+
'targets' => ['test'],
23+
}
24+
}
25+
))
26+
end
27+
let(:result) { double('result') }
28+
29+
context 'when rake exits successfully' do
30+
before do
31+
result.stub(:success?).and_return(true)
32+
subject.stub(:execute).and_return(result)
33+
result.stub(:stdout).and_return('ANYTHING')
34+
end
35+
36+
it { should pass }
37+
end
38+
39+
context 'when rake exits unsuccessfully' do
40+
before do
41+
result.stub(:success?).and_return(false)
42+
subject.stub(:execute).and_return(result)
43+
result.stub(:stdout).and_return('ANYTHING')
44+
end
45+
46+
it { should fail_hook }
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)