Skip to content
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
12 changes: 5 additions & 7 deletions lib/sshkit/backends/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ 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)
end
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|
Expand Down
25 changes: 25 additions & 0 deletions test/unit/backends/test_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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