Commit 9620191c authored by Boris Bügling's avatar Boris Bügling

Merge pull request #3626 from CocoaPods/seg-executable-readpartial

[Executable] Use readpartial instead of gets
parents a1a6919b 0fad2b4f
...@@ -64,7 +64,7 @@ module Pod ...@@ -64,7 +64,7 @@ module Pod
end end
status = popen3(bin, command, stdout, stderr) status = popen3(bin, command, stdout, stderr)
output = stdout.join("\n") + stderr.join("\n") output = stdout.join + stderr.join
unless status.success? unless status.success?
if raise_on_failure if raise_on_failure
raise Informative, "#{full_command}\n\n#{output}" raise Informative, "#{full_command}\n\n#{output}"
...@@ -80,19 +80,37 @@ module Pod ...@@ -80,19 +80,37 @@ module Pod
def self.popen3(bin, command, stdout, stderr) def self.popen3(bin, command, stdout, stderr)
require 'open3' require 'open3'
Open3.popen3(bin, *command) do |i, o, e, t| Open3.popen3(bin, *command) do |i, o, e, t|
Thread.new { while s = o.gets; stdout << s; end } reader(o, stdout)
Thread.new { while s = e.gets; stderr << s; end } reader(e, stderr)
i.close i.close
status = t.value status = t.value
o.flush
e.flush
sleep(0.1)
status status
end end
end end
def self.reader(input, output)
Thread.new do
buf = ''
begin
loop do
buf << input.readpartial(4096)
loop do
string, separator, buf = buf.partition(/[\r\n]/)
if separator.empty?
buf = string
break
end
output << (string << separator)
end
end
rescue EOFError
output << (buf << $/) unless buf.empty?
end
end
end
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
# Helper class that allows to write to an {IO} instance taking into account # Helper class that allows to write to an {IO} instance taking into account
...@@ -125,8 +143,7 @@ module Pod ...@@ -125,8 +143,7 @@ module Pod
# #
def <<(value) def <<(value)
super super
ensure io << "#{ indent }#{ value }" if io
@io << "#{ indent }#{ value }" if @io
end end
end end
end end
......
...@@ -29,5 +29,60 @@ module Pod ...@@ -29,5 +29,60 @@ module Pod
Executable.execute_command('ruby', cmd, true).should == "out\n" Executable.execute_command('ruby', cmd, true).should == "out\n"
end end
end end
it 'returns the right output' do
cmd = ['-e', <<-RB]
puts 'foo'
puts 'bar'
RB
Executable.execute_command('ruby', cmd, true).should == "foo\nbar\n"
end
it 'handles an EOFError' do
cmd = ['-e', <<-RB]
puts 'foo'
print 'bar'
RB
Executable.execute_command('ruby', cmd, true).should == "foo\nbar#{$/}"
end
it 'handles a large amount of output' do
cmd = ['-e', <<-RB]
puts File.read(#{__FILE__.inspect})
RB
Executable.execute_command('ruby', cmd, true).should == File.read(__FILE__)
end
it 'handles carriage returns' do
cmd = ['-e', <<-RB]
print "foo\\rbar\\nbaz\\r"
RB
Executable.execute_command('ruby', cmd, true).should == "foo\rbar\nbaz\r"
end
it 'prints the correct output to the console' do
io = ''
UI.indentation_level = 1
config.verbose = true
Executable::Indenter.any_instance.stubs(:io).returns(io)
cmd = ['-e', <<-RB]
3.times { |i| puts i }
RB
Executable.execute_command('ruby', cmd, true)
io.should == " 0\n 1\n 2\n"
end
describe Executable::Indenter do
it 'indents any appended strings' do
UI.indentation_level = 4
io = StringIO.new
indenter = Executable::Indenter.new(io)
indenter << 'hello'
io.string.should == ' hello'
end
end
end 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