From 39b2539ec7821aa952ca36fbda4c68e35c8a2d41 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Dec 2013 11:17:48 -0800 Subject: [PATCH] commands/plugin: state file keeps track of the Vagrant version --- plugins/commands/plugin/state_file.rb | 37 ++++++++--- .../commands/plugin/state_file_test.rb | 62 ++++++++++++------- 2 files changed, 69 insertions(+), 30 deletions(-) diff --git a/plugins/commands/plugin/state_file.rb b/plugins/commands/plugin/state_file.rb index f57ab9f20..51684256f 100644 --- a/plugins/commands/plugin/state_file.rb +++ b/plugins/commands/plugin/state_file.rb @@ -9,16 +9,23 @@ module VagrantPlugins @path = path @data = {} - @data = JSON.parse(@path.read) if @path.exist? - @data["installed"] ||= [] + if @path.exist? + @data = JSON.parse(@path.read) + upgrade_v0! if !@data["version"] + end + + @data["version"] ||= "1" + @data["installed"] ||= {} end # Add a plugin that is installed to the state file. # # @param [String] name The name of the plugin def add_plugin(name) - if !@data["installed"].include?(name) - @data["installed"] << name + if !@data["installed"].has_key?(name) + @data["installed"][name] = { + "vagrant_version" => Vagrant::VERSION, + } end save! @@ -43,15 +50,27 @@ module VagrantPlugins # This saves the state back into the state file. def save! - # Scrub some fields - @data["installed"].sort! - @data["installed"].uniq! - - # Save @path.open("w+") do |f| f.write(JSON.dump(@data)) end end + + protected + + # This upgrades the internal data representation from V0 (the initial + # version) to V1. + def upgrade_v0! + @data["version"] = "1" + + new_installed = {} + (@data["installed"] || []).each do |plugin| + new_installed[plugin] = {} + end + + @data["installed"] = new_installed + + save! + end end end end diff --git a/test/unit/plugins/commands/plugin/state_file_test.rb b/test/unit/plugins/commands/plugin/state_file_test.rb index 0ef0006db..dd394c788 100644 --- a/test/unit/plugins/commands/plugin/state_file_test.rb +++ b/test/unit/plugins/commands/plugin/state_file_test.rb @@ -18,30 +18,50 @@ describe VagrantPlugins::CommandPlugin::StateFile do subject { described_class.new(path) } - it "should have no plugins without saving some" do - expect(subject.installed_plugins).to be_empty + context "new usage" do + it "should have no plugins without saving some" do + expect(subject.installed_plugins).to be_empty + end + + it "should have plugins when saving" do + subject.add_plugin("foo") + + instance = described_class.new(path) + plugins = instance.installed_plugins + expect(plugins.length).to eql(1) + expect(plugins["foo"]).to eql({ "vagrant_version" => Vagrant::VERSION }) + end + + it "should remove plugins" do + subject.add_plugin("foo") + subject.remove_plugin("foo") + + instance = described_class.new(path) + expect(instance.installed_plugins).to be_empty + end + + it "should store plugins uniquely" do + subject.add_plugin("foo") + subject.add_plugin("foo") + + instance = described_class.new(path) + expect(instance.installed_plugins.keys).to eql(["foo"]) + end end - it "should have plugins when saving" do - subject.add_plugin("foo") + context "with an old-style file" do + before do + data = { + "installed" => ["foo"], + } - instance = described_class.new(path) - expect(instance.installed_plugins).to eql(["foo"]) - end + path.open("w+") do |f| + f.write(JSON.dump(data)) + end + end - it "should remove plugins" do - subject.add_plugin("foo") - subject.remove_plugin("foo") - - instance = described_class.new(path) - expect(instance.installed_plugins).to be_empty - end - - it "should store plugins uniquely" do - subject.add_plugin("foo") - subject.add_plugin("foo") - - instance = described_class.new(path) - expect(instance.installed_plugins).to eql(["foo"]) + it "should have the right installed plugins" do + subject.installed_plugins.keys.should eql(["foo"]) + end end end