diff --git a/config/default.yml b/config/default.yml index e0fc3910..70ae0b2d 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' diff --git a/lib/overcommit/hook/pre_commit/r_spec.rb b/lib/overcommit/hook/pre_commit/r_spec.rb new file mode 100644 index 00000000..26bbc0a8 --- /dev/null +++ b/lib/overcommit/hook/pre_commit/r_spec.rb @@ -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 diff --git a/lib/overcommit/hook/pre_push/r_spec.rb b/lib/overcommit/hook/pre_push/r_spec.rb index a3b4474c..7ce3df29 100644 --- a/lib/overcommit/hook/pre_push/r_spec.rb +++ b/lib/overcommit/hook/pre_push/r_spec.rb @@ -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 diff --git a/lib/overcommit/hook/shared/r_spec.rb b/lib/overcommit/hook/shared/r_spec.rb new file mode 100644 index 00000000..9b41e8e1 --- /dev/null +++ b/lib/overcommit/hook/shared/r_spec.rb @@ -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 diff --git a/spec/overcommit/hook/pre_commit/r_spec_spec.rb b/spec/overcommit/hook/pre_commit/r_spec_spec.rb new file mode 100644 index 00000000..7fdff3b3 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/r_spec_spec.rb @@ -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 `' + 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 `
' + 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 ' + + 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 ' + + 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 diff --git a/spec/overcommit/hook/pre_push/r_spec_spec.rb b/spec/overcommit/hook/pre_push/r_spec_spec.rb index 26f6ea0f..1efc56d7 100644 --- a/spec/overcommit/hook/pre_push/r_spec_spec.rb +++ b/spec/overcommit/hook/pre_push/r_spec_spec.rb @@ -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