From 1eef75a71592e7b4e08801c1d870e7f0f5e23f0d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 5 Jan 2014 08:42:34 -0800 Subject: [PATCH] commands/plugin: list uses PluginManager --- lib/vagrant.rb | 2 +- lib/vagrant/plugin.rb | 1 + lib/vagrant/plugin/manager.rb | 39 +++++++++++++ lib/vagrant/plugin_manager.rb | 16 ------ .../commands/plugin/action/list_plugins.rb | 4 +- test/unit/vagrant/plugin/manager_test.rb | 56 +++++++++++++++++++ 6 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 lib/vagrant/plugin/manager.rb delete mode 100644 lib/vagrant/plugin_manager.rb create mode 100644 test/unit/vagrant/plugin/manager_test.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 2458137e0..c6c0fe904 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -19,7 +19,7 @@ if Vagrant.plugins_enabled? # Initialize Bundler before we load _any_ RubyGems. require_relative "vagrant/bundler" require_relative "vagrant/plugin_manager" - Vagrant::Bundler.instance.init!(Vagrant::PluginManager.plugins) + Vagrant::Bundler.instance.init!(Vagrant::Plugin::Manager.instance.installed_plugins) end # Initialize Vagrant now that our Gem paths are setup diff --git a/lib/vagrant/plugin.rb b/lib/vagrant/plugin.rb index 71d0073bd..66dcbdbec 100644 --- a/lib/vagrant/plugin.rb +++ b/lib/vagrant/plugin.rb @@ -2,6 +2,7 @@ module Vagrant module Plugin autoload :V1, "vagrant/plugin/v1" autoload :V2, "vagrant/plugin/v2" + autoload :Manager, "vagrant/plugin/manager" autoload :StateFile, "vagrant/plugin/state_file" end end diff --git a/lib/vagrant/plugin/manager.rb b/lib/vagrant/plugin/manager.rb new file mode 100644 index 000000000..da5704c7f --- /dev/null +++ b/lib/vagrant/plugin/manager.rb @@ -0,0 +1,39 @@ +require_relative "../shared_helpers" + +module Vagrant + module Plugin + # The Manager helps with installing, listing, and initializing plugins. + class Manager + # Returns the path to the [StateFile] for global plugins. + # + # @return [Pathname] + def self.global_plugins_file + Vagrant.user_data_path.join("plugins.json") + end + + def self.instance + @instance ||= self.new(global_plugins_file) + end + + # @param [Pathname] global_file + def initialize(global_file) + @global_file = StateFile.new(global_file) + end + + # This returns the list of plugins that should be enabled. + # + # @return [Array] + def installed_plugins + @global_file.installed_plugins.keys + end + + # This returns the list of plugins that are installed as + # Gem::Specifications. + # + # @return [Array] + def installed_specs + ::Bundler.load.specs + end + end + end +end diff --git a/lib/vagrant/plugin_manager.rb b/lib/vagrant/plugin_manager.rb deleted file mode 100644 index 2887dac19..000000000 --- a/lib/vagrant/plugin_manager.rb +++ /dev/null @@ -1,16 +0,0 @@ -require "json" - -require_relative "shared_helpers" - -module Vagrant - class PluginManager - def self.global_plugins_file - Vagrant.user_data_path.join("plugins.json") - end - - def self.plugins - plugins = JSON.parse(global_plugins_file.read) - plugins["installed"].keys - end - end -end diff --git a/plugins/commands/plugin/action/list_plugins.rb b/plugins/commands/plugin/action/list_plugins.rb index b446b323a..8c173e6b3 100644 --- a/plugins/commands/plugin/action/list_plugins.rb +++ b/plugins/commands/plugin/action/list_plugins.rb @@ -1,3 +1,5 @@ +require "vagrant/plugin/manager" + module VagrantPlugins module CommandPlugin module Action @@ -20,7 +22,7 @@ module VagrantPlugins # Go through the plugins installed in this environment and # get the latest version of each. installed_map = {} - Bundler.load.specs.each do |spec| + Vagrant::Plugin::Manager.instance.installed_specs.each do |spec| # Ignore specs that aren't in our installed list next if !installed.include?(spec.name) diff --git a/test/unit/vagrant/plugin/manager_test.rb b/test/unit/vagrant/plugin/manager_test.rb new file mode 100644 index 000000000..d886656f1 --- /dev/null +++ b/test/unit/vagrant/plugin/manager_test.rb @@ -0,0 +1,56 @@ +require "json" +require "pathname" + +require "vagrant/plugin" +require "vagrant/plugin/manager" +require "vagrant/plugin/state_file" + +require File.expand_path("../../../base", __FILE__) + +describe Vagrant::Plugin::Manager do + let(:path) do + f = Tempfile.new("vagrant") + p = f.path + f.close + f.unlink + Pathname.new(p) + end + + after do + path.unlink if path.file? + end + + subject { described_class.new(path) } + + context "without state" do + describe "#installed_plugins" do + it "is empty initially" do + expect(subject.installed_plugins).to be_empty + end + end + end + + + context "with state" do + before do + sf = Vagrant::Plugin::StateFile.new(path) + sf.add_plugin("foo") + end + + describe "#installed_plugins" do + it "has the plugins" do + expect(subject.installed_plugins).to eql(["foo"]) + end + end + + describe "#installed_specs" do + it "has the plugins" do + runtime = double("runtime") + runtime.stub(specs: ["foo"]) + ::Bundler.stub(:load => runtime) + + expect(subject.installed_specs).to eql(["foo"]) + end + end + end +end