From b85a565be0ba5e71dde482df9b23a71e0ed926aa Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Tue, 26 Jan 2010 15:35:58 -0500 Subject: [PATCH] added newline sugar to pipe library --- lib/hydra/pipe.rb | 5 ++++- test/test_pipe.rb | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/hydra/pipe.rb b/lib/hydra/pipe.rb index e6d2dcb..577ebaa 100644 --- a/lib/hydra/pipe.rb +++ b/lib/hydra/pipe.rb @@ -34,12 +34,15 @@ module Hydra #:nodoc: # Read a line from a pipe. It will have a trailing newline. def gets force_identification - @reader.gets + @reader.gets.chomp end # Write a line to a pipe. It must have a trailing newline. def write(str) force_identification + unless str =~ /\n$/ + str += "\n" + end begin @writer.write(str) return str diff --git a/test/test_pipe.rb b/test/test_pipe.rb index 9e19b97..6e91a41 100644 --- a/test/test_pipe.rb +++ b/test/test_pipe.rb @@ -8,15 +8,15 @@ class TestPipe < Test::Unit::TestCase should "be able to write messages" do Process.fork do @pipe.identify_as_child - assert_equal "Test Message\n", @pipe.gets - @pipe.write "Message Received\n" - @pipe.write "Second Message\n" + assert_equal "Test Message", @pipe.gets + @pipe.write "Message Received" + @pipe.write "Second Message" @pipe.close end @pipe.identify_as_parent - @pipe.write "Test Message\n" - assert_equal "Message Received\n", @pipe.gets - assert_equal "Second Message\n", @pipe.gets + @pipe.write "Test Message" + assert_equal "Message Received", @pipe.gets + assert_equal "Second Message", @pipe.gets assert_raise Hydra::PipeError::Broken do @pipe.write "anybody home?" end @@ -32,5 +32,13 @@ class TestPipe < Test::Unit::TestCase @pipe.gets 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