From c2649074c3166d7556023249d1d2aa9617452359 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 5 May 2012 20:11:26 -0700 Subject: [PATCH] Test command name validation and fix up some bugs --- lib/vagrant/plugin/v1.rb | 14 ++++++++------ test/unit/vagrant/plugin/v1_test.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index aa09e5ff2..3c81174e4 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -73,13 +73,15 @@ module Vagrant def self.command(name=UNSET_VALUE, &block) data[:command] ||= Registry.new - # Validate the name of the command - if name.to_s !~ /^[-a-z0-9]/i - raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens" - end + if name != UNSET_VALUE + # Validate the name of the command + if name.to_s !~ /^[-a-z0-9]+$/i + raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens" + end - # Register a new command class only if a name was given. - data[:command].register(name.to_sym, &block) if name != UNSET_VALUE + # Register a new command class only if a name was given. + data[:command].register(name.to_sym, &block) + end # Return the registry data[:command] diff --git a/test/unit/vagrant/plugin/v1_test.rb b/test/unit/vagrant/plugin/v1_test.rb index a73a9246f..b4a477cf2 100644 --- a/test/unit/vagrant/plugin/v1_test.rb +++ b/test/unit/vagrant/plugin/v1_test.rb @@ -44,6 +44,15 @@ describe Vagrant::Plugin::V1 do plugin.command[:foo].should == "bar" 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 # Below would raise an error if the value of the command class was # evaluated immediately. By asserting that this does not raise an