diff --git a/lib/sshkit/backends/local.rb b/lib/sshkit/backends/local.rb index 8108255f..564e1910 100644 --- a/lib/sshkit/backends/local.rb +++ b/lib/sshkit/backends/local.rb @@ -13,11 +13,7 @@ def initialize(_ = nil, &block) def upload!(local, remote, options = {}) remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") || pwd_path.nil? if local.is_a?(String) - if options[:recursive] - FileUtils.cp_r(local, remote) - else - FileUtils.cp(local, remote) - end + options[:recursive] ? FileUtils.cp_r(local, remote) : FileUtils.cp(local, remote) else File.open(remote, "wb") do |f| IO.copy_stream(local, f) @@ -25,9 +21,11 @@ def upload!(local, remote, options = {}) end end - def download!(remote, local=nil, _options = {}) + def download!(remote, local=nil, options = {}) remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") || pwd_path.nil? - if local.nil? + if options[:recursive] && (local.nil? || local.is_a?(String)) + FileUtils.cp_r(remote, local || File.basename(remote)) + elsif local.nil? FileUtils.cp(remote, File.basename(remote)) else File.open(remote, "rb") do |f| diff --git a/test/unit/backends/test_local.rb b/test/unit/backends/test_local.rb index b1f6ece8..72aa371f 100644 --- a/test/unit/backends/test_local.rb +++ b/test/unit/backends/test_local.rb @@ -17,6 +17,31 @@ def test_execute assert_equal true, local.execute assert_equal true, local.execute('cd && pwd') end + + def test_recursive_download + Dir.mktmpdir do |dir| + source = File.join(dir, 'source') + target = File.join(dir, 'target') + FileUtils.mkdir_p(source) + File.write(File.join(source, 'file'), 'contents') + + local.download!(source, target, recursive: true) + + assert_equal 'contents', File.read(File.join(target, 'file')) + end + end + + def test_recursive_option_preserves_io_downloads + Tempfile.create do |source| + source.write('contents') + source.flush + output = StringIO.new + + local.download!(source.path, output, recursive: true) + + assert_equal 'contents', output.string + end + end end end end