Merge pull request #4808 from reitermarkus/remove-popen-read

Remove some `#popen_read`s.
This commit is contained in:
Markus Reiter 2018-09-04 06:00:49 +02:00 committed by GitHub
commit 0d46461aa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 58 additions and 44 deletions

View File

@ -6,12 +6,11 @@ class SystemConfig
# java_home doesn't exist on all macOSs; it might be missing on older versions. # java_home doesn't exist on all macOSs; it might be missing on older versions.
return "N/A" unless File.executable? "/usr/libexec/java_home" return "N/A" unless File.executable? "/usr/libexec/java_home"
java_xml = Utils.popen_read("/usr/libexec/java_home", "--xml", "--failfast", err: :close) out, _, status = system_command("/usr/libexec/java_home", args: ["--xml", "--failfast"], print_stderr: false)
return "N/A" unless $CHILD_STATUS.success? return "N/A" unless status.success?
javas = [] javas = []
REXML::XPath.each( xml = REXML::Document.new(out)
REXML::Document.new(java_xml), "//key[text()='JVMVersion']/following-sibling::string" REXML::XPath.each(xml, "//key[text()='JVMVersion']/following-sibling::string") do |item|
) do |item|
javas << item.text javas << item.text
end end
javas.uniq.join(", ") javas.uniq.join(", ")

View File

@ -73,13 +73,12 @@ module Readall
private private
def syntax_errors_or_warnings?(rb) def syntax_errors_or_warnings?(rb)
# Retrieve messages about syntax errors/warnings printed to `$stderr`, but # Retrieve messages about syntax errors/warnings printed to `$stderr`.
# discard a `Syntax OK` printed to `$stdout` (in absence of syntax errors). _, err, status = system_command(RUBY_PATH, args: ["-c", "-w", rb], print_stderr: false)
messages = Utils.popen_read("#{RUBY_PATH} -c -w #{rb} 2>&1 >/dev/null")
# Ignore unnecessary warning about named capture conflicts. # Ignore unnecessary warning about named capture conflicts.
# See https://bugs.ruby-lang.org/issues/12359. # See https://bugs.ruby-lang.org/issues/12359.
messages = messages.lines messages = err.lines
.grep_v(/named capture conflicts a local variable/) .grep_v(/named capture conflicts a local variable/)
.join .join
@ -87,7 +86,7 @@ module Readall
# Only syntax errors result in a non-zero status code. To detect syntax # Only syntax errors result in a non-zero status code. To detect syntax
# warnings we also need to inspect the output to `$stderr`. # warnings we also need to inspect the output to `$stderr`.
!$CHILD_STATUS.success? || !messages.chomp.empty? !status.success? || !messages.chomp.empty?
end end
end end
end end

View File

@ -122,7 +122,7 @@ class JavaRequirement < Requirement
end end
def satisfies_version(java) def satisfies_version(java)
java_version_s = Utils.popen_read(java, "-version", err: :out)[/\d+.\d/] java_version_s = system_command(java, args: ["-version"], print_stderr: false).stderr[/\d+.\d/]
return false unless java_version_s return false unless java_version_s
java_version = Version.create(java_version_s) java_version = Version.create(java_version_s)
needed_version = Version.create(version_without_plus) needed_version = Version.create(version_without_plus)

View File

@ -30,16 +30,16 @@ class SystemCommand
def run! def run!
puts command.shelljoin.gsub(/\\=/, "=") if verbose? || ARGV.debug? puts command.shelljoin.gsub(/\\=/, "=") if verbose? || ARGV.debug?
@merged_output = [] @output = []
each_output_line do |type, line| each_output_line do |type, line|
case type case type
when :stdout when :stdout
$stdout << line if print_stdout? $stdout << line if print_stdout?
@merged_output << [:stdout, line] @output << [:stdout, line]
when :stderr when :stderr
$stderr << line if print_stderr? $stderr << line if print_stderr?
@merged_output << [:stderr, line] @output << [:stderr, line]
end end
end end
@ -100,7 +100,7 @@ class SystemCommand
return if @status.success? return if @status.success?
raise ErrorDuringExecution.new(command, raise ErrorDuringExecution.new(command,
status: @status, status: @status,
output: @merged_output) output: @output)
end end
def expanded_args def expanded_args
@ -128,7 +128,7 @@ class SystemCommand
@status = raw_wait_thr.value @status = raw_wait_thr.value
rescue SystemCallError => e rescue SystemCallError => e
@status = $CHILD_STATUS @status = $CHILD_STATUS
@merged_output << [:stderr, e.message] @output << [:stderr, e.message]
end end
def write_input_to(raw_stdin) def write_input_to(raw_stdin)
@ -158,24 +158,31 @@ class SystemCommand
end end
def result def result
output = @merged_output.each_with_object(stdout: "", stderr: "") do |(type, line), hash| Result.new(command, @output, @status)
hash[type] << line
end
Result.new(command, output[:stdout], output[:stderr], @status)
end end
class Result class Result
attr_accessor :command, :stdout, :stderr, :status, :exit_status attr_accessor :command, :status, :exit_status
def initialize(command, stdout, stderr, status) def initialize(command, output, status)
@command = command @command = command
@stdout = stdout @output = output
@stderr = stderr
@status = status @status = status
@exit_status = status.exitstatus @exit_status = status.exitstatus
end end
def stdout
@stdout ||= @output.select { |type,| type == :stdout }
.map { |_, line| line }
.join
end
def stderr
@stderr ||= @output.select { |type,| type == :stderr }
.map { |_, line| line }
.join
end
def success? def success?
@exit_status.zero? @exit_status.zero?
end end

View File

@ -79,9 +79,9 @@ class SystemConfig
def describe_java def describe_java
return "N/A" unless which "java" return "N/A" unless which "java"
java_version = Utils.popen_read("java", "-version") _, err, status = system_command("java", args: ["-version"], print_stderr: false)
return "N/A" unless $CHILD_STATUS.success? return "N/A" unless status.success?
java_version[/java version "([\d\._]+)"/, 1] || "N/A" err[/java version "([\d\._]+)"/, 1] || "N/A"
end end
def describe_git def describe_git
@ -90,13 +90,14 @@ class SystemConfig
end end
def describe_curl def describe_curl
curl_version_output = Utils.popen_read("#{curl_executable} --version", err: :close) out, = system_command(curl_executable, args: ["--version"])
curl_version_output =~ /^curl ([\d\.]+)/
curl_version = Regexp.last_match(1) if /^curl (?<curl_version>[\d\.]+)/ =~ out
"#{curl_version} => #{curl_executable}" "#{curl_version} => #{curl_executable}"
rescue else
"N/A" "N/A"
end end
end
def dump_verbose_config(f = $stdout) def dump_verbose_config(f = $stdout)
f.puts "HOMEBREW_VERSION: #{HOMEBREW_VERSION}" f.puts "HOMEBREW_VERSION: #{HOMEBREW_VERSION}"

View File

@ -150,7 +150,7 @@ describe Hbc::Pkg, :cask do
"/usr/sbin/pkgutil", "/usr/sbin/pkgutil",
args: ["--pkg-info-plist", pkg_id], args: ["--pkg-info-plist", pkg_id],
).and_return( ).and_return(
SystemCommand::Result.new(nil, pkg_info_plist, nil, instance_double(Process::Status, exitstatus: 0)), SystemCommand::Result.new(nil, [[:stdout, pkg_info_plist]], instance_double(Process::Status, exitstatus: 0)),
) )
info = pkg.info info = pkg.info

View File

@ -55,7 +55,7 @@ describe JavaRequirement do
def setup_java_with_version(version) def setup_java_with_version(version)
IO.write java, <<~SH IO.write java, <<~SH
#!/bin/sh #!/bin/sh
echo 'java version "#{version}"' echo 'java version "#{version}"' 1>&2
SH SH
FileUtils.chmod "+x", java FileUtils.chmod "+x", java
end end

View File

@ -52,7 +52,7 @@ class FakeSystemCommand
if response.respond_to?(:call) if response.respond_to?(:call)
response.call(command_string, options) response.call(command_string, options)
else else
SystemCommand::Result.new(command, response, "", OpenStruct.new(exitstatus: 0)) SystemCommand::Result.new(command, [[:stdout, response]], OpenStruct.new(exitstatus: 0))
end end
end end

View File

@ -2,8 +2,14 @@ require "system_command"
describe SystemCommand::Result do describe SystemCommand::Result do
describe "#to_ary" do describe "#to_ary" do
let(:output) {
[
[:stdout, "output"],
[:stderr, "error"],
]
}
subject(:result) { subject(:result) {
described_class.new([], "output", "error", instance_double(Process::Status, exitstatus: 0, success?: true)) described_class.new([], output, instance_double(Process::Status, exitstatus: 0, success?: true))
} }
it "can be destructed like `Open3.capture3`" do it "can be destructed like `Open3.capture3`" do
@ -16,7 +22,9 @@ describe SystemCommand::Result do
end end
describe "#plist" do describe "#plist" do
subject { described_class.new(command, stdout, "", instance_double(Process::Status, exitstatus: 0)).plist } subject {
described_class.new(command, [[:stdout, stdout]], instance_double(Process::Status, exitstatus: 0)).plist
}
let(:command) { ["true"] } let(:command) { ["true"] }
let(:garbage) { let(:garbage) {