From 15871a481bdca3008471deca582f79c5a5fe7578 Mon Sep 17 00:00:00 2001 From: Fernando Seror Date: Sun, 8 Jan 2017 20:21:52 -0500 Subject: [PATCH 1/3] Warn about changes to CWD on every machine action Whenever the path where the machine was first created changes, Vagrant will now show just one warning when an action is run on the machine. The idea is that if a user copies the machine over to a different directory with the idea of running two different machines, this warning will now help the user determine how to make that work. --- lib/vagrant/machine.rb | 22 ++++++++++++++++++++++ templates/locales/en.yml | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index e7107fb62..a3b13373e 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -170,6 +170,8 @@ module Vagrant # Extra env keys are the remaining opts extra_env = opts.dup + check_cwd + # Create a deterministic ID for this machine vf = nil vf = @env.vagrantfile_name[0] if @env.vagrantfile_name @@ -559,5 +561,25 @@ module Vagrant return nil if !@data_dir @data_dir.join("creator_uid") end + + def check_cwd + vagrant_cwd_filepath = @data_dir.join('vagrant_cwd') + vagrant_cwd = if File.exist?(vagrant_cwd_filepath) + File.read(vagrant_cwd_filepath).chomp + end + + if vagrant_cwd + if vagrant_cwd != @env.cwd.to_s + ui.warn(I18n.t( + 'vagrant.moved_cwd', + old_wd: vagrant_cwd, + current_wd: @env.cwd.to_s)) + + File.write(vagrant_cwd_filepath, @env.cwd) + end + else + File.write(vagrant_cwd_filepath, @env.cwd) + end + end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5e36d67a4..2e6a6f5e9 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -165,6 +165,10 @@ en: description of what they do. %{list} + moved_cwd: |- + This machine used to live in %{old_wd} but it's now at %{current_wd}. + Please change the name of the machine if you want to run it as a different + machine. guest_deb_installing_smb: |- Installing SMB "mount.cifs"... global_status_footer: |- From 48b0e00368adaaeba771112c2236dfd7d7c1f2a8 Mon Sep 17 00:00:00 2001 From: Fernando Seror Date: Tue, 18 Apr 2017 18:45:20 -0500 Subject: [PATCH 2/3] Tests regarding warning whenever CWD changes --- lib/vagrant/machine.rb | 2 +- test/unit/vagrant/machine_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index a3b13373e..4530364ad 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -170,7 +170,7 @@ module Vagrant # Extra env keys are the remaining opts extra_env = opts.dup - check_cwd + check_cwd # Warns the UI if the machine was last used on a different dir # Create a deterministic ID for this machine vf = nil diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index c59d3b029..9b40f9f61 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -307,6 +307,28 @@ describe Vagrant::Machine do expect { instance.action(action_name) }. to raise_error(Vagrant::Errors::UnimplementedProviderAction) end + + it 'should warn if the machine was last run under a different directory' do + action_name = :up + callable = lambda { |_env| } + original_cwd = env.cwd.to_s + + allow(provider).to receive(:action).with(action_name).and_return(callable) + allow(subject.ui).to receive(:warn) + + instance.action(action_name) + + expect(subject.ui).to_not have_received(:warn) + + # Whenever the machine is run on a different directory, the user is warned + allow(env).to receive(:cwd).and_return('/a/new/path') + instance.action(action_name) + + expect(subject.ui).to have_received(:warn) do |warn_msg| + expect(warn_msg).to include(original_cwd) + expect(warn_msg).to include('/a/new/path') + end + end end describe "#action_raw" do From 79c7799fd9bca4517e4083d8b3c241c502039594 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 6 Jun 2017 08:38:03 -0700 Subject: [PATCH 3/3] Add basic unit test for CWD change warning --- lib/vagrant/machine.rb | 21 +++++++++++---------- test/unit/vagrant/machine_test.rb | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 4530364ad..c5e73b415 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -562,22 +562,23 @@ module Vagrant @data_dir.join("creator_uid") 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 vagrant_cwd_filepath = @data_dir.join('vagrant_cwd') vagrant_cwd = if File.exist?(vagrant_cwd_filepath) File.read(vagrant_cwd_filepath).chomp end - if vagrant_cwd - if vagrant_cwd != @env.cwd.to_s - ui.warn(I18n.t( - 'vagrant.moved_cwd', - old_wd: vagrant_cwd, - current_wd: @env.cwd.to_s)) - - File.write(vagrant_cwd_filepath, @env.cwd) - end - else + if vagrant_cwd.nil? + File.write(vagrant_cwd_filepath, @env.cwd) + elsif vagrant_cwd != @env.cwd.to_s + ui.warn(I18n.t( + 'vagrant.moved_cwd', + old_wd: vagrant_cwd, + current_wd: @env.cwd.to_s)) File.write(vagrant_cwd_filepath, @env.cwd) end end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 9b40f9f61..635255c77 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -308,6 +308,23 @@ describe Vagrant::Machine do to raise_error(Vagrant::Errors::UnimplementedProviderAction) 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 action_name = :up callable = lambda { |_env| }