diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 11:04:17 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 13:32:04 -0700 |
| commit | 5a0b547f3289cb8e13b197d021322e03d05bee8e (patch) | |
| tree | d73d52ff19d327025d993ec88922d857dd5c22e5 /spec/unit/application/string_base_spec.rb | |
| parent | 8b37d7038c89bd830b076e838686419ff0068b56 (diff) | |
| download | puppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.tar.gz puppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.tar.xz puppet-5a0b547f3289cb8e13b197d021322e03d05bee8e.zip | |
(#6749) Fix optional vs mandatory argument handling.
optparse will treat '--foo --bar' as "foo with the argument --bar" when foo
takes a mandatory argument. We need to emulate that behaviour in our
pre-parse of the command line.
Incidentally, fix up a bug in boolean options, and improve our testing.
Reviewed-By: Nick Lewis <nick@puppetlabs.com>
Diffstat (limited to 'spec/unit/application/string_base_spec.rb')
| -rwxr-xr-x | spec/unit/application/string_base_spec.rb | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/spec/unit/application/string_base_spec.rb b/spec/unit/application/string_base_spec.rb index 62869fe61..7f06c05f4 100755 --- a/spec/unit/application/string_base_spec.rb +++ b/spec/unit/application/string_base_spec.rb @@ -5,7 +5,6 @@ require 'puppet/application/string_base' require 'tmpdir' class Puppet::Application::StringBase::Basetest < Puppet::Application::StringBase - option("--[no-]foo") end describe Puppet::Application::StringBase do @@ -17,9 +16,15 @@ describe Puppet::Application::StringBase do f.puts "Puppet::String.define(:basetest, '0.0.1')" end - Puppet::String[:basetest, '0.0.1'].action :foo do - option "--foo" - invoke { |*args| args.length } + Puppet::String.define(:basetest, '0.0.1') do + option("--[no-]boolean") + option("--mandatory MANDATORY") + option("--optional [OPTIONAL]") + + action :foo do + option("--action") + invoke { |*args| args.length } + end end end @@ -69,15 +74,15 @@ describe Puppet::Application::StringBase do end it "should report a sensible error when options with = fail" do - app.command_line.stubs(:args).returns %w{--foo=bar foo} + app.command_line.stubs(:args).returns %w{--action=bar foo} expect { app.preinit }. - should raise_error ArgumentError, /Unknown option "--foo"/ + should raise_error ArgumentError, /Unknown option "--action"/ end it "should fail if an action option is before the action" do - app.command_line.stubs(:args).returns %w{--foo foo} + app.command_line.stubs(:args).returns %w{--action foo} expect { app.preinit }. - should raise_error ArgumentError, /Unknown option "--foo"/ + should raise_error ArgumentError, /Unknown option "--action"/ end it "should fail if an unknown option is before the action" do @@ -93,6 +98,20 @@ describe Puppet::Application::StringBase do app.string.should_not be_option :bar app.action.should_not be_option :bar end + + it "should accept --bar as an argument to a mandatory option after action" do + app.command_line.stubs(:args).returns %w{foo --mandatory --bar} + app.preinit and app.parse_options + app.action.name.should == :foo + app.options.should == { :mandatory => "--bar" } + end + + it "should accept --bar as an argument to a mandatory option before action" do + app.command_line.stubs(:args).returns %w{--mandatory --bar foo} + app.preinit and app.parse_options + app.action.name.should == :foo + app.options.should == { :mandatory => "--bar" } + end end end |
