From b45996dada3e45a70ee901ff944e3aa8c146ebfa Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 14 Jun 2018 22:12:19 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Don=E2=80=99t=20use=20`ohai`=20for=20`Syste?= =?UTF-8?q?mCommand`=20output.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Homebrew/cask/lib/hbc/system_command.rb | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index e5a7d9202b..0c6f0eb367 100644 --- a/Library/Homebrew/cask/lib/hbc/system_command.rb +++ b/Library/Homebrew/cask/lib/hbc/system_command.rb @@ -25,11 +25,11 @@ module Hbc each_output_line do |type, line| case type when :stdout + puts line.chomp if print_stdout? processed_output[:stdout] << line - ohai line.chomp if print_stdout? when :stderr + $stderr.puts Formatter.error(line.chomp) if print_stderr? processed_output[:stderr] << line - ohai line.chomp if print_stderr? end end @@ -100,17 +100,23 @@ module Hbc def each_line_from(sources) loop do - readable_sources = IO.select(sources)[0] - readable_sources.delete_if(&:eof?).first(1).each do |source| - type = ((source == sources[0]) ? :stdout : :stderr) + readable_sources, = IO.select(sources) + + readable_sources = readable_sources.reject(&:eof?) + + break if readable_sources.empty? + + readable_sources.each do |source| begin - yield(type, source.readline_nonblock || "") + line = source.readline_nonblock || "" + type = (source == sources[0]) ? :stdout : :stderr + yield(type, line) rescue IO::WaitReadable, EOFError next end end - break if readable_sources.empty? end + sources.each(&:close_read) end From 502b393d66e8665fa207a218ca0bb50910a4c385 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 14 Jun 2018 22:45:07 +0200 Subject: [PATCH 2/4] Fix tests. --- .../Homebrew/test/cask/system_command_spec.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/test/cask/system_command_spec.rb b/Library/Homebrew/test/cask/system_command_spec.rb index ba5ea03fcb..6899d4fa65 100644 --- a/Library/Homebrew/test/cask/system_command_spec.rb +++ b/Library/Homebrew/test/cask/system_command_spec.rb @@ -66,10 +66,10 @@ describe Hbc::SystemCommand, :cask do describe "with default options" do it "echoes only STDERR" do - expected = [2, 4, 6].map { |i| "==> #{i}\n" }.join("") + expected = [2, 4, 6].map { |i| "#{i}\n" }.join expect { described_class.run(command, options) - }.to output(expected).to_stdout + }.to output(expected).to_stderr end include_examples("it returns '1 2 3 4 5 6'") @@ -80,12 +80,10 @@ describe Hbc::SystemCommand, :cask do options.merge!(print_stdout: true) end - it "echoes both STDOUT and STDERR" do - (1..6).each do |i| - expect { - described_class.run(command, options) - }.to output(/==> #{ i }/).to_stdout - end + it "echoes both STDOUT and STDERR", :focus do + expect { described_class.run(command, options) } + .to output("1\n3\n5\n").to_stdout + .and output("2\n4\n6\n").to_stderr end include_examples("it returns '1 2 3 4 5 6'") @@ -111,7 +109,7 @@ describe Hbc::SystemCommand, :cask do end it "echoes only STDOUT" do - expected = [1, 3, 5].map { |i| "==> #{i}\n" }.join("") + expected = [1, 3, 5].map { |i| "#{i}\n" }.join("") expect { described_class.run(command, options) }.to output(expected).to_stdout From 13d9305fad2dbb2668cf5d53db633b2849579ca1 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 14 Jun 2018 22:14:46 +0200 Subject: [PATCH 3/4] Cache `Tty.width`, since it is used heavily because of `ohai`. --- Library/Homebrew/utils/tty.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index 6e321b2f9b..eb6f02746f 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -6,10 +6,12 @@ module Tty end def width - width = `/bin/stty size 2>/dev/null`.split[1] - width = `/usr/bin/tput cols 2>/dev/null`.split[0] if width.to_i.zero? - width ||= 80 - width.to_i + @width ||= begin + width = `/bin/stty size 2>/dev/null`.split[1] + width = `/usr/bin/tput cols 2>/dev/null`.split[0] if width.to_i.zero? + width ||= 80 + width.to_i + end end def truncate(string) From 44f56a077369b5e32674ece240211071de6176ae Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 14 Jun 2018 22:48:37 +0200 Subject: [PATCH 4/4] Refactor `join("")` to `join`. --- Library/Homebrew/rubocops/extend/formula_cop.rb | 2 +- Library/Homebrew/test/cask/download_strategy_spec.rb | 2 +- Library/Homebrew/test/cask/dsl_spec.rb | 2 +- Library/Homebrew/test/cask/pkg_spec.rb | 2 +- Library/Homebrew/test/cask/system_command_spec.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 02bbb7c6d9..362a5134c0 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -460,7 +460,7 @@ module RuboCop when :str node.str_content when :dstr - node.each_child_node(:str).map(&:str_content).join("") + node.each_child_node(:str).map(&:str_content).join when :const node.const_name when :sym diff --git a/Library/Homebrew/test/cask/download_strategy_spec.rb b/Library/Homebrew/test/cask/download_strategy_spec.rb index ed8cd01ef6..3a69edaaf4 100644 --- a/Library/Homebrew/test/cask/download_strategy_spec.rb +++ b/Library/Homebrew/test/cask/download_strategy_spec.rb @@ -148,7 +148,7 @@ describe "download strategies", :cask do "DBw2KdR24q9t1wfjS9LUzelf5TWk6ojj8p9%2FHjl%2Fi%2FVCXN", "N4o1mW%2FMayy2tTY1qcC%2FTmqI1ulZS8SNuaSgr9Iys9oDF1%2", "BPK%2B4Sg==", - ].join("") + ].join end describe "#tarball_path" do diff --git a/Library/Homebrew/test/cask/dsl_spec.rb b/Library/Homebrew/test/cask/dsl_spec.rb index 67fcb027a8..2500a53fa3 100644 --- a/Library/Homebrew/test/cask/dsl_spec.rb +++ b/Library/Homebrew/test/cask/dsl_spec.rb @@ -20,7 +20,7 @@ describe Hbc::DSL, :cask do } it "prints a warning that it has encountered an unexpected method" do - expected = Regexp.compile(<<~EOS.lines.map(&:chomp).join("")) + expected = Regexp.compile(<<~EOS.lines.map(&:chomp).join) (?m) Warning: .* diff --git a/Library/Homebrew/test/cask/pkg_spec.rb b/Library/Homebrew/test/cask/pkg_spec.rb index f92d6854e2..6a7247058e 100644 --- a/Library/Homebrew/test/cask/pkg_spec.rb +++ b/Library/Homebrew/test/cask/pkg_spec.rb @@ -134,7 +134,7 @@ describe Hbc::Pkg, :cask do #{volume} paths - #{(pkg_files + pkg_directories).map { |f| "#{f}" }.join("")} + #{(pkg_files + pkg_directories).map { |f| "#{f}" }.join} diff --git a/Library/Homebrew/test/cask/system_command_spec.rb b/Library/Homebrew/test/cask/system_command_spec.rb index 6899d4fa65..6e7397931f 100644 --- a/Library/Homebrew/test/cask/system_command_spec.rb +++ b/Library/Homebrew/test/cask/system_command_spec.rb @@ -109,7 +109,7 @@ describe Hbc::SystemCommand, :cask do end it "echoes only STDOUT" do - expected = [1, 3, 5].map { |i| "#{i}\n" }.join("") + expected = [1, 3, 5].map { |i| "#{i}\n" }.join expect { described_class.run(command, options) }.to output(expected).to_stdout