diff --git a/plugins/guests/linux/cap/persist_mount_shared_folder.rb b/plugins/guests/linux/cap/persist_mount_shared_folder.rb index e05699d5a..120e1f541 100644 --- a/plugins/guests/linux/cap/persist_mount_shared_folder.rb +++ b/plugins/guests/linux/cap/persist_mount_shared_folder.rb @@ -17,11 +17,6 @@ module VagrantPlugins # @param [Machine] machine The machine to run the action on # @param [Map] A map of folders to add to fstab def self.persist_mount_shared_folder(machine, folders) - if !fstab_exists?(machine) - @@logger.info("no fstab file found, not modifying /etc/fstab") - return - end - if folders.nil? @@logger.info("clearing /etc/fstab") self.remove_vagrant_managed_fstab(machine) @@ -66,7 +61,11 @@ module VagrantPlugins end def self.remove_vagrant_managed_fstab(machine) - machine.communicate.sudo("sed -i '/\#VAGRANT-BEGIN/,/\#VAGRANT-END/d' /etc/fstab") + if fstab_exists?(machine) + machine.communicate.sudo("sed -i '/\#VAGRANT-BEGIN/,/\#VAGRANT-END/d' /etc/fstab") + else + @@logger.info("no fstab file found, carrying on") + end end end end diff --git a/test/unit/plugins/guests/linux/cap/persist_mount_shared_folder_test.rb b/test/unit/plugins/guests/linux/cap/persist_mount_shared_folder_test.rb index 31f7ae764..b6d446e53 100644 --- a/test/unit/plugins/guests/linux/cap/persist_mount_shared_folder_test.rb +++ b/test/unit/plugins/guests/linux/cap/persist_mount_shared_folder_test.rb @@ -83,12 +83,20 @@ describe "VagrantPlugins::GuestLinux::Cap::PersistMountSharedFolder" do context "fstab does not exist" do before do allow(cap).to receive(:fstab_exists?).and_return(false) + # Ensure /etc/fstab is not being modified + expect(comm).not_to receive(:sudo).with(/sed -i .? \/etc\/fstab/) end - it "does not modify /etc/fstab" do - expect(cap).not_to receive(:remove_vagrant_managed_fstab) + it "creates /etc/fstab" do + expect(cap).to receive(:remove_vagrant_managed_fstab) + expect(comm).to receive(:sudo).with(/>> \/etc\/fstab/) + cap.persist_mount_shared_folder(machine, []) + end + + it "does not remove contents of /etc/fstab" do + expect(cap).to receive(:remove_vagrant_managed_fstab) expect(comm).not_to receive(:sudo).with(/echo '' >> \/etc\/fstab/) - cap.persist_mount_shared_folder(machine, folders) + cap.persist_mount_shared_folder(machine, nil) end end end