diff options
author | The Bundler Bot <bot@bundler.io> | 2018-03-17 18:08:24 +0000 |
---|---|---|
committer | Colby Swandale <me@colby.fyi> | 2018-04-20 10:27:59 +1000 |
commit | 36492998846af615110b2fbe97329d41d66d8e4e (patch) | |
tree | c3b1311b670b7c34d653ccf0560dede113a82b73 | |
parent | 295f928098303d71c0314176f32a7d995b67e3c3 (diff) | |
download | bundler-36492998846af615110b2fbe97329d41d66d8e4e.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
See https://github.com/bundler/bundler/issues/6190
The error is happening because we're not checking if stderr is closed prior to writing to it.
My fix is to add a check to determine if stderr was closed.
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)`?
(cherry picked from commit 6a7af6b4016ad2c659a66cff86074f7dd52b3cdf)
-rw-r--r-- | lib/bundler/ui/shell.rb | 4 | ||||
-rw-r--r-- | spec/bundler/ui/shell_spec.rb | 9 | ||||
-rw-r--r-- | spec/support/streams.rb | 12 |
3 files changed, 20 insertions, 5 deletions
diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb index 3b3b6bfb53..16e3d15713 100644 --- a/lib/bundler/ui/shell.rb +++ b/lib/bundler/ui/shell.rb @@ -109,7 +109,9 @@ module Bundler end def tell_err(message, color = nil, newline = nil) - newline = message.to_s !~ /( |\t)\Z/ unless newline + return if @shell.send(:stderr).closed? + + newline ||= message.to_s !~ /( |\t)\Z/ message = word_wrap(message) if newline.is_a?(Hash) && newline[:wrap] color = nil if color && !$stderr.tty? 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 |