Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Pathname in download/upload #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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