From c672168236fcc4e67330f6a09bb3d03c93b756e4 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Tue, 3 May 2011 11:36:33 -0400 Subject: [PATCH] Fixed GridIO#gets to handle the end of a file Neither a multi-character nor single-character separator search returned nil after hitting the EOF. --- lib/mongo/gridfs/grid_io.rb | 4 ++-- test/grid_io_test.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index 87cb178..4e2349f 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -235,7 +235,7 @@ module Mongo end end end - result + result.length > 0 ? result : nil else result = '' len = 0 @@ -244,7 +244,7 @@ module Mongo len += 1 break if char == separator || (length ? len >= length : false) end - result + result.length > 0 ? result : nil end end diff --git a/test/grid_io_test.rb b/test/grid_io_test.rb index 5e166b1..f29547b 100644 --- a/test/grid_io_test.rb +++ b/test/grid_io_test.rb @@ -69,6 +69,24 @@ class GridIOTest < Test::Unit::TestCase assert_equal 10, string.length end + should "read to the end of the file one line at a time" do + file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id}) + bytes = 0 + while string = file.gets + bytes += string.length + end + assert_equal 1_000_000, bytes + end + + should "read to the end of the file one multi-character separator at a time" do + file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id}) + bytes = 0 + while string = file.gets("45") + bytes += string.length + end + assert_equal 1_000_000, bytes + end + should "read to a given separator" do file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id}) string = file.gets("5")