diff --git a/lib/vagrant/data_store.rb b/lib/vagrant/data_store.rb index f3e69f93a..af3a117da 100644 --- a/lib/vagrant/data_store.rb +++ b/lib/vagrant/data_store.rb @@ -25,8 +25,11 @@ module Vagrant return if !file_path clean_nil_and_empties - File.open(file_path, "w") do |f| - f.write(to_json) + + if empty? + File.delete(file_path) if File.file?(file_path) + else + File.open(file_path, "w") { |f| f.write(to_json) } end end diff --git a/test/vagrant/data_store_test.rb b/test/vagrant/data_store_test.rb index 53616a5ee..89addb959 100644 --- a/test/vagrant/data_store_test.rb +++ b/test/vagrant/data_store_test.rb @@ -11,7 +11,7 @@ class DataStoreTest < Test::Unit::TestCase end teardown do - File.delete(@db_file) + File.delete(@db_file) if File.file?(@db_file) end should "be a hash with indifferent access" do @@ -39,6 +39,17 @@ class DataStoreTest < Test::Unit::TestCase assert_equal "changed", @klass.new(@db_file)[:foo] end + should "delete the data store file if the hash is empty" do + @instance[:foo] = :bar + @instance.commit + assert File.exist?(@db_file) + + @instance.clear + assert @instance.empty? + @instance.commit + assert !File.exist?(@db_file) + end + should "clean nil and empties if commit is called" do @instance[:foo] = { :bar => nil } @instance[:bar] = {}