Skip to content

Commit b58b635

Browse files
sdsquorauk
andauthored
Add RSpec Hook to PreCommit, Add Applicable files if set (#842)
- **Add RSpec to pre_commit hooks** - **Add applicable files to RSpec command if include configuration set** - **Fix RuboCop errors** --------- Co-authored-by: Max Prettyjohns <[email protected]>
1 parent c4aa796 commit b58b635

File tree

6 files changed

+194
-8
lines changed

6 files changed

+194
-8
lines changed

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,11 @@ PreCommit:
709709
install_command: 'pip install restructuredtext_lint'
710710
include: '**/*.rst'
711711

712+
RSpec:
713+
enabled: false
714+
description: 'Run tests with Rspec'
715+
required_executable: 'rspec'
716+
712717
RuboCop:
713718
enabled: false
714719
description: 'Analyze with RuboCop'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'overcommit/hook/shared/r_spec'
4+
5+
module Overcommit::Hook::PreCommit
6+
# Runs `rspec` test suite
7+
#
8+
# @see http://rspec.info/
9+
class RSpec < Base
10+
include Overcommit::Hook::Shared::RSpec
11+
end
12+
end
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
# frozen_string_literal: true
22

3+
require 'overcommit/hook/shared/r_spec'
4+
35
module Overcommit::Hook::PrePush
4-
# Runs `rspec` test suite before push
6+
# Runs `rspec` test suite
57
#
68
# @see http://rspec.info/
79
class RSpec < Base
8-
def run
9-
result = execute(command)
10-
return :pass if result.success?
11-
12-
output = result.stdout + result.stderr
13-
[:fail, output]
14-
end
10+
include Overcommit::Hook::Shared::RSpec
1511
end
1612
end

lib/overcommit/hook/shared/r_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Overcommit::Hook::Shared
4+
# Runs `rspec` test suite before push
5+
#
6+
# @see http://rspec.info/
7+
module RSpec
8+
def run
9+
result = if @config['include']
10+
execute(command, args: applicable_files)
11+
else
12+
execute(command)
13+
end
14+
15+
return :pass if result.success?
16+
17+
output = result.stdout + result.stderr
18+
[:fail, output]
19+
end
20+
end
21+
end
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Overcommit::Hook::PreCommit::RSpec do
6+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
7+
let(:context) { double('context') }
8+
subject { described_class.new(config, context) }
9+
10+
context 'when rspec exits successfully' do
11+
let(:result) { double('result') }
12+
13+
before do
14+
result.stub(:success?).and_return(true)
15+
subject.stub(:execute).and_return(result)
16+
end
17+
18+
it { should pass }
19+
20+
it {
21+
expect(subject).to receive(:execute).with(['rspec']).and_return(result)
22+
23+
subject.run
24+
}
25+
end
26+
27+
context 'with included files set' do
28+
let(:result) { double('result') }
29+
let(:config) do
30+
super().merge(Overcommit::Configuration.new(
31+
'PreCommit' => {
32+
'RSpec' => {
33+
'include' => ['**/*_spec.rb'],
34+
}
35+
}
36+
))
37+
end
38+
39+
let(:context) { double('context') }
40+
41+
before do
42+
result.stub(:success?).and_return(true)
43+
subject.stub(:execute).and_return(result)
44+
subject.stub(:applicable_files).and_return('spec/test_spec.rb')
45+
end
46+
47+
it { should pass }
48+
49+
it {
50+
expect(subject).to receive(:execute).with(['rspec'],
51+
args: 'spec/test_spec.rb').and_return(result)
52+
53+
subject.run
54+
}
55+
end
56+
57+
context 'when rspec exits unsuccessfully' do
58+
let(:result) { double('result') }
59+
60+
before do
61+
result.stub(:success?).and_return(false)
62+
subject.stub(:execute).and_return(result)
63+
end
64+
65+
context 'with a runtime error' do
66+
before do
67+
result.stub(stdout: '', stderr: <<-MSG)
68+
/home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load': /home/user/dev/github/overcommit/spec/overcommit/hook/pre_push/rspec_spec.rb:49: can't find string "EOS" anywhere before EOF (SyntaxError)
69+
/home/user/dev/overcommit/spec/overcommit/hook/pre_push/rspec_spec.rb:29: syntax error, unexpected end-of-input
70+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
71+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
72+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
73+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
74+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
75+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
76+
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
77+
from /home/user/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>'
78+
from /home/user/.rbenv/versions/2.2.1/bin/rspec:23:in `load'
79+
from /home/user/.rbenv/versions/2.2.1/bin/rspec:23:in `<main>'
80+
MSG
81+
end
82+
83+
it { should fail_hook }
84+
end
85+
86+
context 'with a test failure' do
87+
before do
88+
result.stub(stderr: '', stdout: <<-MSG)
89+
.FF
90+
91+
Failures:
92+
93+
1) Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a runtime error should fail
94+
Failure/Error: it { should fail_hook }
95+
expected that the hook would fail
96+
# ./spec/overcommit/hook/pre_push/rspec_spec.rb:45:in `block (4 levels) in <top (required)>'
97+
98+
2) Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a test failure should fail
99+
Failure/Error: it { should fail_hook }
100+
expected that the hook would fail
101+
# ./spec/overcommit/hook/pre_push/rspec_spec.rb:57:in `block (4 levels) in <top (required)>'
102+
103+
Finished in 0.00505 seconds (files took 0.27437 seconds to load)
104+
3 examples, 2 failures
105+
106+
Failed examples:
107+
108+
rspec ./spec/overcommit/hook/pre_push/rspec_spec.rb:45 # Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a runtime error should fail
109+
rspec ./spec/overcommit/hook/pre_push/rspec_spec.rb:57 # Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a test failure should fail
110+
MSG
111+
end
112+
113+
it { should fail_hook }
114+
end
115+
end
116+
end

spec/overcommit/hook/pre_push/r_spec_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,42 @@
1616
end
1717

1818
it { should pass }
19+
20+
it {
21+
expect(subject).to receive(:execute).with(['rspec']).and_return(result)
22+
23+
subject.run
24+
}
25+
end
26+
27+
context 'with included files set' do
28+
let(:result) { double('result') }
29+
let(:config) do
30+
super().merge(Overcommit::Configuration.new(
31+
'PrePush' => {
32+
'RSpec' => {
33+
'include' => ['**/*_spec.rb'],
34+
}
35+
}
36+
))
37+
end
38+
39+
let(:context) { double('context') }
40+
41+
before do
42+
result.stub(:success?).and_return(true)
43+
subject.stub(:execute).and_return(result)
44+
subject.stub(:applicable_files).and_return('spec/test_spec.rb')
45+
end
46+
47+
it { should pass }
48+
49+
it {
50+
expect(subject).to receive(:execute).with(['rspec'],
51+
args: 'spec/test_spec.rb').and_return(result)
52+
53+
subject.run
54+
}
1955
end
2056

2157
context 'when rspec exits unsuccessfully' do

0 commit comments

Comments
 (0)