Skip to content

Commit 6e28aa3

Browse files
Add rewind method to file operations.
1 parent ef67be4 commit 6e28aa3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ def print(*items)
131131
nil
132132
end
133133

134-
# Returns the size of the file from stats
135134
def size
136135
stat.size
137136
end
138137

138+
def rewind
139+
self.pos = 0
140+
end
141+
139142
# Writes each argument to the stream, appending a newline to any item
140143
# that does not already end in a newline. Array arguments are flattened.
141144
def puts(*items)

test/test_file.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,14 @@ def test_size_should_return_size_from_stat
162162
@sftp.expects(:fstat!).with("handle").returns(stat)
163163
assert_equal 1024, @file.size
164164
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
165175
end

0 commit comments

Comments
 (0)