From 46e9250f09f6209519c4aff61fc8f431906ce2f9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 29 May 2010 12:15:55 -0700 Subject: [PATCH] Remove the dotfile (.vagrant) if no more active VMs exist --- lib/vagrant/environment.rb | 13 +++++++------ test/vagrant/environment_test.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 3258c9b77..f600ca3e8 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -305,10 +305,7 @@ module Vagrant # Persists this environment's VM to the dotfile so it can be # re-loaded at a later time. def update_dotfile - if parent - parent.update_dotfile - return - end + return parent.update_dotfile if parent # Generate and save the persisted VM info data = vms.inject({}) do |acc, data| @@ -317,8 +314,12 @@ module Vagrant acc end - File.open(dotfile_path, 'w+') do |f| - f.write(data.to_json) + if data.empty? + File.rm(dotfile_path) + else + File.open(dotfile_path, 'w+') do |f| + f.write(data.to_json) + end end # Also add to the global store diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index fcdbd66b9..3f0c702d1 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -733,6 +733,16 @@ class EnvironmentTest < Test::Unit::TestCase @env.update_dotfile end + should "remove the dotfile if the data is empty" do + vms = { + :foo => create_vm(false) + } + + @env.stubs(:vms).returns(vms) + File.expects(:rm).with(@env.dotfile_path).once + @env.update_dotfile + end + should "write the proper data to dotfile" do vms = { :foo => create_vm(false),