diff --git a/lib/vagrant/data_store.rb b/lib/vagrant/data_store.rb index 37908a3ed..f59f004b5 100644 --- a/lib/vagrant/data_store.rb +++ b/lib/vagrant/data_store.rb @@ -21,25 +21,34 @@ module Vagrant def initialize(file_path) @logger = Log4r::Logger.new("vagrant::datastore") - @logger.info("Created: #{file_path}") - @file_path = Pathname.new(file_path) + if file_path + @logger.info("Created: #{file_path}") - if @file_path.exist? - raise Errors::DotfileIsDirectory if @file_path.directory? + @file_path = Pathname.new(file_path) + if @file_path.exist? + raise Errors::DotfileIsDirectory if @file_path.directory? - begin - merge!(JSON.parse(@file_path.read)) - rescue JSON::ParserError - # Ignore if the data is invalid in the file. - @logger.error("Data store contained invalid JSON. Ignoring.") + begin + merge!(JSON.parse(@file_path.read)) + rescue JSON::ParserError + # Ignore if the data is invalid in the file. + @logger.error("Data store contained invalid JSON. Ignoring.") + end end + else + @logger.info("No file path. In-memory data store.") + @file_path = nil end end # Commits any changes to the data to disk. Even if the data # hasn't changed, it will be reserialized and written to disk. def commit + if !@file_path + raise StandardError, "In-memory data stores can't be committed." + end + clean_nil_and_empties if empty? diff --git a/test/unit/vagrant/data_store_test.rb b/test/unit/vagrant/data_store_test.rb index 3bd125074..0cf2cc308 100644 --- a/test/unit/vagrant/data_store_test.rb +++ b/test/unit/vagrant/data_store_test.rb @@ -64,4 +64,16 @@ describe Vagrant::DataStore do # The file should no longer exist db_file.should_not be_exist end + + it "works if the DB file is nil" do + store = described_class.new(nil) + store[:foo] = "bar" + store[:foo].should == "bar" + end + + it "throws an exception if attempting to commit a data store with no file" do + store = described_class.new(nil) + expect { store.commit }. + to raise_error(StandardError) + end end