1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

unit tests for extensions mechanism, and a bit more robustness

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6438 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2007-03-16 22:11:45 +00:00
parent 55e194c3f0
commit 4c58afbe6e
2 changed files with 87 additions and 2 deletions

View file

@ -1,5 +1,5 @@
module Capistrano
class ExtensionProxy
class ExtensionProxy #:nodoc:
def initialize(config, mod)
@config = config
extend(mod)
@ -10,11 +10,25 @@ module Capistrano
end
end
# Holds the set of registered plugins, keyed by name (where the name is a
# symbol).
EXTENSIONS = {}
# Register the given module as a plugin with the given name. It will henceforth
# be available via a proxy object on Configuration instances, accessible by
# a method with the given name.
def self.plugin(name, mod)
name = name.to_sym
return false if EXTENSIONS.has_key?(name)
methods = Capistrano::Configuration.public_instance_methods +
Capistrano::Configuration.protected_instance_methods +
Capistrano::Configuration.private_instance_methods
if methods.include?(name.to_s)
raise Capistrano::Error, "registering a plugin named `#{name}' would shadow a method on Capistrano::Configuration with the same name"
end
Capistrano::Configuration.class_eval <<-STR, __FILE__, __LINE__+1
def #{name}
@__#{name}_proxy ||= Capistrano::ExtensionProxy.new(self, Capistrano::EXTENSIONS[#{name.inspect}])
@ -25,7 +39,9 @@ module Capistrano
return true
end
# Unregister the plugin with the given name.
def self.remove_plugin(name)
name = name.to_sym
if EXTENSIONS.delete(name)
Capistrano::Configuration.send(:remove_method, name)
return true
@ -34,7 +50,7 @@ module Capistrano
return false
end
def self.configuration(*args)
def self.configuration(*args) #:nodoc:
warn "[DEPRECATION] Capistrano.configuration is deprecated. Use Capistrano::Configuration.instance instead"
Capistrano::Configuration.instance(*args)
end

69
test/extensions_test.rb Normal file
View file

@ -0,0 +1,69 @@
require "#{File.dirname(__FILE__)}/utils"
require 'capistrano'
class ExtensionsTest < Test::Unit::TestCase
module CustomExtension
def do_something(command)
run(command)
end
end
def setup
@config = Capistrano::Configuration.new
end
def teardown
Capistrano::EXTENSIONS.keys.each { |e| Capistrano.remove_plugin(e) }
end
def test_register_plugin_should_add_instance_method_on_configuration_and_return_true
assert !@config.respond_to?(:custom_stuff)
assert Capistrano.plugin(:custom_stuff, CustomExtension)
assert @config.respond_to?(:custom_stuff)
end
def test_register_plugin_that_already_exists_should_return_false
assert Capistrano.plugin(:custom_stuff, CustomExtension)
assert !Capistrano.plugin(:custom_stuff, CustomExtension)
end
def test_register_plugin_with_public_method_name_should_fail
method = Capistrano::Configuration.public_instance_methods.first
assert_not_nil method, "need a public instance method for testing"
assert_raises(Capistrano::Error) { Capistrano.plugin(method, CustomExtension) }
end
def test_register_plugin_with_protected_method_name_should_fail
method = Capistrano::Configuration.protected_instance_methods.first
assert_not_nil method, "need a protected instance method for testing"
assert_raises(Capistrano::Error) { Capistrano.plugin(method, CustomExtension) }
end
def test_register_plugin_with_private_method_name_should_fail
method = Capistrano::Configuration.private_instance_methods.first
assert_not_nil method, "need a private instance method for testing"
assert_raises(Capistrano::Error) { Capistrano.plugin(method, CustomExtension) }
end
def test_unregister_plugin_that_does_not_exist_should_return_false
assert !Capistrano.remove_plugin(:custom_stuff)
end
def test_unregister_plugin_should_remove_method_and_return_true
assert Capistrano.plugin(:custom_stuff, CustomExtension)
assert @config.respond_to?(:custom_stuff)
assert Capistrano.remove_plugin(:custom_stuff)
assert !@config.respond_to?(:custom_stuff)
end
def test_registered_plugin_proxy_should_return_proxy_object
Capistrano.plugin(:custom_stuff, CustomExtension)
assert_instance_of Capistrano::ExtensionProxy, @config.custom_stuff
end
def test_proxy_object_should_delegate_to_configuration
Capistrano.plugin(:custom_stuff, CustomExtension)
@config.expects(:run).with("hello")
@config.custom_stuff.do_something("hello")
end
end