From de6a2052c2aeda1cd76ba828936a9d6f0ac7e907 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Tue, 22 Feb 2011 11:18:19 -0800 Subject: (#5552) Clean up subcommand handling inside puppet cert. We now have a regular, testable mechanism for handling the legacy '--' version of subcommands, as well as a modern bareword subcommand pattern. This makes it sensible to test command handling and avoid regressions. We identified a few quirks in the command line as part of this process. Pair-With: Jesse Wolfe Signed-off-by: Daniel Pittman --- spec/unit/application/cert_spec.rb | 58 +++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'spec') diff --git a/spec/unit/application/cert_spec.rb b/spec/unit/application/cert_spec.rb index 4663fc938..2f57d07a9 100755 --- a/spec/unit/application/cert_spec.rb +++ b/spec/unit/application/cert_spec.rb @@ -51,7 +51,7 @@ describe Puppet::Application::Cert do it "should set cert_mode to :destroy for --clean" do @cert_app.handle_clean(0) - @cert_app.cert_mode.should == :destroy + @cert_app.subcommand.should == :destroy end it "should set all to true for --all" do @@ -68,7 +68,7 @@ describe Puppet::Application::Cert do it "should set cert_mode to #{method} with option --#{method}" do @cert_app.send("handle_#{method}".to_sym, nil) - @cert_app.cert_mode.should == method + @cert_app.subcommand.should == method end end @@ -114,19 +114,19 @@ describe Puppet::Application::Cert do end it "should set the ca_location to :local if the cert_mode is generate" do - @cert_app.find_mode('--generate') + @cert_app.subcommand = 'generate' Puppet::SSL::Host.expects(:ca_location=).with(:local) @cert_app.setup end it "should set the ca_location to :local if the cert_mode is destroy" do - @cert_app.find_mode('--destroy') + @cert_app.subcommand = 'destroy' Puppet::SSL::Host.expects(:ca_location=).with(:local) @cert_app.setup end it "should set the ca_location to :only if the cert_mode is print" do - @cert_app.find_mode('--print') + @cert_app.subcommand = 'print' Puppet::SSL::Host.expects(:ca_location=).with(:only) @cert_app.setup end @@ -171,24 +171,54 @@ describe Puppet::Application::Cert do @cert_app.main end - it "should delegate to ca.apply with current set cert_mode" do - @cert_app.cert_mode = "currentmode" + it "should revoke cert if cert_mode is clean" do + @cert_app.subcommand = :destroy @cert_app.command_line.stubs(:args).returns(["host"]) - @ca.expects(:apply).with { |cert_mode,to| cert_mode == "currentmode" } + @ca.expects(:apply).with { |cert_mode,to| cert_mode == :revoke } + @ca.expects(:apply).with { |cert_mode,to| cert_mode == :destroy } @cert_app.main end + end - it "should revoke cert if cert_mode is clean" do - @cert_app.cert_mode = :destroy - @cert_app.command_line.stubs(:args).returns(["host"]) + describe "when identifying subcommands" do + before :each do + @cert_app.all = false + @ca = stub_everything 'ca' + @cert_app.ca = @ca + end - @ca.expects(:apply).with { |cert_mode,to| cert_mode == :revoke } - @ca.expects(:apply).with { |cert_mode,to| cert_mode == :destroy } + %w{list revoke generate sign print verify fingerprint}.each do |cmd| + short = cmd[0,1] + [cmd, "--#{cmd}", "-#{short}"].each do |option| + # In our command line '-v' was eaten by 'verbose', so we can't consume + # it here; this is a special case from our otherwise standard + # processing. --daniel 2011-02-22 + next if option == "-v" - @cert_app.main + it "should recognise '#{option}'" do + args = [option, "fun.example.com"] + + @cert_app.command_line.stubs(:args).returns(args) + @cert_app.parse_options + @cert_app.subcommand.should == cmd.to_sym + + args.should == ["fun.example.com"] + end + end end + %w{clean --clean -c}.each do |ugly| + it "should recognise the '#{ugly}' option as destroy" do + args = [ugly, "fun.example.com"] + + @cert_app.command_line.stubs(:args).returns(args) + @cert_app.parse_options + @cert_app.subcommand.should == :destroy + + args.should == ["fun.example.com"] + end + end end end -- cgit From 309b9320feef3e1a9459c7a26d10955b4d6b549c Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Tue, 22 Feb 2011 11:25:03 -0800 Subject: (#5552) Display help when no subcommand is given. Previously, when the command line was empty we would try and invoke an empty method; this was less helpful than telling people what they could actually do, so we adapt our code to do precisely that. Paired-With: Jesse Wolfe Signed-off-by: Daniel Pittman --- spec/unit/application/cert_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'spec') diff --git a/spec/unit/application/cert_spec.rb b/spec/unit/application/cert_spec.rb index 2f57d07a9..b3257916b 100755 --- a/spec/unit/application/cert_spec.rb +++ b/spec/unit/application/cert_spec.rb @@ -189,6 +189,16 @@ describe Puppet::Application::Cert do @cert_app.ca = @ca end + it "should not fail when no command is given" do + # Make the help method silent for testing; this is a bit nasty, but we + # can't identify a cleaner method. Help welcome. --daniel 2011-02-22 + Puppet.features.stubs(:usage?).returns(false) + @cert_app.stubs(:puts) + + @cert_app.command_line.stubs(:args).returns([]) + expect { @cert_app.parse_options }.should raise_error SystemExit + end + %w{list revoke generate sign print verify fingerprint}.each do |cmd| short = cmd[0,1] [cmd, "--#{cmd}", "-#{short}"].each do |option| -- cgit