From 7de798221421103346d5c8cbd00b257db1dbea69 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 14 Jul 2010 21:03:58 -0700 Subject: [PATCH] CleanMachineFolder won't run if the folder is < 10 characters. Small safeguard againt unwanted rm-rfs. --- lib/vagrant/action/vm/clean_machine_folder.rb | 6 ++++++ test/vagrant/action/vm/clean_machine_folder_test.rb | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/action/vm/clean_machine_folder.rb b/lib/vagrant/action/vm/clean_machine_folder.rb index 6f8172f73..86975bc12 100644 --- a/lib/vagrant/action/vm/clean_machine_folder.rb +++ b/lib/vagrant/action/vm/clean_machine_folder.rb @@ -19,6 +19,12 @@ module Vagrant def clean_machine_folder folder = File.join(VirtualBox::Global.global.system_properties.default_machine_folder, "*") + + # Small safeguard against potentially unwanted rm-rf, since the default + # machine folder will typically always be greater than 10 characters long. + # For users with it < 10, out of luck? + return if folder.length < 10 + Dir[folder].each do |f| next unless File.directory?(f) diff --git a/test/vagrant/action/vm/clean_machine_folder_test.rb b/test/vagrant/action/vm/clean_machine_folder_test.rb index 90b1ef06c..71d2cf5ab 100644 --- a/test/vagrant/action/vm/clean_machine_folder_test.rb +++ b/test/vagrant/action/vm/clean_machine_folder_test.rb @@ -19,7 +19,7 @@ class CleanMachineFolderVMActionTest < Test::Unit::TestCase context "cleaning the folder" do setup do - @machine_folder = "foo" + @machine_folder = "/foo/bar/baz" @folder = File.join(@machine_folder, "*") VirtualBox::Global.global.system_properties.stubs(:default_machine_folder).returns(@machine_folder) File.stubs(:file?).returns(true) @@ -73,5 +73,10 @@ class CleanMachineFolderVMActionTest < Test::Unit::TestCase @instance.clean_machine_folder end + + should "do nothing if folder is < 10 characters" do + VirtualBox::Global.global.system_properties.stubs(:default_machine_folder).returns("foo") + Dir.expects(:[]).never + end end end