diff options
-rw-r--r-- | lib/puppet/interface.rb | 29 | ||||
-rw-r--r-- | spec/unit/interface_spec.rb | 22 |
2 files changed, 18 insertions, 33 deletions
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb index 476de8bbf..f9b26950a 100644 --- a/lib/puppet/interface.rb +++ b/lib/puppet/interface.rb @@ -6,8 +6,10 @@ class Puppet::Interface include Puppet::Interface::ActionManager extend Puppet::Interface::ActionManager + # This is just so we can search for actions. We only use its # list of directories to search. + # Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb def self.autoloader @autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/interface") end @@ -30,35 +32,22 @@ class Puppet::Interface end end end - @interfaces.keys + Puppet::Interface.constants.map { |c| c.to_s.downcase } end - # Return an interface by name, loading from disk if necessary. - def self.interface(name) - @interfaces ||= {} - unless @interfaces[unify_name(name)] - require "puppet/interface/#{unify_name(name)}" - end - @interfaces[unify_name(name)] - rescue Exception => detail - puts detail.backtrace if Puppet[:trace] - $stderr.puts "Unable to find interface '#{name.to_s}': #{detail}." + def self.const_missing(name) + require "puppet/interface/#{name.to_s.downcase}" + const_get(name) if const_defined?(name) + rescue LoadError + nil end def self.register_interface(name, instance) - @interfaces ||= {} - @interfaces[unify_name(name)] = instance const_set(name2const(name), instance) end def self.unload_interface(name) - @interfaces ||= {} - @interfaces.delete(unify_name(name)) - const = name2const(name) - const_get(const) - remove_const(const) - rescue - # nothing - if the constant-getting fails, just return + remove_const(name2const(name)) rescue nil end def self.unify_name(name) diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb index 8799d6b74..4b6fd117f 100644 --- a/spec/unit/interface_spec.rb +++ b/spec/unit/interface_spec.rb @@ -14,7 +14,9 @@ describe Puppet::Interface do end it "should register itself" do - Puppet::Interface.expects(:register_interface).with { |name, inst| name == :me and inst.is_a?(Puppet::Interface) } + Puppet::Interface.expects(:register_interface).with do |name, inst| + name == :me and inst.is_a?(Puppet::Interface) + end Puppet::Interface.new(:me) end @@ -46,6 +48,7 @@ describe Puppet::Interface do Puppet::Interface.new(:me).default_format.should == :pson end + # Why? it "should create a class-level autoloader" do Puppet::Interface.autoloader.should be_instance_of(Puppet::Util::Autoload) end @@ -54,14 +57,6 @@ describe Puppet::Interface do Puppet::Interface.new(:me, :verb => "foo").verb.should == "foo" end - it "should be able to register and return interfaces" do - $stderr.stubs(:puts) - face = Puppet::Interface.new(:me) - Puppet::Interface.unload_interface(:me) # to remove from the initial registration - Puppet::Interface.register_interface(:me, face) - Puppet::Interface.interface(:me).should equal(face) - end - it "should create an associated constant when registering an interface" do $stderr.stubs(:puts) face = Puppet::Interface.new(:me) @@ -70,29 +65,30 @@ describe Puppet::Interface do Puppet::Interface::Me.should equal(face) end + # Why is unloading interfaces important? it "should be able to unload interfaces" do $stderr.stubs(:puts) face = Puppet::Interface.new(:me) Puppet::Interface.unload_interface(:me) - Puppet::Interface.interface(:me).should be_nil + Puppet::Interface.const_defined?(:Me).should be_false end it "should remove the associated constant when an interface is unregistered" do $stderr.stubs(:puts) face = Puppet::Interface.new(:me) Puppet::Interface.unload_interface(:me) - lambda { Puppet::Interface.const_get("Me") }.should raise_error(NameError) + Puppet::Interface.const_defined?("Me").should be_false end it "should try to require interfaces that are not known" do Puppet::Interface.expects(:require).with "puppet/interface/foo" - Puppet::Interface.interface(:foo) + Puppet::Interface.const_get(:Foo) end it "should not fail when requiring an interface fails" do $stderr.stubs(:puts) Puppet::Interface.expects(:require).with("puppet/interface/foo").raises LoadError - lambda { Puppet::Interface.interface(:foo) }.should_not raise_error + lambda { Puppet::Interface::Foo }.should_not raise_error end it "should be able to load all actions in all search paths" |