diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 13:33:03 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 13:33:03 -0700 |
| commit | 9ce031d300738c778254358792ee295c08252cff (patch) | |
| tree | c0489dfd1b5d0a2cdf516d51f34a87e18c150e94 /spec/unit/application/string_base_spec.rb | |
| parent | 505a48c0d316aad7ff26ae2c0ade294707ca081e (diff) | |
| parent | 0c74495529bd697cdc42986882fc3efb4cdc9903 (diff) | |
| download | puppet-9ce031d300738c778254358792ee295c08252cff.tar.gz puppet-9ce031d300738c778254358792ee295c08252cff.tar.xz puppet-9ce031d300738c778254358792ee295c08252cff.zip | |
Merge branch 'feature/master/6749-actions-need-to-support-options'
Diffstat (limited to 'spec/unit/application/string_base_spec.rb')
| -rwxr-xr-x | spec/unit/application/string_base_spec.rb | 138 |
1 files changed, 107 insertions, 31 deletions
diff --git a/spec/unit/application/string_base_spec.rb b/spec/unit/application/string_base_spec.rb index 86f9c09aa..71e67283d 100755 --- a/spec/unit/application/string_base_spec.rb +++ b/spec/unit/application/string_base_spec.rb @@ -15,6 +15,17 @@ describe Puppet::Application::StringBase do File.open(File.join(@dir, 'puppet', 'string', 'basetest.rb'), 'w') do |f| f.puts "Puppet::String.define(:basetest, '0.0.1')" end + + 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 after :all do @@ -22,53 +33,118 @@ describe Puppet::Application::StringBase do $LOAD_PATH.pop end - before do - @app = Puppet::Application::StringBase::Basetest.new - @app.stubs(:exit) - @app.stubs(:puts) + let :app do + app = Puppet::Application::StringBase::Basetest.new + app.stubs(:exit) + app.stubs(:puts) + app.command_line.stubs(:subcommand_name).returns 'subcommand' Puppet::Util::Log.stubs(:newdestination) + app end - describe "when calling main" do - before do - @app.verb = :find - @app.arguments = ["myname", "myarg"] - @app.string.stubs(:find) + describe "#preinit" do + before :each do + app.command_line.stubs(:args).returns %w{} end - it "should send the specified verb and name to the string" do - @app.string.expects(:find).with("myname", "myarg") + describe "parsing the command line" do + context "with just an action" do + before :all do + app.command_line.stubs(:args).returns %w{foo} + app.preinit + end - @app.main - end + it "should set the string based on the type" do + app.string.name.should == :basetest + end - it "should use its render method to render any result" + it "should set the format based on the string default" do + app.format.should == :pson + end - it "should exit with the current exit code" - end + it "should find the action" do + app.action.should be + app.action.name.should == :foo + end + end - describe "during setup" do - before do - @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) - @app.stubs(:validate) + it "should fail if no action is given" do + expect { app.preinit }. + should raise_error ArgumentError, /No action given/ + end + + it "should report a sensible error when options with = fail" do + app.command_line.stubs(:args).returns %w{--action=bar foo} + expect { app.preinit }. + 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{--action foo} + expect { app.preinit }. + should raise_error ArgumentError, /Unknown option "--action"/ + end + + it "should fail if an unknown option is before the action" do + app.command_line.stubs(:args).returns %w{--bar foo} + expect { app.preinit }. + should raise_error ArgumentError, /Unknown option "--bar"/ + end + + it "should not fail if an unknown option is after the action" do + app.command_line.stubs(:args).returns %w{foo --bar} + app.preinit + app.action.name.should == :foo + 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 + + it "should not skip when --foo=bar is given" do + app.command_line.stubs(:args).returns %w{--mandatory=bar --bar foo} + expect { app.preinit }. + should raise_error ArgumentError, /Unknown option "--bar"/ + end end + end - it "should set the verb from the command line arguments" do - @app.setup - @app.verb.should == "find" + describe "#setup" do + it "should remove the action name from the arguments" do + app.command_line.stubs(:args).returns %w{--mandatory --bar foo} + app.preinit and app.parse_options and app.setup + app.arguments.should == [{ :mandatory => "--bar" }] end + end - it "should make sure arguments are an array" do - @app.command_line.stubs(:args).returns(["find", "myname", "myarg"]) - @app.setup - @app.arguments.should == ["myname", "myarg"] + describe "#main" do + before do + app.string = Puppet::String[:basetest, '0.0.1'] + app.action = app.string.get_action(:foo) + app.format = :pson + app.arguments = ["myname", "myarg"] end - it "should set the options on the string" do - @app.options[:foo] = "bar" - @app.setup + it "should send the specified verb and name to the string" do + app.string.expects(:foo).with(*app.arguments) + app.main + end - @app.string.options.should == @app.options + it "should use its render method to render any result" do + app.expects(:render).with(app.arguments.length + 1) + app.main end end end |
