Skip to content

Commit 8af7582

Browse files
authored
Merge pull request #83 from joshuamcginnis/streaming_support
Add #size and #rewind to Net::SFTP::Operations::File
2 parents 5ad0ab7 + 6e28aa3 commit 8af7582

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ def print(*items)
131131
nil
132132
end
133133

134+
def size
135+
stat.size
136+
end
137+
138+
def rewind
139+
self.pos = 0
140+
end
141+
134142
# Writes each argument to the stream, appending a newline to any item
135143
# that does not already end in a newline. Array arguments are flattened.
136144
def puts(*items)

test/test_file.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,20 @@ def test_stat_should_return_attributes_object_for_handle
156156
@sftp.expects(:fstat!).with("handle").returns(stat)
157157
assert_equal stat, @file.stat
158158
end
159-
end
159+
160+
def test_size_should_return_size_from_stat
161+
stat = stub(size: 1024)
162+
@sftp.expects(:fstat!).with("handle").returns(stat)
163+
assert_equal 1024, @file.size
164+
end
165+
166+
def test_rewind
167+
@sftp.expects(:write!).with("handle", 0, "hello world\n")
168+
@sftp.expects(:read!).with("handle", 12, 8192).returns("hello world\n")
169+
@sftp.expects(:read!).with("handle", 0, 8192).returns("hello world\n")
170+
@file.puts "hello world\n"
171+
assert_equal "hello", @file.read(5)
172+
@file.rewind
173+
assert_equal "hello world", @file.read(11)
174+
end
175+
end

0 commit comments

Comments
 (0)