|
| 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 |
0 commit comments