Fix VAGRANT_CWD handling in vagrant push ftp

When testing all of the push functionality I ran into the fact that the
FTP upload code did not recognize that I had VAGRANT_CWD set, so it
wasn't finding the right files to upload.

This should make everything work properly relative to that location.
This commit is contained in:
Paul Hinze 2022-01-06 13:39:16 -06:00
parent 0255034126
commit d03a058e04
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 41 additions and 5 deletions

View File

@ -21,7 +21,7 @@ module VagrantPlugins
files = nil
begin
files = Hash[*all_files.flat_map do |file|
relative_path = relative_path_for(file, config.dir)
relative_path = relative_path_for(file, base_dir)
destination = File.join(config.destination, relative_path)
file = File.expand_path(file, env.root_path)
[file, destination]
@ -31,11 +31,11 @@ module VagrantPlugins
end
ftp = "#{config.username}@#{config.host}:#{config.destination}"
env.ui.info "Uploading #{env.root_path} to #{ftp}"
env.ui.info "Uploading #{files.length} files to #{env.root_path} to #{ftp}"
connect do |ftp|
files.each do |local, remote|
@logger.info "Uploading #{local} => #{remote}"
env.ui.info "Uploading #{local} => #{remote}"
ftp.upload(local, remote)
end
end
@ -54,7 +54,7 @@ module VagrantPlugins
# only returns **files**, not folders or symlinks!
# @return [Array<String>]
def all_files
files = glob("#{config.dir}/**/*") + includes_files
files = glob("#{base_dir}/**/*") + includes_files
filter_excludes!(files, config.excludes)
files.reject! { |f| !File.file?(f) }
files
@ -64,7 +64,7 @@ module VagrantPlugins
# @return [Array<String>]
def includes_files
includes = config.includes.flat_map do |i|
path = absolute_path_for(i, config.dir)
path = absolute_path_for(i, base_dir)
[path, "#{path}/**/*"]
end
@ -121,6 +121,14 @@ module VagrantPlugins
rescue ArgumentError
return path
end
def base_dir
if Pathname.new(config.dir).absolute?
config.dir
else
File.join(File.expand_path(env.root_path), config.dir)
end
end
end
end
end

View File

@ -64,6 +64,9 @@ describe VagrantPlugins::FTPPush::Push do
.and_return(%w(*.rb))
end
after do
server.reset
end
it "pushes the files to the server" do
subject.push
@ -74,6 +77,31 @@ describe VagrantPlugins::FTPPush::Push do
expect(subject).to receive(:all_files).and_raise(SystemStackError)
expect{ subject.push }.to raise_error(VagrantPlugins::FTPPush::Errors::TooManyFiles)
end
context "when VAGRANT_CWD is set to something relative" do
# this will be the PWD for the test context
let(:pwd) { Pathname.new(Dir.mktmpdir("vagrant-ftp-push-pwd")) }
before do
# this path should have a ../ in it since the pwd is another temp dir
# and measuring path from one to the other. they're most likely to be
# siblings
relative_path = Pathname.new(@dir).relative_path_from(pwd)
allow(env).to receive(:root_path) { relative_path.to_s }
# reset config.dir to its default since it was set absolute above
allow(config).to receive(:dir) { "." }
end
it "properly paths out the files to upload" do
Vagrant::Util::SafeChdir.safe_chdir(pwd) do
subject.push
end
expect(server.files).to eq(%w(Gemfile data.txt))
ensure
FileUtils.rm_rf(pwd)
end
end
end
describe "#connect" do