added newline sugar to pipe library

This commit is contained in:
Nick Gauthier 2010-01-26 15:35:58 -05:00
parent b491f767a0
commit b85a565be0
2 changed files with 18 additions and 7 deletions

View File

@ -34,12 +34,15 @@ module Hydra #:nodoc:
# Read a line from a pipe. It will have a trailing newline. # Read a line from a pipe. It will have a trailing newline.
def gets def gets
force_identification force_identification
@reader.gets @reader.gets.chomp
end end
# Write a line to a pipe. It must have a trailing newline. # Write a line to a pipe. It must have a trailing newline.
def write(str) def write(str)
force_identification force_identification
unless str =~ /\n$/
str += "\n"
end
begin begin
@writer.write(str) @writer.write(str)
return str return str

View File

@ -8,15 +8,15 @@ class TestPipe < Test::Unit::TestCase
should "be able to write messages" do should "be able to write messages" do
Process.fork do Process.fork do
@pipe.identify_as_child @pipe.identify_as_child
assert_equal "Test Message\n", @pipe.gets assert_equal "Test Message", @pipe.gets
@pipe.write "Message Received\n" @pipe.write "Message Received"
@pipe.write "Second Message\n" @pipe.write "Second Message"
@pipe.close @pipe.close
end end
@pipe.identify_as_parent @pipe.identify_as_parent
@pipe.write "Test Message\n" @pipe.write "Test Message"
assert_equal "Message Received\n", @pipe.gets assert_equal "Message Received", @pipe.gets
assert_equal "Second Message\n", @pipe.gets assert_equal "Second Message", @pipe.gets
assert_raise Hydra::PipeError::Broken do assert_raise Hydra::PipeError::Broken do
@pipe.write "anybody home?" @pipe.write "anybody home?"
end end
@ -32,5 +32,13 @@ class TestPipe < Test::Unit::TestCase
@pipe.gets @pipe.gets
end end
end end
should "handle newlines" do
Process.fork do
@pipe.identify_as_child
@pipe.write "Message\n"
end
@pipe.identify_as_parent
assert_equal "Message", @pipe.gets
end
end end
end end