From 5ee1c93c428af7528655e16b4dc468a5086c6d41 Mon Sep 17 00:00:00 2001 From: Michael Moen Date: Thu, 9 Nov 2017 15:08:18 -0600 Subject: [PATCH] call #to_s on remote so Pathnames don't go :boom: --- CHANGELOG.md | 1 + lib/sshkit/backends/local.rb | 4 ++-- lib/sshkit/backends/netssh.rb | 4 ++-- test/functional/backends/test_local.rb | 10 ++++++++++ test/functional/backends/test_netssh.rb | 10 ++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ce61623..3ccf0a4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ appear at the top. ## [Unreleased][] * Your contribution here! + * [#410](https://github.com/capistrano/sshkit/pull/410): call #to_s on remote so Pathnames don't go :boom: - [@UnderpantsGnome](https://github.com/UnderpantsGnome) ## [1.15.0][] (2017-11-03) diff --git a/lib/sshkit/backends/local.rb b/lib/sshkit/backends/local.rb index 0620e102..f5fb4f9b 100644 --- a/lib/sshkit/backends/local.rb +++ b/lib/sshkit/backends/local.rb @@ -11,7 +11,7 @@ def initialize(_ = nil, &block) end def upload!(local, remote, options = {}) - remote = File.join(pwd_path, remote) unless remote.start_with?("/") + remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") if local.is_a?(String) if options[:recursive] FileUtils.cp_r(local, remote) @@ -26,7 +26,7 @@ def upload!(local, remote, options = {}) end def download!(remote, local=nil, _options = {}) - remote = File.join(pwd_path, remote) unless remote.start_with?("/") + remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") if local.nil? FileUtils.cp(remote, File.basename(remote)) else diff --git a/lib/sshkit/backends/netssh.rb b/lib/sshkit/backends/netssh.rb index a5533d99..a16a2c39 100644 --- a/lib/sshkit/backends/netssh.rb +++ b/lib/sshkit/backends/netssh.rb @@ -63,7 +63,7 @@ def assign_defaults def upload!(local, remote, options = {}) summarizer = transfer_summarizer('Uploading', options) - remote = File.join(pwd_path, remote) unless remote.start_with?("/") + remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") with_ssh do |ssh| ssh.scp.upload!(local, remote, options, &summarizer) end @@ -71,7 +71,7 @@ def upload!(local, remote, options = {}) def download!(remote, local=nil, options = {}) summarizer = transfer_summarizer('Downloading', options) - remote = File.join(pwd_path, remote) unless remote.start_with?("/") + remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") with_ssh do |ssh| ssh.scp.download!(remote, local, options, &summarizer) end diff --git a/test/functional/backends/test_local.rb b/test/functional/backends/test_local.rb index b559f241..073516b5 100644 --- a/test/functional/backends/test_local.rb +++ b/test/functional/backends/test_local.rb @@ -20,6 +20,16 @@ def test_upload end end + def test_upload_via_pathname + Dir.mktmpdir do |dir| + File.new("#{dir}/local", 'w') + Local.new do + upload!("#{dir}/local", Pathname.new("#{dir}/remote")) + end.run + assert File.exist?("#{dir}/remote") + end + end + def test_upload_within file_contents = "Some Content" actual_file_contents = nil diff --git a/test/functional/backends/test_netssh.rb b/test/functional/backends/test_netssh.rb index f260f60a..63df4d2c 100644 --- a/test/functional/backends/test_netssh.rb +++ b/test/functional/backends/test_netssh.rb @@ -145,6 +145,16 @@ def test_upload_large_file assert_equal File.open(file_name).read, file_contents end + def test_upload_via_pathname + file_contents = "" + Netssh.new(a_host) do |_host| + file_name = Pathname.new(File.join("/tmp", SecureRandom.uuid)) + upload!(StringIO.new('example_io'), file_name) + file_contents = download!(file_name) + end.run + assert_equal "example_io", file_contents + end + def test_interaction_handler captured_command_result = nil Netssh.new(a_host) do