From b1621fbc91587ae46f2ea0dc3be4e6b0d4858b48 Mon Sep 17 00:00:00 2001 From: Asutosh Palai Date: Tue, 31 May 2016 17:55:18 +0530 Subject: Added spec for plugin api --- lib/bundler/plugin.rb | 4 +++- lib/bundler/plugin/api.rb | 15 ++++++++++----- lib/bundler/plugin/index.rb | 2 +- spec/plugins/api.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ spec/plugins/command.rb | 4 ++-- 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 spec/plugins/api.rb diff --git a/lib/bundler/plugin.rb b/lib/bundler/plugin.rb index fb577ad6bc..c888dbcd97 100644 --- a/lib/bundler/plugin.rb +++ b/lib/bundler/plugin.rb @@ -92,7 +92,6 @@ module Bundler # At present it only checks whether it contains plugin.rb file # # @param [Pathname] plugin_path the path plugin is installed at - # # @raise [Error] if plugin.rb file is not found def validate_plugin!(plugin_path) plugin_file = plugin_path.join(PLUGIN_FILE_NAME) @@ -116,6 +115,9 @@ module Bundler @commands = commands end + # Executes the plugin.rb file + # + # @param [String] name of the plugin def load_plugin(name) # Need to ensure before this that plugin root where the rest of gems # are installed to be on load path to support plugin deps. Currently not diff --git a/lib/bundler/plugin/api.rb b/lib/bundler/plugin/api.rb index 93b17fd8e8..ea9ff1ea12 100644 --- a/lib/bundler/plugin/api.rb +++ b/lib/bundler/plugin/api.rb @@ -8,6 +8,13 @@ module Bundler # interactions to methods of this class only. This will save them from breaking # when some internal change. # + # Currently we are delegating the methods defined in Bundler class to + # itself. So, this class acts as a buffer. + # + # If there is some change in the Bundler class that is incompatible to its + # previous behavior or if otherwise desired, we can reimplement(or implement) + # the method to preserve compatibility. + # # To use this, either the class can inherit this class or use it directly. # For example of both types of use, refer the file `spec/plugins/command.rb` # @@ -39,11 +46,9 @@ module Bundler Bundler.tmp(File.join(["plugin", name])) end - # The bundler settings - # - # @return [Bundler::Setting] setting object of bundler - def settings - Bundler.settings + def method_missing(name, *args, &blk) + super unless Bundler.respond_to?(name) + Bundler.send(name, *args, &blk) end end end diff --git a/lib/bundler/plugin/index.rb b/lib/bundler/plugin/index.rb index 5db8662bdc..26ba4ae3e2 100644 --- a/lib/bundler/plugin/index.rb +++ b/lib/bundler/plugin/index.rb @@ -24,7 +24,7 @@ module Bundler common = commands & @commands.keys raise "Command(s) #{common.join(", ")} are already registered" if common.any? - commands.each{|c| @commands[c] = name } + commands.each {|c| @commands[c] = name } save_index end diff --git a/spec/plugins/api.rb b/spec/plugins/api.rb new file mode 100644 index 0000000000..8fd5689227 --- /dev/null +++ b/spec/plugins/api.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true +require "spec_helper" + +require "bundler/plugin" + +describe Bundler::Plugin::Api do + context "plugin declarations" do + before do + stub_const "UserPluginClass", Class.new(Bundler::Plugin::Api) + 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 } + + 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 } + + UserPluginClass.command "meh", NewClass + expect(success).to be true + end + end + + context "bundler interfaces provided" do + before do + stub_const "UserPluginClass", Class.new(Bundler::Plugin::Api) + end + + subject(:api) { UserPluginClass.new } + + # A test of delegation + it "provides the bundler settings" do + expect(api.settings).to eq(Bundler.settings) + end + end +end diff --git a/spec/plugins/command.rb b/spec/plugins/command.rb index 646f95c733..5e3d8217bd 100644 --- a/spec/plugins/command.rb +++ b/spec/plugins/command.rb @@ -4,7 +4,7 @@ require "spec_helper" describe "command plugins" do it "executes without arguments" do build_repo2 do - build_plugin "command-mah"do |s| + build_plugin "command-mah" do |s| s.write "plugin.rb", <<-RUBY module Mah class Plugin < Bundler::Plugin::Api @@ -28,7 +28,7 @@ describe "command plugins" do it "accepts the arguments" do build_repo2 do - build_plugin "the-echoer"do |s| + build_plugin "the-echoer" do |s| s.write "plugin.rb", <<-RUBY module Resonance class Echoer -- cgit v1.2.1