Skip to content

Commit

Permalink
Add RSpec Hook to PreCommit, Add Applicable files if set (#842)
Browse files Browse the repository at this point in the history
- **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]>
  • Loading branch information
sds and quorauk authored Feb 25, 2024
1 parent c4aa796 commit b58b635
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 8 deletions.
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,11 @@ PreCommit:
install_command: 'pip install restructuredtext_lint'
include: '**/*.rst'

RSpec:
enabled: false
description: 'Run tests with Rspec'
required_executable: 'rspec'

RuboCop:
enabled: false
description: 'Analyze with RuboCop'
Expand Down
12 changes: 12 additions & 0 deletions lib/overcommit/hook/pre_commit/r_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'overcommit/hook/shared/r_spec'

module Overcommit::Hook::PreCommit
# Runs `rspec` test suite
#
# @see http://rspec.info/
class RSpec < Base
include Overcommit::Hook::Shared::RSpec
end
end
12 changes: 4 additions & 8 deletions lib/overcommit/hook/pre_push/r_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# frozen_string_literal: true

require 'overcommit/hook/shared/r_spec'

module Overcommit::Hook::PrePush
# Runs `rspec` test suite before push
# Runs `rspec` test suite
#
# @see http://rspec.info/
class RSpec < Base
def run
result = execute(command)
return :pass if result.success?

output = result.stdout + result.stderr
[:fail, output]
end
include Overcommit::Hook::Shared::RSpec
end
end
21 changes: 21 additions & 0 deletions lib/overcommit/hook/shared/r_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Overcommit::Hook::Shared
# Runs `rspec` test suite before push
#
# @see http://rspec.info/
module RSpec
def run
result = if @config['include']
execute(command, args: applicable_files)
else
execute(command)
end

return :pass if result.success?

output = result.stdout + result.stderr
[:fail, output]
end
end
end
116 changes: 116 additions & 0 deletions spec/overcommit/hook/pre_commit/r_spec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::RSpec do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

context 'when rspec exits successfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

it { should pass }

it {
expect(subject).to receive(:execute).with(['rspec']).and_return(result)

subject.run
}
end

context 'with included files set' do
let(:result) { double('result') }
let(:config) do
super().merge(Overcommit::Configuration.new(
'PreCommit' => {
'RSpec' => {
'include' => ['**/*_spec.rb'],
}
}
))
end

let(:context) { double('context') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
subject.stub(:applicable_files).and_return('spec/test_spec.rb')
end

it { should pass }

it {
expect(subject).to receive(:execute).with(['rspec'],
args: 'spec/test_spec.rb').and_return(result)

subject.run
}
end

context 'when rspec exits unsuccessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
subject.stub(:execute).and_return(result)
end

context 'with a runtime error' do
before do
result.stub(stdout: '', stderr: <<-MSG)
/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)
/home/user/dev/overcommit/spec/overcommit/hook/pre_push/rspec_spec.rb:29: syntax error, unexpected end-of-input
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'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
from /home/user/.rbenv/gems/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
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)>'
from /home/user/.rbenv/versions/2.2.1/bin/rspec:23:in `load'
from /home/user/.rbenv/versions/2.2.1/bin/rspec:23:in `<main>'
MSG
end

it { should fail_hook }
end

context 'with a test failure' do
before do
result.stub(stderr: '', stdout: <<-MSG)
.FF
Failures:
1) Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a runtime error should fail
Failure/Error: it { should fail_hook }
expected that the hook would fail
# ./spec/overcommit/hook/pre_push/rspec_spec.rb:45:in `block (4 levels) in <top (required)>'
2) Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a test failure should fail
Failure/Error: it { should fail_hook }
expected that the hook would fail
# ./spec/overcommit/hook/pre_push/rspec_spec.rb:57:in `block (4 levels) in <top (required)>'
Finished in 0.00505 seconds (files took 0.27437 seconds to load)
3 examples, 2 failures
Failed examples:
rspec ./spec/overcommit/hook/pre_push/rspec_spec.rb:45 # Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a runtime error should fail
rspec ./spec/overcommit/hook/pre_push/rspec_spec.rb:57 # Overcommit::Hook::PrePush::RSpec when rspec exits unsuccessfully with a test failure should fail
MSG
end

it { should fail_hook }
end
end
end
36 changes: 36 additions & 0 deletions spec/overcommit/hook/pre_push/r_spec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@
end

it { should pass }

it {
expect(subject).to receive(:execute).with(['rspec']).and_return(result)

subject.run
}
end

context 'with included files set' do
let(:result) { double('result') }
let(:config) do
super().merge(Overcommit::Configuration.new(
'PrePush' => {
'RSpec' => {
'include' => ['**/*_spec.rb'],
}
}
))
end

let(:context) { double('context') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
subject.stub(:applicable_files).and_return('spec/test_spec.rb')
end

it { should pass }

it {
expect(subject).to receive(:execute).with(['rspec'],
args: 'spec/test_spec.rb').and_return(result)

subject.run
}
end

context 'when rspec exits unsuccessfully' do
Expand Down

0 comments on commit b58b635

Please sign in to comment.