Refactor ErrorDuringExecution.
This commit is contained in:
parent
bb29150096
commit
2452b27866
@ -53,31 +53,6 @@ module Hbc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CaskCommandFailedError < CaskError
|
|
||||||
def initialize(cmd, stdout, stderr, status)
|
|
||||||
@cmd = cmd
|
|
||||||
@stdout = stdout
|
|
||||||
@stderr = stderr
|
|
||||||
@status = status
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
s = "Command failed to execute!\n"
|
|
||||||
s.concat("\n")
|
|
||||||
s.concat("==> Failed command:\n")
|
|
||||||
s.concat(@cmd.join(" ")).concat("\n")
|
|
||||||
s.concat("\n")
|
|
||||||
s.concat("==> Standard Output of failed command:\n")
|
|
||||||
s.concat(@stdout).concat("\n")
|
|
||||||
s.concat("\n")
|
|
||||||
s.concat("==> Standard Error of failed command:\n")
|
|
||||||
s.concat(@stderr).concat("\n")
|
|
||||||
s.concat("\n")
|
|
||||||
s.concat("==> Exit status of failed command:\n")
|
|
||||||
s.concat(@status.inspect).concat("\n")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class CaskX11DependencyError < AbstractCaskErrorWithToken
|
class CaskX11DependencyError < AbstractCaskErrorWithToken
|
||||||
def to_s
|
def to_s
|
||||||
<<~EOS
|
<<~EOS
|
||||||
|
|||||||
@ -84,7 +84,10 @@ module Hbc
|
|||||||
|
|
||||||
def assert_success
|
def assert_success
|
||||||
return if processed_status&.success?
|
return if processed_status&.success?
|
||||||
raise CaskCommandFailedError.new(command, processed_output[:stdout], processed_output[:stderr], processed_status)
|
raise ErrorDuringExecution.new(command,
|
||||||
|
stdout: processed_output[:stdout],
|
||||||
|
stderr: processed_output[:stderr],
|
||||||
|
status: processed_status)
|
||||||
end
|
end
|
||||||
|
|
||||||
def expanded_args
|
def expanded_args
|
||||||
|
|||||||
@ -69,7 +69,7 @@ class AbstractDownloadStrategy
|
|||||||
|
|
||||||
def safe_system(*args)
|
def safe_system(*args)
|
||||||
if @shutup
|
if @shutup
|
||||||
quiet_system(*args) || raise(ErrorDuringExecution.new(args.shift, args))
|
quiet_system(*args) || raise(ErrorDuringExecution.new(args, status: $CHILD_STATUS))
|
||||||
else
|
else
|
||||||
super(*args)
|
super(*args)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
require "shellwords"
|
||||||
|
|
||||||
class UsageError < RuntimeError
|
class UsageError < RuntimeError
|
||||||
attr_reader :reason
|
attr_reader :reason
|
||||||
|
|
||||||
@ -524,9 +526,22 @@ end
|
|||||||
|
|
||||||
# raised by safe_system in utils.rb
|
# raised by safe_system in utils.rb
|
||||||
class ErrorDuringExecution < RuntimeError
|
class ErrorDuringExecution < RuntimeError
|
||||||
def initialize(cmd, args = [])
|
def initialize(cmd, status:, stdout: nil, stderr: nil)
|
||||||
args = args.map { |a| a.to_s.gsub " ", "\\ " }.join(" ")
|
s = "Failure while executing; `#{cmd.shelljoin.gsub(/\\=/, "=")}` exited with #{status.exitstatus}."
|
||||||
super "Failure while executing: #{cmd} #{args}"
|
|
||||||
|
if stdout
|
||||||
|
s << "==> Standard Output of failed command:\n"
|
||||||
|
s << stdout
|
||||||
|
s << "\n" unless stdout.end_with?("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
if stderr
|
||||||
|
s << "==> Standard Error of failed command:\n"
|
||||||
|
s << stderr
|
||||||
|
s << "\n" unless stderr.end_with?("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
super s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,9 @@ class Keg
|
|||||||
# patchelf requires that the ELF file have a .dynstr section.
|
# patchelf requires that the ELF file have a .dynstr section.
|
||||||
# Skip ELF files that do not have a .dynstr section.
|
# Skip ELF files that do not have a .dynstr section.
|
||||||
return if ["cannot find section .dynstr", "strange: no string table"].include?(old_rpath)
|
return if ["cannot find section .dynstr", "strange: no string table"].include?(old_rpath)
|
||||||
raise ErrorDuringExecution, "#{cmd_rpath}\n#{old_rpath}" unless $CHILD_STATUS.success?
|
unless $CHILD_STATUS.success?
|
||||||
|
raise ErrorDuringExecution.new(cmd_rpath, stdout: old_rpath, status: $CHILD_STATUS)
|
||||||
|
end
|
||||||
|
|
||||||
rpath = old_rpath
|
rpath = old_rpath
|
||||||
.split(":")
|
.split(":")
|
||||||
|
|||||||
@ -118,12 +118,10 @@ module ELFShim
|
|||||||
patchelf = DevelopmentTools.locate "patchelf"
|
patchelf = DevelopmentTools.locate "patchelf"
|
||||||
if path.dylib?
|
if path.dylib?
|
||||||
command = [patchelf, "--print-soname", path.expand_path.to_s]
|
command = [patchelf, "--print-soname", path.expand_path.to_s]
|
||||||
soname = Utils.popen_read(*command).chomp
|
soname = Utils.safe_popen_read(*command).chomp
|
||||||
raise ErrorDuringExecution, command unless $CHILD_STATUS.success?
|
|
||||||
end
|
end
|
||||||
command = [patchelf, "--print-needed", path.expand_path.to_s]
|
command = [patchelf, "--print-needed", path.expand_path.to_s]
|
||||||
needed = Utils.popen_read(*command).split("\n")
|
needed = Utils.safe_popen_read(*command).split("\n")
|
||||||
raise ErrorDuringExecution, command unless $CHILD_STATUS.success?
|
|
||||||
[soname, needed]
|
[soname, needed]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -131,8 +129,7 @@ module ELFShim
|
|||||||
soname = nil
|
soname = nil
|
||||||
needed = []
|
needed = []
|
||||||
command = ["readelf", "-d", path.expand_path.to_s]
|
command = ["readelf", "-d", path.expand_path.to_s]
|
||||||
lines = Utils.popen_read(*command).split("\n")
|
lines = Utils.safe_popen_read(*command).split("\n")
|
||||||
raise ErrorDuringExecution, command unless $CHILD_STATUS.success?
|
|
||||||
lines.each do |s|
|
lines.each do |s|
|
||||||
filename = s[/\[(.*)\]/, 1]
|
filename = s[/\[(.*)\]/, 1]
|
||||||
next if filename.nil?
|
next if filename.nil?
|
||||||
|
|||||||
@ -67,8 +67,7 @@ class EmbeddedPatch
|
|||||||
def apply
|
def apply
|
||||||
data = contents.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX)
|
data = contents.gsub("HOMEBREW_PREFIX", HOMEBREW_PREFIX)
|
||||||
args = %W[-g 0 -f -#{strip}]
|
args = %W[-g 0 -f -#{strip}]
|
||||||
Utils.popen_write("patch", *args) { |p| p.write(data) }
|
Utils.safe_popen_write("patch", *args) { |p| p.write(data) }
|
||||||
raise ErrorDuringExecution.new("patch", args) unless $CHILD_STATUS.success?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
|
|||||||
@ -69,7 +69,7 @@ describe Hbc::SystemCommand, :cask do
|
|||||||
it "throws an error" do
|
it "throws an error" do
|
||||||
expect {
|
expect {
|
||||||
described_class.run!(command)
|
described_class.run!(command)
|
||||||
}.to raise_error(Hbc::CaskCommandFailedError)
|
}.to raise_error(ErrorDuringExecution)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -183,9 +183,10 @@ describe CurlDownloadStrategyError do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe ErrorDuringExecution do
|
describe ErrorDuringExecution do
|
||||||
subject { described_class.new("badprg", %w[arg1 arg2]) }
|
subject { described_class.new(["badprg", "arg1", "arg2"], status: status) }
|
||||||
|
let(:status) { instance_double(Process::Status, exitstatus: 17) }
|
||||||
|
|
||||||
its(:to_s) { is_expected.to eq("Failure while executing: badprg arg1 arg2") }
|
its(:to_s) { is_expected.to eq("Failure while executing; `badprg arg1 arg2` exited with 17.") }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ChecksumMismatchError do
|
describe ChecksumMismatchError do
|
||||||
|
|||||||
@ -296,7 +296,7 @@ end
|
|||||||
|
|
||||||
# Kernel.system but with exceptions
|
# Kernel.system but with exceptions
|
||||||
def safe_system(cmd, *args, **options)
|
def safe_system(cmd, *args, **options)
|
||||||
Homebrew.system(cmd, *args, **options) || raise(ErrorDuringExecution.new(cmd, args))
|
Homebrew.system(cmd, *args, **options) || raise(ErrorDuringExecution.new([cmd, *args], status: $CHILD_STATUS))
|
||||||
end
|
end
|
||||||
|
|
||||||
# prints no output
|
# prints no output
|
||||||
|
|||||||
@ -5,7 +5,7 @@ module Utils
|
|||||||
|
|
||||||
def self.safe_popen_read(*args, **options, &block)
|
def self.safe_popen_read(*args, **options, &block)
|
||||||
output = popen_read(*args, **options, &block)
|
output = popen_read(*args, **options, &block)
|
||||||
raise ErrorDuringExecution, args unless $CHILD_STATUS.success?
|
raise ErrorDuringExecution(args, stdout: output, status: $CHILD_STATUS) unless $CHILD_STATUS.success?
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -14,8 +14,8 @@ module Utils
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.safe_popen_write(*args, **options, &block)
|
def self.safe_popen_write(*args, **options, &block)
|
||||||
output = popen_write(args, **options, &block)
|
output = popen_write(*args, **options, &block)
|
||||||
raise ErrorDuringExecution, args unless $CHILD_STATUS.success?
|
raise ErrorDuringExecution(args, stdout: output, status: $CHILD_STATUS) unless $CHILD_STATUS.success?
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user