summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsutosh Palai <asupalai@gmail.com>2016-07-21 02:32:25 +0530
committerAsutosh Palai <asupalai@gmail.com>2016-08-16 09:46:18 +0530
commit7ec9e457bd939f4565d35c5b064401933763d020 (patch)
tree45e0b6e9f731b7677937fb770c8d8b9d460d79d7
parent5977342e7ac4600051e7a38977a4e7c7cb420cc1 (diff)
downloadbundler-7ec9e457bd939f4565d35c5b064401933763d020.tar.gz
Added specs for hooks
-rw-r--r--lib/bundler/plugin.rb3
-rw-r--r--spec/bundler/plugin/api_spec.rb11
-rw-r--r--spec/bundler/plugin/index_spec.rb77
-rw-r--r--spec/bundler/plugin_spec.rb47
-rw-r--r--spec/plugins/hook_spec.rb2
-rw-r--r--spec/support/helpers.rb1
6 files changed, 107 insertions, 34 deletions
diff --git a/lib/bundler/plugin.rb b/lib/bundler/plugin.rb
index e55f0fcf1b..54a9ac33a4 100644
--- a/lib/bundler/plugin.rb
+++ b/lib/bundler/plugin.rb
@@ -159,12 +159,13 @@ module Bundler
Index.new.installed?(plugin)
end
- # Used by specs
def reset!
instance_variables.each {|i| remove_instance_variable(i) }
@sources = {}
@commands = {}
+ @hooks = {}
+ @loaded = []
end
# Post installation processing and registering with index
diff --git a/spec/bundler/plugin/api_spec.rb b/spec/bundler/plugin/api_spec.rb
index a227d31591..0eba52301a 100644
--- a/spec/bundler/plugin/api_spec.rb
+++ b/spec/bundler/plugin/api_spec.rb
@@ -38,6 +38,17 @@ describe Bundler::Plugin::API do
UserPluginClass.source "a_source", NewClass
end
end
+
+ describe "#hook" do
+ it "accepts a block and passes it to Plugin module" do
+ foo = double("tester")
+ expect(foo).to receive(:called)
+
+ expect(Bundler::Plugin).to receive(:add_hook).with("post-foo").and_yield
+
+ Bundler::Plugin::API.hook("post-foo") { foo.called }
+ end
+ end
end
context "bundler interfaces provided" do
diff --git a/spec/bundler/plugin/index_spec.rb b/spec/bundler/plugin/index_spec.rb
index 337182abf1..ffd18b2039 100644
--- a/spec/bundler/plugin/index_spec.rb
+++ b/spec/bundler/plugin/index_spec.rb
@@ -4,84 +4,94 @@ require "spec_helper"
describe Bundler::Plugin::Index do
Index = Bundler::Plugin::Index
- subject(:index) { Index.new }
-
before do
gemfile ""
+ path = lib_path(plugin_name)
+ index.register_plugin("new-plugin", path.to_s, [path.join("lib").to_s], commands, sources, hooks)
end
- describe "#register plugin" do
- before do
- path = lib_path("new-plugin")
- index.register_plugin("new-plugin", path.to_s, [path.join("lib").to_s], [], [])
- end
+ let(:plugin_name) { "new-plugin" }
+ let(:commands) { [] }
+ let(:sources) { [] }
+ let(:hooks) { [] }
+
+ subject(:index) { Index.new }
+ describe "#register plugin" do
it "is available for retrieval" do
- expect(index.plugin_path("new-plugin")).to eq(lib_path("new-plugin"))
+ expect(index.plugin_path(plugin_name)).to eq(lib_path(plugin_name))
end
it "load_paths is available for retrival" do
- expect(index.load_paths("new-plugin")).to eq([lib_path("new-plugin").join("lib").to_s])
+ expect(index.load_paths(plugin_name)).to eq([lib_path(plugin_name).join("lib").to_s])
end
it "is persistent" do
new_index = Index.new
- expect(new_index.plugin_path("new-plugin")).to eq(lib_path("new-plugin"))
+ expect(new_index.plugin_path(plugin_name)).to eq(lib_path(plugin_name))
end
it "load_paths are persistant" do
new_index = Index.new
- expect(new_index.load_paths("new-plugin")).to eq([lib_path("new-plugin").join("lib").to_s])
+ expect(new_index.load_paths(plugin_name)).to eq([lib_path(plugin_name).join("lib").to_s])
end
end
describe "commands" do
- before do
- path = lib_path("cplugin")
- index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["newco"], [])
- end
+ let(:commands) { ["newco"] }
it "returns the plugins name on query" do
- expect(index.command_plugin("newco")).to eq("cplugin")
+ expect(index.command_plugin("newco")).to eq(plugin_name)
end
it "raises error on conflict" do
expect do
- index.register_plugin("aplugin", lib_path("aplugin").to_s, lib_path("aplugin").join("lib").to_s, ["newco"], [])
+ index.register_plugin("aplugin", lib_path("aplugin").to_s, lib_path("aplugin").join("lib").to_s, ["newco"], [], [])
end.to raise_error(Index::CommandConflict)
end
it "is persistent" do
new_index = Index.new
- expect(new_index.command_plugin("newco")).to eq("cplugin")
+ expect(new_index.command_plugin("newco")).to eq(plugin_name)
end
end
describe "source" do
- before do
- path = lib_path("splugin")
- index.register_plugin("splugin", path.to_s, [path.join("lib").to_s], [], ["new_source"])
- end
+ let(:sources) { ["new_source"] }
it "returns the plugins name on query" do
- expect(index.source_plugin("new_source")).to eq("splugin")
+ expect(index.source_plugin("new_source")).to eq(plugin_name)
end
it "raises error on conflict" do
expect do
- index.register_plugin("aplugin", lib_path("aplugin").to_s, lib_path("aplugin").join("lib").to_s, [], ["new_source"])
+ index.register_plugin("aplugin", lib_path("aplugin").to_s, lib_path("aplugin").join("lib").to_s, [], ["new_source"], [])
end.to raise_error(Index::SourceConflict)
end
it "is persistent" do
new_index = Index.new
- expect(new_index.source_plugin("new_source")).to eq("splugin")
+ expect(new_index.source_plugin("new_source")).to eq(plugin_name)
+ end
+ end
+
+ describe "hook" do
+ let(:hooks) { ["before-foo", "after-bar"] }
+
+ it "returns the plugins name on query" do
+ expect(index.hook_plugins("after-bar")).to include(plugin_name)
+ end
+
+ it "is persistent" do
+ new_index = Index.new
+ expect(new_index.hook_plugins("after-bar")).to eq([plugin_name])
end
end
describe "global index" do
before do
Dir.chdir(tmp) do
+ Bundler::Plugin.reset!
path = lib_path("gplugin")
index.register_plugin("gplugin", path.to_s, [path.join("lib").to_s], [], ["glb_source"])
end
@@ -94,10 +104,9 @@ describe Bundler::Plugin::Index do
end
describe "after conflict" do
- before do
- path = lib_path("aplugin")
- index.register_plugin("aplugin", path.to_s, [path.join("lib").to_s], ["foo"], ["bar"])
- end
+ let(:commands) { ["foo"] }
+ let(:sources) { ["bar"] }
+ let(:hooks) { ["hoook"] }
shared_examples "it cleans up" do
it "the path" do
@@ -111,13 +120,17 @@ describe Bundler::Plugin::Index do
it "the source" do
expect(index.source_plugin("xbar")).to be_falsy
end
+
+ it "the hook" do
+ expect(index.hook_plugins("xhoook")).to be_falsy
+ end
end
context "on command conflict it cleans up" do
before do
expect do
path = lib_path("cplugin")
- index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["foo"], ["xbar"])
+ index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["foo"], ["xbar"], ["xhoook"])
end.to raise_error(Index::CommandConflict)
end
@@ -128,7 +141,7 @@ describe Bundler::Plugin::Index do
before do
expect do
path = lib_path("cplugin")
- index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["xfoo"], ["bar"])
+ index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["xfoo"], ["bar"], ["xhoook"])
end.to raise_error(Index::SourceConflict)
end
@@ -139,7 +152,7 @@ describe Bundler::Plugin::Index do
before do
expect do
path = lib_path("cplugin")
- index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["foo"], ["bar"])
+ index.register_plugin("cplugin", path.to_s, [path.join("lib").to_s], ["foo"], ["bar"], ["xhoook"])
end.to raise_error(Index::CommandConflict)
end
diff --git a/spec/bundler/plugin_spec.rb b/spec/bundler/plugin_spec.rb
index f6f10201b3..ab0bbf4095 100644
--- a/spec/bundler/plugin_spec.rb
+++ b/spec/bundler/plugin_spec.rb
@@ -228,4 +228,51 @@ describe Bundler::Plugin do
end
end
end
+
+ describe "#hook" do
+ before do
+ path = lib_path("foo-plugin")
+ build_lib "foo-plugin", :path => path do |s|
+ s.write "plugins.rb", code
+ end
+
+ allow(index).to receive(:hook_plugins).with(event).
+ and_return(["foo-plugin"])
+ allow(index).to receive(:plugin_path).with("foo-plugin").and_return(path)
+ allow(index).to receive(:load_paths).with("foo-plugin").and_return([])
+ end
+
+ let(:code) { <<-RUBY }
+ Bundler::Plugin::API.hook("event-1") { puts "hook for event 1" }
+ RUBY
+
+ let(:event) { "event-1" }
+
+ it "executes the hook" do
+ out = capture_output do
+ Plugin.hook("event-1")
+ end.strip
+
+ expect(out).to eq("hook for event 1")
+ end
+
+ context "single plugin declaring more than one hook" do
+ let(:code) { <<-RUBY }
+ Bundler::Plugin::API.hook("event-1") {}
+ Bundler::Plugin::API.hook("event-2") {}
+ puts "loaded"
+ RUBY
+
+ let(:event) { /event-1|event-2/ }
+
+ it "evals plugins.rb once" do
+ out = capture_output do
+ Plugin.hook("event-1")
+ Plugin.hook("event-2")
+ end.strip
+
+ expect(out).to eq("loaded")
+ end
+ end
+ end
end
diff --git a/spec/plugins/hook_spec.rb b/spec/plugins/hook_spec.rb
index 3622d2a6e9..44ceab232d 100644
--- a/spec/plugins/hook_spec.rb
+++ b/spec/plugins/hook_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require "spec_helper"
-describe "hook plugins", :focused do
+describe "hook plugins" do
before do
build_repo2 do
build_plugin "before-install-plugin" do |s|
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb
index fe79604f30..426b492da2 100644
--- a/spec/support/helpers.rb
+++ b/spec/support/helpers.rb
@@ -16,6 +16,7 @@ module Spec
Bundler.reset!
Bundler.ui = nil
Bundler.ui # force it to initialize
+ Plugin.reset!
end
def self.bang(method)