diff --git a/lib/vagrant/active_list.rb b/lib/vagrant/active_list.rb index 334ef7c8a..66874f4c9 100644 --- a/lib/vagrant/active_list.rb +++ b/lib/vagrant/active_list.rb @@ -26,7 +26,11 @@ module Vagrant @list ||= {} return @list unless File.file?(path) File.open(path, "r") do |f| - @list = JSON.parse(f.read) + 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 diff --git a/test/vagrant/active_list_test.rb b/test/vagrant/active_list_test.rb index 29d90b1ad..da579d765 100644 --- a/test/vagrant/active_list_test.rb +++ b/test/vagrant/active_list_test.rb @@ -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