Skip to content

Commit eb1861b

Browse files
committed
Handle when delim isn't found but limit provided
A separator string could be provided that was not found in the stream. In that scenario, we were ignoring the limit argument. This change respects the limit argument even when the separator isn't found.
1 parent 5ac8c5d commit eb1861b

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def gets(sep_or_limit=$/, limit=Float::INFINITY)
106106
@pos += offset
107107
line, @buffer = @buffer[0,offset], @buffer[offset..-1]
108108
return line
109+
elsif lim < @buffer.length
110+
@pos += lim
111+
line, @buffer = @buffer[0,lim], @buffer[lim..-1]
112+
return line
109113
elsif !fill
110114
return nil if @buffer.empty?
111115
@pos += @buffer.length

test/test_file.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def test_gets_with_delimiter_and_limit_should_read_to_limit_if_less_than_delimit
106106
assert_equal "hello", @file.gets("w", 5)
107107
end
108108

109+
def test_gets_when_no_such_delimiter_exists_in_stream_but_limit_provided_should_read_to_limit
110+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
111+
assert_equal "hello w", @file.gets("z", 7)
112+
end
113+
109114
def test_gets_at_EOF_should_return_nil
110115
@sftp.expects(:read!).returns(nil)
111116
assert_nil @file.gets

0 commit comments

Comments
 (0)