require_relative "../../helper" require_relative "../../../lib/fabriq/logging" require_relative "../../../lib/fabriq/plugin_port" describe Fabriq::PluginPort do before do @port = Fabriq::PluginPort end describe '#plugin_load_paths' do before do Dir.stubs(:glob) File.stubs(:expand_path).returns("/home") end it "returns all plugins in HOME/.fabriq/plugins" do Dir.expects(:glob).with("/home/.fabriq/plugins/*").returns( ["plugin_dir"] ) @port.plugin_load_paths.must_include("plugin_dir") end it "returns all plugins in current run path/plugins" do Dir.stubs(:pwd).returns("/path/pwd") Dir.expects(:glob).with("/path/pwd/plugins/*").returns( ["plugin_dir"] ) @port.plugin_load_paths.must_include("plugin_dir") end end describe '#require_plugins' do before do File.stubs(:exist?).returns(true) end it "requires all plugins from path" do @port.stubs(:plugin_load_paths).returns(["plugin/path"]) @port.expects(:require_plugin_from_path).with("plugin/path") @port.require_plugins end it "skips plugin paths that doesn't exist" do File.stubs(:exist?).returns(false) @port.stubs(:plugin_load_paths).returns(["plugin/path"]) @port.expects(:require_plugin_from_path).never @port.require_plugins end end describe '#require_plugin_from_path' do before do File.stubs(:exist?).returns(true) end it "requires the plugins main file" do @port.expects(:require).with("/plugin/maja_calendar/lib/maja_calendar.rb") @port.require_plugin_from_path("/plugin/maja_calendar") end it "logs an error message if the plugin's main file could not be found" do File.stubs(:exist?).returns(false) @port.expects(:error) @port.require_plugin_from_path("/plugin/maja_calendar") end end describe '#register_plugin' do before do @plugin_klass = mock('Plugin Klass') @plugin_instance = mock('Plugin Instance') @plugin_klass.stubs(:init).returns(@plugin_instance) @port.stubs(:invoke_plugin_callback) end it "inits the plugin" do @plugin_klass.expects(:init) @port.register_plugin(@plugin_klass) end it "appends the initialized plugin to the plugins collection" do @port.register_plugin(@plugin_klass) @port.plugins.must_include(@plugin_instance) end it "invokes the plugin configuration, passing the plugins configurator object" do configurator = mock('configurator') config_proc = -> {} @plugin_instance.stubs(:configurator).returns(configurator) config_proc.expects(:call).with(configurator) @port.register_plugin(@plugin_klass, &config_proc) end it "sends the 'registered' event to the plugin" do @port.expects(:invoke_plugin_callback).with(@plugin_instance, :registered) @port.register_plugin(@plugin_klass) end end describe '#initialize_plugins' do it "sends the 'initialized' event to each registered plugin" do plugin = mock('Plugin') @port.plugins = [ plugin ] @port.expects(:invoke_plugin_callback).with(plugin, :initialized) @port.initialize_plugins end end end