diff --git a/lib/vagrant/plugin/state_file.rb b/lib/vagrant/plugin/state_file.rb index 5a0892263..50f7ac658 100644 --- a/lib/vagrant/plugin/state_file.rb +++ b/lib/vagrant/plugin/state_file.rb @@ -24,53 +24,18 @@ module Vagrant end upgrade_v0! if !@data["version"] - upgrade_v1! if @data["version"].to_s == "1" end - @data["version"] ||= "2" - @data["ruby"] ||= {"installed" => {}} + @data["version"] ||= "1" + @data["installed"] ||= {} @data["go_plugin"] ||= {} end - # Add a go plugin that is installed to the state file. - # - # @param [String] name The name of the plugin - def add_go_plugin(name, **opts) - @data["go_plugin"][name] = { - "source" => opts[:source] - } - - save! - end - - # Remove a plugin that is installed from the state file. - # - # @param [String] name The name of the plugin - def remove_go_plugin(name) - @data["go_plugin"].delete(name) - - save! - end - - # @return [Boolean] go plugin is present in this state file - def has_go_plugin?(name) - @data["go_plugin"].key?(name) - end - - # This returns a hash of installed go plugins according to the state - # file. Note that this may _not_ directly match over to actually - # installed plugins. - # - # @return [Hash] - def installed_go_plugins - @data["go_plugin"] - end - # Add a plugin that is installed to the state file. # # @param [String] name The name of the plugin def add_plugin(name, **opts) - @data["ruby"]["installed"][name] = { + @data["installed"][name] = { "ruby_version" => RUBY_VERSION, "vagrant_version" => Vagrant::VERSION, "gem_version" => opts[:version] || "", @@ -83,12 +48,49 @@ module Vagrant save! end + # Add a go plugin that is installed to the state file + # + # @param [String, Symbol] name Plugin name + def add_go_plugin(name, **opts) + @data["go_plugin"][name] = { + "source" => opts[:source] + } + + save! + end + + # Remove a go plugin that is installed from the state file + # + # @param [String, Symbol] name Name of the plugin + def remove_go_plugin(name) + @data["go_plugin"].delete(name.to_s) + + save! + end + + # Check if go plugin is installed from the state file + # + # @param [String, Symbol] name Plugin name + # @return [Boolean] + def has_go_plugin?(name) + @data["go_plugin"].key?(name.to_s) + end + + # This returns a hash of installed go plugins according to the state + # file. Note that this may _not_ directly match over to actually + # installed plugins + # + # @return [Hash] + def installed_go_plugins + @data["go_plugin"] + end + # Adds a RubyGems index source to look up gems. # # @param [String] url URL of the source. def add_source(url) - @data["ruby"]["sources"] ||= [] - @data["ruby"]["sources"] |= [url] + @data["sources"] ||= [] + @data["sources"] << url if !@data["sources"].include?(url) save! end @@ -98,21 +100,21 @@ module Vagrant # # @return [Hash] def installed_plugins - @data["ruby"]["installed"] + @data["installed"] end # Returns true/false if the plugin is present in this state file. # # @return [Boolean] def has_plugin?(name) - @data["ruby"]["installed"].key?(name) + @data["installed"].key?(name) end # Remove a plugin that is installed from the state file. # # @param [String] name The name of the plugin. def remove_plugin(name) - @data["ruby"]["installed"].delete(name) + @data["installed"].delete(name) save! end @@ -120,8 +122,8 @@ module Vagrant # # @param [String] url URL of the source def remove_source(url) - @data["ruby"]["sources"] ||= [] - @data["ruby"]["sources"].delete(url) + @data["sources"] ||= [] + @data["sources"].delete(url) save! end @@ -130,7 +132,7 @@ module Vagrant # # @return [Array] def sources - @data["ruby"]["sources"] || [] + @data["sources"] || [] end # This saves the state back into the state file. @@ -164,18 +166,6 @@ module Vagrant save! end - - # This upgrades the internal data representation from V1 to V2 - def upgrade_v1! - @data.delete("version") - new_data = { - "version" => "2", - "ruby" => @data, - "go_plugin" => {} - } - - save! - end end end end diff --git a/test/unit/vagrant/plugin/state_file_test.rb b/test/unit/vagrant/plugin/state_file_test.rb index 9546db7bc..14897c366 100644 --- a/test/unit/vagrant/plugin/state_file_test.rb +++ b/test/unit/vagrant/plugin/state_file_test.rb @@ -118,4 +118,56 @@ describe Vagrant::Plugin::StateFile do to raise_error(Vagrant::Errors::PluginStateFileParseError) end end + + context "go plugin usage" do + describe "#add_go_plugin" do + it "should add plugin to list of installed go plugins" do + subject.add_go_plugin("foo", source: "http://localhost/foo.zip") + expect(subject.installed_go_plugins).to include("foo") + end + + it "should update source when added again" do + subject.add_go_plugin("foo", source: "http://localhost/foo.zip") + expect(subject.installed_go_plugins["foo"]["source"]).to eq("http://localhost/foo.zip") + subject.add_go_plugin("foo", source: "http://localhost/foo1.zip") + expect(subject.installed_go_plugins["foo"]["source"]).to eq("http://localhost/foo1.zip") + end + end + + describe "#remove_go_plugin" do + before do + subject.add_go_plugin("foo", source: "http://localhost/foo.zip") + end + + it "should remove the installed plugin" do + subject.remove_go_plugin("foo") + expect(subject.installed_go_plugins).not_to include("foo") + end + + it "should remove plugin not installed" do + subject.remove_go_plugin("foo") + expect(subject.installed_go_plugins).not_to include("foo") + subject.remove_go_plugin("foo") + expect(subject.installed_go_plugins).not_to include("foo") + end + end + + describe "#has_go_plugin?" do + before do + subject.add_go_plugin("foo", source: "http://localhost/foo.zip") + end + + it "should return true when plugin is installed" do + expect(subject.has_go_plugin?("foo")).to be_truthy + end + + it "should return false when plugin is not installed" do + expect(subject.has_go_plugin?("fee")).to be_falsey + end + + it "should allow symbol names" do + expect(subject.has_go_plugin?(:foo)).to be_truthy + end + end + end end