From 29950b28af07b5b7b4215947519d5aaffe1d86f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Fri, 16 Mar 2018 11:07:02 +0100 Subject: fix to stop writing to stderr if closed --- lib/bundler/ui/shell.rb | 2 ++ spec/bundler/ui/shell_spec.rb | 9 +++++++++ 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 -- cgit v1.2.1