summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsutosh Palai <asupalai@gmail.com>2016-06-07 11:13:46 +0530
committerAsutosh Palai <asupalai@gmail.com>2016-06-07 12:40:19 +0530
commitd4a9f2e7b0fc332d843ce6835c2b29bdae1e9184 (patch)
tree1480b1371c6f28022058e9284e1524032337a5ca
parent5a30dc76ba27fa12d6ffd56b18cd8e022e69d07e (diff)
downloadbundler-d4a9f2e7b0fc332d843ce6835c2b29bdae1e9184.tar.gz
Added unit test form plugin module
-rw-r--r--lib/bundler/plugin.rb8
-rw-r--r--spec/bundler/plugin/api_spec.rb (renamed from spec/plugins/api.rb)12
-rw-r--r--spec/bundler/plugin_spec.rb106
3 files changed, 115 insertions, 11 deletions
diff --git a/lib/bundler/plugin.rb b/lib/bundler/plugin.rb
index 8de8dab06f..7e1f616956 100644
--- a/lib/bundler/plugin.rb
+++ b/lib/bundler/plugin.rb
@@ -30,7 +30,7 @@ module Bundler
register_plugin name, plugin_path
Bundler.ui.info "Installed plugin #{name}"
- rescue StandardError => e
+ rescue PluginError => e
Bundler.rm_rf(plugin_path) if plugin_path
Bundler.ui.error "Failed to install plugin #{name}: #{e.message}\n #{e.backtrace.join("\n ")}"
end
@@ -111,7 +111,11 @@ module Bundler
@commands = {}
- load path.join(PLUGIN_FILE_NAME), true
+ begin
+ load path.join(PLUGIN_FILE_NAME), true
+ rescue StandardError => e
+ raise MalformattedPlugin, "#{e.class}: #{e.message}"
+ end
index.register_plugin name, path.to_s, @commands.keys
ensure
diff --git a/spec/plugins/api.rb b/spec/bundler/plugin/api_spec.rb
index e9bb81201a..59f845e5cb 100644
--- a/spec/plugins/api.rb
+++ b/spec/bundler/plugin/api_spec.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true
require "spec_helper"
-require "bundler/plugin"
-
describe Bundler::Plugin::API do
context "plugin declarations" do
before do
@@ -10,21 +8,17 @@ describe Bundler::Plugin::API do
end
it "declares a command plugin with same class as handler" do
- success = false
- allow(Bundler::Plugin).to receive(:add_command).with("meh", UserPluginClass) { success = true }
+ allow(Bundler::Plugin).
+ to receive(:add_command).with("meh", UserPluginClass).once
UserPluginClass.command "meh"
-
- expect(success).to be true
end
it "accepts another class as argument that handles the command" do
- success = false
stub_const "NewClass", Class.new
- allow(Bundler::Plugin).to receive(:add_command).with("meh", NewClass) { success = true }
+ allow(Bundler::Plugin).to receive(:add_command).with("meh", NewClass).once
UserPluginClass.command "meh", NewClass
- expect(success).to be true
end
end
diff --git a/spec/bundler/plugin_spec.rb b/spec/bundler/plugin_spec.rb
new file mode 100644
index 0000000000..76d4261d50
--- /dev/null
+++ b/spec/bundler/plugin_spec.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+require "spec_helper"
+
+describe Bundler::Plugin do
+ Plugin = Bundler::Plugin
+
+ let(:installer) { double(:installer) }
+ let(:index) { double(:index) }
+
+ before do
+ build_lib "new-plugin", :path => lib_path("new-plugin") do |s|
+ s.write "plugins.rb"
+ end
+
+ build_lib "another-plugin", :path => lib_path("another-plugin") do |s|
+ s.write "plugins.rb"
+ end
+
+ allow(Plugin::Installer).to receive(:new) { installer }
+ allow(Plugin).to receive(:index) { index }
+ allow(index).to receive(:register_plugin)
+ end
+
+ describe "install command" do
+ let(:opts) {{"version" => "~> 1.0", "source" => "foo"}}
+
+ before do
+ allow(installer).
+ to receive(:install).with("new-plugin", opts) {lib_path("new-plugin")}
+ end
+
+ it "passes the name and options to installer" do
+ allow(installer).to receive(:install).with("new-plugin", opts) do
+ lib_path("new-plugin")
+ end.once
+
+ subject.install "new-plugin", opts
+ end
+
+ it "validates the installed plugin" do
+ allow(subject).
+ to receive(:validate_plugin!).with(lib_path("new-plugin")).once
+
+ subject.install "new-plugin", opts
+ end
+
+ it "registers the plugin with index" do
+ allow(index).to receive(:register_plugin).
+ with("new-plugin", lib_path("new-plugin").to_s, []).once
+ subject.install "new-plugin", opts
+ end
+ end
+
+ describe "evaluate gemfile for plugins" do
+ let(:definition) { double("definition") }
+ let(:gemfile) { bundled_app("Gemfile") }
+
+ before do
+ allow(Plugin::DSL).to receive(:evaluate) { definition }
+ end
+
+ it "doesn't calls installer without any plugins" do
+ allow(definition).to receive(:dependencies) { [] }
+ allow(installer).to receive(:install_definition).never
+
+ subject.eval_gemfile(gemfile)
+ end
+
+ it "should validate and register the plugins" do
+ allow(definition).to receive(:dependencies) { [1, 2] }
+ plugin_paths = {
+ "new-plugin" => lib_path("new-plugin"),
+ "another-plugin" => lib_path("another-plugin"),
+ }
+ allow(installer).to receive(:install_definition) {plugin_paths}
+
+ expect(subject).to receive(:validate_plugin!).twice
+ expect(subject).to receive(:register_plugin).twice
+
+ subject.eval_gemfile(gemfile)
+ end
+ end
+
+ describe "#command?" do
+ it "returns true value for commands in index" do
+ allow(index).
+ to receive(:command_plugin).with("newcommand") { "my-plugin" }
+ result = subject.command? "newcommand"
+ expect(result).to be_truthy
+ end
+
+ it "returns false value for commands not in index" do
+ allow(index).to receive(:command_plugin).with("newcommand") { nil }
+ result = subject.command? "newcommand"
+ expect(result).to be_falsy
+ end
+ end
+
+ describe "#exec_command" do
+ it "raises UndefinedCommandError when command is not found" do
+ allow(index).to receive(:command_plugin).with("newcommand") { nil }
+ expect { subject.exec_command("newcommand", []) }.
+ to raise_error(Plugin::UndefinedCommandError)
+ end
+ end
+end