-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
6 changed files
with
194 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters