From 1b4d7848bc81a082e8affdff2eb80538df4bc7b1 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 16 Jun 2017 15:15:02 -0700 Subject: [PATCH] Fix vagrant_cwd warnings Prior to this commit, if a user ran a vagrant command within a subdir, it would warn about the cwd changing which is not actually the case. This commit adds an additional check to see if vagrant is being invoked within a subdirectory so that it doesn't warn the user. --- lib/vagrant/machine.rb | 8 ++++---- test/unit/vagrant/machine_test.rb | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 884af9503..7c2310acb 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -577,13 +577,13 @@ module Vagrant end if vagrant_cwd.nil? - File.write(vagrant_cwd_filepath, @env.cwd) - elsif vagrant_cwd != @env.cwd.to_s + File.write(vagrant_cwd_filepath, @env.root_path) + elsif vagrant_cwd != @env.root_path.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) + current_wd: @env.root_path.to_s)) + File.write(vagrant_cwd_filepath, @env.root_path) end end end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 635255c77..8163b46c4 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -338,7 +338,7 @@ describe Vagrant::Machine do 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') + allow(env).to receive(:root_path).and_return('/a/new/path') instance.action(action_name) expect(subject.ui).to have_received(:warn) do |warn_msg| @@ -346,6 +346,28 @@ describe Vagrant::Machine do expect(warn_msg).to include('/a/new/path') end end + + context "if in a subdir" do + let (:data_dir) { env.cwd } + + it 'should not warn if vagrant is run in subdirectory' 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) + # mock out cwd to be subdir and ensure no warn is printed + allow(env).to receive(:cwd).and_return("#{original_cwd}/a/new/path") + + instance.action(action_name) + expect(subject.ui).to_not have_received(:warn) + end + end end describe "#action_raw" do