Skip to content

Commit

Permalink
Handle Pathname in download/upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tietew committed Feb 19, 2020
1 parent acbd1f7 commit 8d43a01
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/net/sftp/operations/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class Download
# This will return immediately, and requires that the SSH event loop be
# run in order to effect the download. (See #wait.)
def initialize(sftp, local, remote, options={}, &progress)
local = local.to_path if local.respond_to?(:to_path)
remote = remote.to_path if remote.respond_to?(:to_path)

@sftp = sftp
@local = local
@remote = remote
Expand Down
3 changes: 3 additions & 0 deletions lib/net/sftp/operations/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class Upload
# This will return immediately, and requires that the SSH event loop be
# run in order to effect the upload. (See #wait.)
def initialize(sftp, local, remote, options={}, &progress) #:nodoc:
local = local.to_path if local.respond_to?(:to_path)
remote = remote.to_path if remote.respond_to?(:to_path)

@sftp = sftp
@local = local
@remote = remote
Expand Down
1 change: 1 addition & 0 deletions test/common.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test/unit'
require 'mocha/setup'
require 'stringio'
require 'pathname'

begin
require 'net/ssh'
Expand Down
16 changes: 15 additions & 1 deletion test/test_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def test_download_file_should_transfer_remote_to_local_in_spite_of_fragmentation
assert_equal text, file.string
end

def test_download_file_should_handle_Pathname
local = Pathname.new("/path/to/local").freeze
remote = Pathname.new("/path/to/remote").freeze
text = "this is some text\n"

expect_file_transfer(remote.to_s, text)

file = StringIO.new
File.stubs(:open).with(local.to_s, "wb").returns(file)

assert_scripted_command { sftp.download(remote, local) }
assert_equal text, file.string
end

def test_download_large_file_should_transfer_remote_to_local
local = "/path/to/local"
remote = "/path/to/remote"
Expand Down Expand Up @@ -286,4 +300,4 @@ def prepare_directory_tree_download(local, remote)

[file1, file2]
end
end
end
7 changes: 7 additions & 0 deletions test/test_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def test_upload_file_should_send_file_contents
assert_scripted_command { sftp.upload("/path/to/local", "/path/to/remote") }
end

def test_upload_file_should_handle_Pathname
local = Pathname.new("/path/to/local").freeze
remote = Pathname.new("/path/to/remote").freeze
expect_file_transfer(local.to_s, remote.to_s, "here are the contents")
assert_scripted_command { sftp.upload(local, remote) }
end

def test_upload_file_without_remote_uses_filename_of_local_file
expect_file_transfer("/path/to/local", "local", "here are the contents")

Expand Down

0 comments on commit 8d43a01

Please sign in to comment.