Commit f377c313 authored by Florian R. Hanke's avatar Florian R. Hanke

[Executable] Extract output handling to make it testable.

parent 179600e2
......@@ -48,34 +48,42 @@ module Pod
def self.execute_command(executable, command, raise_on_failure)
bin = `which #{executable}`.strip
raise Informative, "Unable to locate the executable `#{executable}`" if bin.empty?
output = StringIO.new
with_output(bin, command.map(&:to_s), output) do |full_command, status|
unless status.success?
if raise_on_failure
raise Informative, "#{full_command}\n\n#{output.string}"
else
UI.message("[!] Failed: #{full_command}".red)
end
end
end
output.string
end
require 'shellwords'
command = command.map(&:to_s)
private
#
#
def self.with_output bin, command, output
full_command = "#{bin} #{command.join(' ')}"
UI.message("$ #{full_command}") if Config.instance.verbose?
output = StringIO.new
indented = Indenter.new(output)
status = with_verbose do
spawn(bin, command) do |line|
indented << line
indented.write line
end
end
unless status.success?
if raise_on_failure
raise Informative, "#{full_command}\n\n#{output.string}"
else
UI.message("[!] Failed: #{full_command}".red)
end
end
yield full_command, status if block_given?
output.string
status
end
private
# Spawn a subprocess.
#
......@@ -150,8 +158,12 @@ module Pod
#
# @return [void]
#
def <<(value)
@io << "#{ indent }#{ value }"
def write(value)
@io.write "#{ indent }#{ value }"
end
def flush
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment