Allow data store to work even if file path is nil
This commit is contained in:
parent
bd7d86f4fa
commit
7b9f64f577
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue