Test command name validation and fix up some bugs

This commit is contained in:
Mitchell Hashimoto 2012-05-05 20:11:26 -07:00
parent e4fa5bb489
commit c2649074c3
2 changed files with 17 additions and 6 deletions

View File

@ -73,13 +73,15 @@ module Vagrant
def self.command(name=UNSET_VALUE, &block) def self.command(name=UNSET_VALUE, &block)
data[:command] ||= Registry.new data[:command] ||= Registry.new
if name != UNSET_VALUE
# Validate the name of the command # Validate the name of the command
if name.to_s !~ /^[-a-z0-9]/i if name.to_s !~ /^[-a-z0-9]+$/i
raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens" raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens"
end end
# Register a new command class only if a name was given. # Register a new command class only if a name was given.
data[:command].register(name.to_sym, &block) if name != UNSET_VALUE data[:command].register(name.to_sym, &block)
end
# Return the registry # Return the registry
data[:command] data[:command]

View File

@ -44,6 +44,15 @@ describe Vagrant::Plugin::V1 do
plugin.command[:foo].should == "bar" plugin.command[:foo].should == "bar"
end end
["spaces bad", "sym^bols"].each do |bad|
it "should not allow bad command name: #{bad}" do
plugin = Class.new(described_class)
expect { plugin.command(bad) {} }.
to raise_error(Vagrant::Plugin::V1::InvalidCommandName)
end
end
it "should lazily register command classes" do it "should lazily register command classes" do
# Below would raise an error if the value of the command class was # Below would raise an error if the value of the command class was
# evaluated immediately. By asserting that this does not raise an # evaluated immediately. By asserting that this does not raise an