Skip to content

Commit 0de5236

Browse files
committed
Handle nil separator passed to #gets
Ruby's IO documentation says that if nil is passed as the separator argument, the IO object should read to the end of the file or until the limit argument is reached.
1 parent eb1861b commit 0de5236

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ def gets(sep_or_limit=$/, limit=Float::INFINITY)
9393
lim = limit
9494
end
9595

96-
delim = if sep_string.length == 0
96+
delim = if sep_string && sep_string.length == 0
9797
"#{$/}#{$/}"
9898
else
9999
sep_string
100100
end
101101

102102
loop do
103-
at = @buffer.index(delim)
103+
at = @buffer.index(delim) if delim
104104
if at
105105
offset = [at + delim.length, lim].min
106106
@pos += offset

test/test_file.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def test_gets_when_no_such_delimiter_exists_in_stream_should_read_to_EOF
9191
assert @file.eof?
9292
end
9393

94+
def test_gets_when_nil_delimiter_should_fread_to_EOF
95+
@sftp.expects(:read!).times(2).returns("hello world\ngoodbye world\n\nfarewell!\n", nil)
96+
assert_equal "hello world\ngoodbye world\n\nfarewell!\n", @file.gets(nil)
97+
assert @file.eof?
98+
end
99+
94100
def test_gets_with_integer_argument_should_read_number_of_bytes
95101
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
96102
assert_equal "hello w", @file.gets(7)
@@ -111,6 +117,11 @@ def test_gets_when_no_such_delimiter_exists_in_stream_but_limit_provided_should_
111117
assert_equal "hello w", @file.gets("z", 7)
112118
end
113119

120+
def test_gets_when_nil_delimiter_and_limit_provided_should_read_to_limit
121+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
122+
assert_equal "hello w", @file.gets(nil, 7)
123+
end
124+
114125
def test_gets_at_EOF_should_return_nil
115126
@sftp.expects(:read!).returns(nil)
116127
assert_nil @file.gets

0 commit comments

Comments
 (0)