Skip to content

Commit 8d43a01

Browse files
committed
Handle Pathname in download/upload
1 parent acbd1f7 commit 8d43a01

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

lib/net/sftp/operations/download.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class Download
145145
# This will return immediately, and requires that the SSH event loop be
146146
# run in order to effect the download. (See #wait.)
147147
def initialize(sftp, local, remote, options={}, &progress)
148+
local = local.to_path if local.respond_to?(:to_path)
149+
remote = remote.to_path if remote.respond_to?(:to_path)
150+
148151
@sftp = sftp
149152
@local = local
150153
@remote = remote

lib/net/sftp/operations/upload.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class Upload
141141
# This will return immediately, and requires that the SSH event loop be
142142
# run in order to effect the upload. (See #wait.)
143143
def initialize(sftp, local, remote, options={}, &progress) #:nodoc:
144+
local = local.to_path if local.respond_to?(:to_path)
145+
remote = remote.to_path if remote.respond_to?(:to_path)
146+
144147
@sftp = sftp
145148
@local = local
146149
@remote = remote

test/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'test/unit'
22
require 'mocha/setup'
33
require 'stringio'
4+
require 'pathname'
45

56
begin
67
require 'net/ssh'

test/test_download.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def test_download_file_should_transfer_remote_to_local_in_spite_of_fragmentation
3535
assert_equal text, file.string
3636
end
3737

38+
def test_download_file_should_handle_Pathname
39+
local = Pathname.new("/path/to/local").freeze
40+
remote = Pathname.new("/path/to/remote").freeze
41+
text = "this is some text\n"
42+
43+
expect_file_transfer(remote.to_s, text)
44+
45+
file = StringIO.new
46+
File.stubs(:open).with(local.to_s, "wb").returns(file)
47+
48+
assert_scripted_command { sftp.download(remote, local) }
49+
assert_equal text, file.string
50+
end
51+
3852
def test_download_large_file_should_transfer_remote_to_local
3953
local = "/path/to/local"
4054
remote = "/path/to/remote"
@@ -286,4 +300,4 @@ def prepare_directory_tree_download(local, remote)
286300

287301
[file1, file2]
288302
end
289-
end
303+
end

test/test_upload.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ def test_upload_file_should_send_file_contents
1010
assert_scripted_command { sftp.upload("/path/to/local", "/path/to/remote") }
1111
end
1212

13+
def test_upload_file_should_handle_Pathname
14+
local = Pathname.new("/path/to/local").freeze
15+
remote = Pathname.new("/path/to/remote").freeze
16+
expect_file_transfer(local.to_s, remote.to_s, "here are the contents")
17+
assert_scripted_command { sftp.upload(local, remote) }
18+
end
19+
1320
def test_upload_file_without_remote_uses_filename_of_local_file
1421
expect_file_transfer("/path/to/local", "local", "here are the contents")
1522

0 commit comments

Comments
 (0)