If JSON parsing fails on reading the active list, just set it to an empty hash

This commit is contained in:
Mitchell Hashimoto 2010-05-06 11:02:53 -07:00
parent 699fa2ce77
commit 4d042da99b
2 changed files with 17 additions and 1 deletions

View File

@ -26,7 +26,11 @@ module Vagrant
@list ||= {}
return @list unless File.file?(path)
File.open(path, "r") do |f|
begin
@list = JSON.parse(f.read)
rescue Exception
@list = {}
end
# This forces earlier versions of Vagrant to use the new hash
# format. Clearing out the old data isn't a big deal since it

View File

@ -57,6 +57,18 @@ class ActiveListTest < Test::Unit::TestCase
assert result.equal?(@list.list)
assert result.equal?(@list.list)
end
should "be an empty hash if JSON parsing raises an exception" do
file = mock("file")
file.stubs(:read)
File.expects(:file?).returns(true)
File.expects(:open).with(@list.path, 'r').once.yields(file)
JSON.expects(:parse).raises(Exception)
assert_nothing_raised do
assert_equal Hash.new, @list.list(true)
end
end
end
context "filter list" do