Add basic unit test for CWD change warning

This commit is contained in:
Brian Cain 2017-06-06 08:38:03 -07:00
parent 48b0e00368
commit 79c7799fd9
2 changed files with 28 additions and 10 deletions

View File

@ -562,22 +562,23 @@ module Vagrant
@data_dir.join("creator_uid") @data_dir.join("creator_uid")
end end
# Checks the current directory for a given machine
# and displays a warning if that machine has moved
# from its previous location on disk. If the machine
# has moved, it prints a warning to the user.
def check_cwd def check_cwd
vagrant_cwd_filepath = @data_dir.join('vagrant_cwd') vagrant_cwd_filepath = @data_dir.join('vagrant_cwd')
vagrant_cwd = if File.exist?(vagrant_cwd_filepath) vagrant_cwd = if File.exist?(vagrant_cwd_filepath)
File.read(vagrant_cwd_filepath).chomp File.read(vagrant_cwd_filepath).chomp
end end
if vagrant_cwd if vagrant_cwd.nil?
if vagrant_cwd != @env.cwd.to_s File.write(vagrant_cwd_filepath, @env.cwd)
elsif vagrant_cwd != @env.cwd.to_s
ui.warn(I18n.t( ui.warn(I18n.t(
'vagrant.moved_cwd', 'vagrant.moved_cwd',
old_wd: vagrant_cwd, old_wd: vagrant_cwd,
current_wd: @env.cwd.to_s)) current_wd: @env.cwd.to_s))
File.write(vagrant_cwd_filepath, @env.cwd)
end
else
File.write(vagrant_cwd_filepath, @env.cwd) File.write(vagrant_cwd_filepath, @env.cwd)
end end
end end

View File

@ -308,6 +308,23 @@ describe Vagrant::Machine do
to raise_error(Vagrant::Errors::UnimplementedProviderAction) to raise_error(Vagrant::Errors::UnimplementedProviderAction)
end end
it 'should not warn if the machines cwd has not changed' do
initial_action_name = :up
second_action_name = :reload
callable = lambda { |_env| }
original_cwd = env.cwd.to_s
allow(provider).to receive(:action).with(initial_action_name).and_return(callable)
allow(provider).to receive(:action).with(second_action_name).and_return(callable)
allow(subject.ui).to receive(:warn)
instance.action(initial_action_name)
expect(subject.ui).to_not have_received(:warn)
instance.action(second_action_name)
expect(subject.ui).to_not have_received(:warn)
end
it 'should warn if the machine was last run under a different directory' do it 'should warn if the machine was last run under a different directory' do
action_name = :up action_name = :up
callable = lambda { |_env| } callable = lambda { |_env| }