Skip to content

Commit f5b9b4d

Browse files
authored
Merge pull request #81 from jstuckey/add-limit-argument
Make file operation work with CSV library
2 parents 8af7582 + 7a059cd commit f5b9b4d

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,35 @@ def read(n=nil)
8181
# Reads up to the next instance of +sep_string+ in the stream, and
8282
# returns the bytes read (including +sep_string+). If +sep_string+ is
8383
# omitted, it defaults to +$/+. If EOF is encountered before any data
84-
# could be read, #gets will return +nil+.
85-
def gets(sep_string=$/)
86-
delim = if sep_string.length == 0
84+
# could be read, #gets will return +nil+. If the first argument is an
85+
# integer, or optional second argument is given, the returning string
86+
# would not be longer than the given value in bytes.
87+
def gets(sep_or_limit=$/, limit=Float::INFINITY)
88+
if sep_or_limit.is_a? Integer
89+
sep_string = $/
90+
lim = sep_or_limit
91+
else
92+
sep_string = sep_or_limit
93+
lim = limit
94+
end
95+
96+
delim = if sep_string && sep_string.length == 0
8797
"#{$/}#{$/}"
8898
else
8999
sep_string
90100
end
91101

92102
loop do
93-
at = @buffer.index(delim)
103+
at = @buffer.index(delim) if delim
94104
if at
95-
offset = at + delim.length
105+
offset = [at + delim.length, lim].min
96106
@pos += offset
97107
line, @buffer = @buffer[0,offset], @buffer[offset..-1]
98108
return line
109+
elsif lim < @buffer.length
110+
@pos += lim
111+
line, @buffer = @buffer[0,lim], @buffer[lim..-1]
112+
return line
99113
elsif !fill
100114
return nil if @buffer.empty?
101115
@pos += @buffer.length
@@ -107,12 +121,20 @@ def gets(sep_string=$/)
107121

108122
# Same as #gets, but raises EOFError if EOF is encountered before any
109123
# data could be read.
110-
def readline(sep_string=$/)
111-
line = gets(sep_string)
124+
def readline(sep_or_limit=$/, limit=Float::INFINITY)
125+
line = gets(sep_or_limit, limit)
112126
raise EOFError if line.nil?
113127
return line
114128
end
115129

130+
# Resets position to beginning of file
131+
def rewind
132+
@pos = 0
133+
@real_pos = 0
134+
@real_eof = false
135+
@buffer = ""
136+
end
137+
116138
# Writes the given data to the stream, incrementing the file position and
117139
# returning the number of bytes written.
118140
def write(data)

test/test_file.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ 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+
100+
def test_gets_with_integer_argument_should_read_number_of_bytes
101+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
102+
assert_equal "hello w", @file.gets(7)
103+
end
104+
105+
def test_gets_with_delimiter_and_limit_should_read_to_delimiter_if_less_than_limit
106+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
107+
assert_equal "hello w", @file.gets("w", 11)
108+
end
109+
110+
def test_gets_with_delimiter_and_limit_should_read_to_limit_if_less_than_delimiter
111+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
112+
assert_equal "hello", @file.gets("w", 5)
113+
end
114+
115+
def test_gets_when_no_such_delimiter_exists_in_stream_but_limit_provided_should_read_to_limit
116+
@sftp.expects(:read!).returns("hello world\ngoodbye world\n\nfarewell!\n")
117+
assert_equal "hello w", @file.gets("z", 7)
118+
end
119+
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+
94125
def test_gets_at_EOF_should_return_nil
95126
@sftp.expects(:read!).returns(nil)
96127
assert_nil @file.gets
@@ -102,6 +133,15 @@ def test_readline_should_raise_exception_on_EOF
102133
assert_raises(EOFError) { @file.readline }
103134
end
104135

136+
def test_rewind_should_reset_to_beginning_of_file
137+
@sftp.expects(:read!).times(2).returns("hello world", nil)
138+
@file.read
139+
assert @file.eof?
140+
@file.rewind
141+
assert !@file.eof?
142+
assert_equal 0, @file.pos
143+
end
144+
105145
def test_write_should_write_data_and_increment_pos_and_return_data_length
106146
@sftp.expects(:write!).with("handle", 0, "hello world")
107147
assert_equal 11, @file.write("hello world")

0 commit comments

Comments
 (0)