commands/plugin: state file keeps track of the Vagrant version

This commit is contained in:
Mitchell Hashimoto 2013-12-04 11:17:48 -08:00
parent b2844d420d
commit 39b2539ec7
2 changed files with 69 additions and 30 deletions

View File

@ -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

View File

@ -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