summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/interface.rb10
-rwxr-xr-x[-rw-r--r--]spec/unit/interface_spec.rb27
2 files changed, 32 insertions, 5 deletions
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
index dfd75ef58..d169067ea 100644
--- a/lib/puppet/interface.rb
+++ b/lib/puppet/interface.rb
@@ -55,12 +55,12 @@ class Puppet::Interface
remove_const(constantize(name)) rescue nil
end
- def self.unify_name(name)
- name.to_s.downcase.to_sym
- end
-
def self.constantize(name)
- name.to_s.split(/\W|_/).map { |x| x.capitalize }.join
+ unless name.to_s =~ /^[-_a-z]+$/i then
+ raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid interface name"
+ end
+
+ name.to_s.split(/[-_]/).map { |x| x.capitalize }.join
end
attr_accessor :default_format
diff --git a/spec/unit/interface_spec.rb b/spec/unit/interface_spec.rb
index 4b6fd117f..cfa0111f6 100644..100755
--- a/spec/unit/interface_spec.rb
+++ b/spec/unit/interface_spec.rb
@@ -92,4 +92,31 @@ describe Puppet::Interface do
end
it "should be able to load all actions in all search paths"
+
+ describe "#constantize" do
+ faulty = [1, "#foo", "$bar", "sturm und drang", :"sturm und drang"]
+ valid = {
+ "foo" => "Foo",
+ :foo => "Foo",
+ "foo_bar" => "FooBar",
+ :foo_bar => "FooBar",
+ "foo-bar" => "FooBar",
+ :"foo-bar" => "FooBar",
+ }
+
+ valid.each do |input, expect|
+ it "should map '#{input}' to '#{expect}'" do
+ result = Puppet::Interface.constantize(input)
+ result.should be_a String
+ result.to_s.should == expect
+ end
+ end
+
+ faulty.each do |input|
+ it "should fail when presented with #{input.inspect} (#{input.class})" do
+ expect { Puppet::Interface.constantize(input) }.
+ should raise_error ArgumentError, /not a valid interface name/
+ end
+ end
+ end
end