From 4d042da99be050545401a7a29f9956fd038bdba1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 May 2010 11:02:53 -0700 Subject: [PATCH] If JSON parsing fails on reading the active list, just set it to an empty hash --- lib/vagrant/active_list.rb | 6 +++++- test/vagrant/active_list_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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