diff options
author | The Bundler Bot <bot@bundler.io> | 2018-03-17 18:08:24 +0000 |
---|---|---|
committer | The Bundler Bot <bot@bundler.io> | 2018-03-17 18:08:24 +0000 |
commit | 6a7af6b4016ad2c659a66cff86074f7dd52b3cdf (patch) | |
tree | 6f034a4fcc3076be95186865d9fcfcf938112e3f | |
parent | 7f4c1a81b7b92e234fce0e3592d27c396d2451ac (diff) | |
parent | 29950b28af07b5b7b4215947519d5aaffe1d86f9 (diff) | |
download | bundler-6a7af6b4016ad2c659a66cff86074f7dd52b3cdf.tar.gz |
Auto merge of #6349 - shime:check-if-stderr-is-closed-before-reporting-errors, r=segiddins
Fix to check if stderr was closed before writing to it
### What was the end-user problem that led to this PR?
See https://github.com/bundler/bundler/issues/6190
### What was your diagnosis of the problem?
The error is happening because we're not checking if stderr is closed prior to writing to it.
### What is your fix for the problem, implemented in this PR?
My fix is to add a check to determine if stderr was closed.
### Why did you choose this fix out of the possible options?
I chose to fix it here for a start and discuss with others if more checks were needed. Maybe we need to check if stderr is closed for all references of `$stderr.puts` and replace `abort` with `$stderr.puts` and `exit(1)`?
-rw-r--r-- | lib/bundler/ui/shell.rb | 2 | ||||
-rw-r--r-- | spec/bundler/ui/shell_spec.rb | 9 | ||||
-rw-r--r-- | spec/support/streams.rb | 12 |
3 files changed, 19 insertions, 4 deletions
diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb index fdf073ffb8..16e3d15713 100644 --- a/lib/bundler/ui/shell.rb +++ b/lib/bundler/ui/shell.rb @@ -109,6 +109,8 @@ module Bundler end def tell_err(message, color = nil, newline = nil) + return if @shell.send(:stderr).closed? + newline ||= message.to_s !~ /( |\t)\Z/ message = word_wrap(message) if newline.is_a?(Hash) && newline[:wrap] diff --git a/spec/bundler/ui/shell_spec.rb b/spec/bundler/ui/shell_spec.rb index 9a47a3572f..951a446aff 100644 --- a/spec/bundler/ui/shell_spec.rb +++ b/spec/bundler/ui/shell_spec.rb @@ -59,6 +59,15 @@ RSpec.describe Bundler::UI::Shell do it "prints to stderr" do expect { subject.error("error!!!") }.to output("error!!!\n").to_stderr end + + context "when stderr is closed" do + it "doesn't report anything" do + output = capture(:stderr, :closed => true) do + subject.error("Something went wrong") + end + expect(output).to_not eq("Something went wrong\n") + end + end end end end diff --git a/spec/support/streams.rb b/spec/support/streams.rb index b9e1d292dc..a947eebf6f 100644 --- a/spec/support/streams.rb +++ b/spec/support/streams.rb @@ -2,14 +2,18 @@ require "stringio" -def capture(*streams) - streams.map!(&:to_s) +def capture(*args) + opts = args.pop if args.last.is_a?(Hash) + opts ||= {} + + args.map!(&:to_s) begin result = StringIO.new - streams.each {|stream| eval "$#{stream} = result" } + result.close if opts[:closed] + args.each {|stream| eval "$#{stream} = result" } yield ensure - streams.each {|stream| eval("$#{stream} = #{stream.upcase}") } + args.each {|stream| eval("$#{stream} = #{stream.upcase}") } end result.string end |