diff --git a/lib/overcommit/hook/pre_push/r_spec.rb b/lib/overcommit/hook/pre_push/r_spec.rb index a3b4474c..2af3c2cd 100644 --- a/lib/overcommit/hook/pre_push/r_spec.rb +++ b/lib/overcommit/hook/pre_push/r_spec.rb @@ -6,7 +6,7 @@ module Overcommit::Hook::PrePush # @see http://rspec.info/ class RSpec < Base def run - result = execute(command) + result = execute(command, live_output: config['live_output']) return :pass if result.success? output = result.stdout + result.stderr diff --git a/lib/overcommit/subprocess.rb b/lib/overcommit/subprocess.rb index 47713ff2..b5f89d62 100644 --- a/lib/overcommit/subprocess.rb +++ b/lib/overcommit/subprocess.rb @@ -31,7 +31,7 @@ def spawn(args, options = {}) process = ChildProcess.build(*args) - out, err = assign_output_streams(process) + out, err = assign_output_streams(process, options) process.duplex = true if options[:input] # Make stdin available if needed process.start @@ -48,10 +48,7 @@ def spawn(args, options = {}) end process.wait - err.rewind - out.rewind - - Result.new(process.exit_code, out.read, err.read) + build_result(process, out, err, options) end # Spawns a new process in the background using the given array of @@ -85,7 +82,12 @@ def win32_prepare_args(args) # @param process [ChildProcess] # @return [Array] - def assign_output_streams(process) + def assign_output_streams(process, options = {}) + if options[:live_output] + process.io.inherit! + return [] + end + %w[out err].map do |stream_name| ::Tempfile.new(stream_name).tap do |stream| stream.sync = true @@ -93,6 +95,15 @@ def assign_output_streams(process) end end end + + def build_result(process, out, err, options = {}) + return Result.new(process.exit_code, '', '') if options[:live_output] + + err.rewind + out.rewind + + Result.new(process.exit_code, out.read, err.read) + end 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..8ba28dfa 100644 --- a/spec/overcommit/hook/pre_push/r_spec_spec.rb +++ b/spec/overcommit/hook/pre_push/r_spec_spec.rb @@ -77,4 +77,47 @@ it { should fail_hook } end end + + context 'when live_output option is true' do + let(:config) do + Overcommit::ConfigurationLoader.default_configuration.merge( + Overcommit::Configuration.new( + 'PrePush' => { + 'RSpec' => { + 'live_output' => true + } + } + ) + ) + end + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(true) + end + + it 'calls execute with the live_output flag set' do + expect(subject).to receive(:execute).with(['rspec'], live_output: true).and_return(result) + expect(subject).to pass + end + + context do + let(:child_process) { double('child process') } + let(:io) { double('io') } + + it 'the child process inherits the io' do + io.stub(:inherit!) + child_process.stub(io: io, start: nil, wait: nil, exit_code: 0) + expect(ChildProcess).to receive(:build).with('rspec').and_return(child_process) + expect(subject).to pass + end + + it 'the child process inherits the io' do + io.stub(:inherit!) + child_process.stub(io: io, start: nil, wait: nil, exit_code: 1) + expect(ChildProcess).to receive(:build).with('rspec').and_return(child_process) + expect(subject).to fail_hook + end + end + end end