From c1909e80b423264796b4ae4dd5a6232de99fdd18 Mon Sep 17 00:00:00 2001 From: nandahkrishna Date: Thu, 17 Sep 2020 04:18:13 +0530 Subject: [PATCH] utils/tty: fix TTY handling for stderr --- Library/Homebrew/tap.rb | 16 +++++++++------- Library/Homebrew/test/cmd/tap_spec.rb | 3 +-- Library/Homebrew/test/cmd/untap_spec.rb | 4 ++-- Library/Homebrew/utils.rb | 8 ++++++-- Library/Homebrew/utils/tty.rb | 13 ++++++++++++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 32b68ce230..663fc28b9c 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -256,7 +256,7 @@ class Tap return if !full_clone || !shallow? end - ohai "Unshallowing #{name}" unless quiet + $stderr.ohai "Unshallowing #{name}" unless quiet args = %w[fetch --unshallow] args << "-q" if quiet path.cd { safe_system "git", *args } @@ -265,7 +265,7 @@ class Tap clear_cache - ohai "Tapping #{name}" unless quiet + $stderr.ohai "Tapping #{name}" unless quiet args = %W[clone #{requested_remote} #{path}] args << "--depth=1" unless full_clone args << "-q" if quiet @@ -291,7 +291,7 @@ class Tap link_completions_and_manpages formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ") - puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet + $stderr.puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet CacheStoreDatabase.use(:descriptions) do |db| DescriptionCacheStore.new(db) .update_from_formula_names!(formula_names) @@ -301,7 +301,7 @@ class Tap return unless private? return if quiet - puts <<~EOS + $stderr.puts <<~EOS It looks like you tapped a private repository. To avoid entering your credentials each time you update, you can use git HTTP credential caching or issue the following command: @@ -321,7 +321,7 @@ class Tap require "descriptions" raise TapUnavailableError, name unless installed? - puts "Untapping #{name}..." + $stderr.puts "Untapping #{name}..." abv = path.abv formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ") @@ -335,7 +335,7 @@ class Tap Utils::Link.unlink_completions(path) path.rmtree path.parent.rmdir_if_possible - puts "Untapped#{formatted_contents} (#{abv})." + $stderr.puts "Untapped#{formatted_contents} (#{abv})." Commands.rebuild_commands_completion_list clear_cache @@ -625,7 +625,9 @@ class CoreTap < Tap def install(full_clone: true, quiet: false, clone_target: nil, force_auto_update: nil) remote = Homebrew::EnvConfig.core_git_remote - puts "HOMEBREW_CORE_GIT_REMOTE set: using #{remote} for Homebrew/core Git remote URL." if remote != default_remote + if remote != default_remote + $stderr.puts "HOMEBREW_CORE_GIT_REMOTE set: using #{remote} for Homebrew/core Git remote URL." + end super(full_clone: full_clone, quiet: quiet, clone_target: remote, force_auto_update: force_auto_update) end diff --git a/Library/Homebrew/test/cmd/tap_spec.rb b/Library/Homebrew/test/cmd/tap_spec.rb index 69f43ae84a..c90155e085 100644 --- a/Library/Homebrew/test/cmd/tap_spec.rb +++ b/Library/Homebrew/test/cmd/tap_spec.rb @@ -11,8 +11,7 @@ describe "brew tap", :integration_test do path = setup_test_tap expect { brew "tap", "--force-auto-update", "--full", "homebrew/bar", path/".git" } - .to output(/Tapped/).to_stdout - .and output(/Cloning/).to_stderr + .to output(/Tapped/).to_stderr .and be_a_success end end diff --git a/Library/Homebrew/test/cmd/untap_spec.rb b/Library/Homebrew/test/cmd/untap_spec.rb index 9e2568597f..7a33683d46 100644 --- a/Library/Homebrew/test/cmd/untap_spec.rb +++ b/Library/Homebrew/test/cmd/untap_spec.rb @@ -11,8 +11,8 @@ describe "brew untap", :integration_test do setup_test_tap expect { brew "untap", "homebrew/foo" } - .to output(/Untapped/).to_stdout - .and not_to_output.to_stderr + .to output(/Untapped/).to_stderr + .and not_to_output.to_stdout .and be_a_success end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 42e4c6667a..e56867e16f 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -131,11 +131,15 @@ module Kernel # Print a warning (do this rarely) def opoo(message) - $stderr.puts Formatter.warning(message, label: "Warning") + Tty.with($stderr) do |stderr| + stderr.puts Formatter.warning(message, label: "Warning") + end end def onoe(message) - $stderr.puts Formatter.error(message, label: "Error") + Tty.with($stderr) do |stderr| + stderr.puts Formatter.error(message, label: "Error") + end end def ofail(error) diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index a09f2f375b..b1d97a35bf 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -4,8 +4,19 @@ # # @api private module Tty + @stream = $stdout + module_function + def with(stream) + previous_stream = @stream + @stream = stream + + yield stream + ensure + @stream = previous_stream + end + def strip_ansi(string) string.gsub(/\033\[\d+(;\d+)*m/, "") end @@ -78,6 +89,6 @@ module Tty return false if Homebrew::EnvConfig.no_color? return true if Homebrew::EnvConfig.color? - $stdout.tty? + @stream.tty? end end