As these options should be available to all applications we move them into Puppet::Application. This moves also various initialize and setup code into Puppet::Application.
Signed-off-by: Peter Meier <[email protected]> --- lib/puppet/application.rb | 22 +++++++--- lib/puppet/application/agent.rb | 24 +---------- lib/puppet/application/apply.rb | 10 +---- lib/puppet/application/cert.rb | 9 +---- lib/puppet/application/describe.rb | 2 + lib/puppet/application/doc.rb | 15 +------ lib/puppet/application/filebucket.rb | 11 +----- lib/puppet/application/kick.rb | 9 +--- lib/puppet/application/master.rb | 27 +++---------- lib/puppet/application/queue.rb | 26 +------------ lib/puppet/application/resource.rb | 10 +---- spec/unit/application/agent_spec.rb | 48 +---------------------- spec/unit/application/apply_spec.rb | 18 +-------- spec/unit/application/cert_spec.rb | 14 ------- spec/unit/application/doc_spec.rb | 43 ++++---------------- spec/unit/application/filebucket_spec.rb | 18 +-------- spec/unit/application/master_spec.rb | 47 +++------------------- spec/unit/application/queue_spec.rb | 63 ------------------------------ spec/unit/application/resource_spec.rb | 33 +++------------ spec/unit/application_spec.rb | 54 ++++++++++++++++++++++--- 20 files changed, 108 insertions(+), 395 deletions(-) diff --git a/lib/puppet/application.rb b/lib/puppet/application.rb index f0159a6..1b4703b 100644 --- a/lib/puppet/application.rb +++ b/lib/puppet/application.rb @@ -246,12 +246,22 @@ class Application help end + # Every app responds to --debug and --verbose + option("--debug", "-d") + option("--verbose", "-v") + def should_parse_config? self.class.should_parse_config? end # override to execute code before running anything else def preinit + { + :verbose => false, + :debug => false, + }.each do |val,key| + options[val] = key + end end def initialize(command_line = nil) @@ -297,16 +307,14 @@ class Application def setup # Handle the logging settings - if options[:debug] or options[:verbose] - Puppet::Util::Log.newdestination(:console) - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end + if options[:debug] || options[:verbose] + Puppet::Util::Log.newdestination(:console) unless Puppet[:daemonize] + Puppet::Util::Log.level = options[:debug] ? :debug : :info end Puppet::Util::Log.newdestination(:syslog) unless options[:setdest] + + exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? end def parse_options diff --git a/lib/puppet/application/agent.rb b/lib/puppet/application/agent.rb index 2b75505..a1de912 100644 --- a/lib/puppet/application/agent.rb +++ b/lib/puppet/application/agent.rb @@ -8,6 +8,7 @@ class Puppet::Application::Agent < Puppet::Application attr_accessor :args, :agent, :daemon, :host def preinit + super # Do an initial trap, so that cancels don't get a stack trace. trap(:INT) do $stderr.puts "Cancelling startup" @@ -17,8 +18,6 @@ class Puppet::Application::Agent < Puppet::Application { :waitforcert => nil, :detailed_exitcodes => false, - :verbose => false, - :debug => false, :centrallogs => false, :setdest => false, :enable => false, @@ -41,10 +40,8 @@ class Puppet::Application::Agent < Puppet::Application option("--centrallogging") option("--disable") option("--enable") - option("--debug","-d") option("--fqdn FQDN","-f") option("--test","-t") - option("--verbose","-v") option("--fingerprint") option("--digest DIGEST") @@ -144,20 +141,6 @@ class Puppet::Application::Agent < Puppet::Application options[:detailed_exitcodes] = true end - # Handle the logging settings. - def setup_logs - if options[:debug] or options[:verbose] - Puppet::Util::Log.newdestination(:console) - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end - end - - Puppet::Util::Log.newdestination(:syslog) unless options[:setdest] - end - def enable_disable_client(agent) if options[:enable] agent.enable @@ -196,10 +179,7 @@ class Puppet::Application::Agent < Puppet::Application def setup setup_test if options[:test] - - setup_logs - - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? + super # If noop is set, then also enable diffs Puppet[:show_diff] = true if Puppet[:noop] diff --git a/lib/puppet/application/apply.rb b/lib/puppet/application/apply.rb index 33a70ce..dc804d3 100644 --- a/lib/puppet/application/apply.rb +++ b/lib/puppet/application/apply.rb @@ -4,12 +4,10 @@ class Puppet::Application::Apply < Puppet::Application should_parse_config - option("--debug","-d") option("--execute EXECUTE","-e") do |arg| options[:code] = arg end option("--loadclasses","-L") - option("--verbose","-v") option("--use-nodes") option("--detailed-exitcodes") @@ -134,7 +132,7 @@ class Puppet::Application::Apply < Puppet::Application end def setup - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? + super # If noop is set, then also enable diffs Puppet[:show_diff] = true if Puppet[:noop] @@ -147,11 +145,5 @@ class Puppet::Application::Apply < Puppet::Application $stderr.puts "Exiting" exit(1) end - - if options[:debug] - Puppet::Util::Log.level = :debug - elsif options[:verbose] - Puppet::Util::Log.level = :info - end end end diff --git a/lib/puppet/application/cert.rb b/lib/puppet/application/cert.rb index 467b0c8..c438f65 100644 --- a/lib/puppet/application/cert.rb +++ b/lib/puppet/application/cert.rb @@ -30,10 +30,6 @@ class Puppet::Application::Cert < Puppet::Application @signed = true end - option("--debug", "-d") do |arg| - Puppet::Util::Log.level = :debug - end - require 'puppet/ssl/certificate_authority/interface' Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS.reject {|m| m == :destroy }.each do |method| option("--#{method}", "-#{method.to_s[0,1]}") do @@ -41,9 +37,6 @@ class Puppet::Application::Cert < Puppet::Application end end - option("--verbose", "-v") do - Puppet::Util::Log.level = :info - end def main if @all @@ -64,7 +57,7 @@ class Puppet::Application::Cert < Puppet::Application end def setup - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? + super Puppet::Util::Log.newdestination :console diff --git a/lib/puppet/application/describe.rb b/lib/puppet/application/describe.rb index e76b347..54d419c 100644 --- a/lib/puppet/application/describe.rb +++ b/lib/puppet/application/describe.rb @@ -181,6 +181,7 @@ class Puppet::Application::Describe < Puppet::Application option("--meta","-m") def preinit + super options[:parameters] = true end @@ -195,6 +196,7 @@ class Puppet::Application::Describe < Puppet::Application end def setup + super options[:types] = command_line.args.dup handle_help(nil) unless options[:list] || options[:types].size > 0 $stderr.puts "Warning: ignoring types when listing all types" if options[:list] && options[:types].size > 0 diff --git a/lib/puppet/application/doc.rb b/lib/puppet/application/doc.rb index 1f6c632..a4c423e 100644 --- a/lib/puppet/application/doc.rb +++ b/lib/puppet/application/doc.rb @@ -8,6 +8,7 @@ class Puppet::Application::Doc < Puppet::Application attr_accessor :unknown_args, :manifest def preinit + super {:references => [], :mode => :text, :format => :to_rest }.each do |name,value| options[name] = value end @@ -17,8 +18,6 @@ class Puppet::Application::Doc < Puppet::Application option("--all","-a") option("--outputdir OUTPUTDIR","-o") - option("--verbose","-v") - option("--debug","-d") option("--charset CHARSET") option("--format FORMAT", "-f") do |arg| @@ -126,6 +125,7 @@ class Puppet::Application::Doc < Puppet::Application end def setup + super # sole manifest documentation if command_line.args.size > 0 options[:mode] = :rdoc @@ -166,16 +166,5 @@ class Puppet::Application::Doc < Puppet::Application # Now parse the config Puppet.parse_config - - # Handle the logging settings. - if options[:debug] or options[:verbose] - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end - - Puppet::Util::Log.newdestination(:console) - end end end diff --git a/lib/puppet/application/filebucket.rb b/lib/puppet/application/filebucket.rb index 9c3c79b..a17259a 100644 --- a/lib/puppet/application/filebucket.rb +++ b/lib/puppet/application/filebucket.rb @@ -5,10 +5,8 @@ class Puppet::Application::Filebucket < Puppet::Application should_not_parse_config option("--bucket BUCKET","-b") - option("--debug","-d") option("--local","-l") option("--remote","-r") - option("--verbose","-v") attr :args @@ -47,6 +45,7 @@ class Puppet::Application::Filebucket < Puppet::Application end def setup + super Puppet::Log.newdestination(:console) @client = nil @@ -57,17 +56,9 @@ class Puppet::Application::Filebucket < Puppet::Application exit(1) end - if options[:debug] - Puppet::Log.level = :debug - elsif options[:verbose] - Puppet::Log.level = :info - end - # Now parse the config Puppet.parse_config - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? - require 'puppet/file_bucket/dipper' begin if options[:local] or options[:bucket] diff --git a/lib/puppet/application/kick.rb b/lib/puppet/application/kick.rb index 37aeb1e..ecbbdc4 100644 --- a/lib/puppet/application/kick.rb +++ b/lib/puppet/application/kick.rb @@ -8,7 +8,6 @@ class Puppet::Application::Kick < Puppet::Application option("--all","-a") option("--foreground","-f") - option("--debug","-d") option("--ping","-P") option("--test") @@ -150,6 +149,7 @@ class Puppet::Application::Kick < Puppet::Application end def preinit + super [:INT, :TERM].each do |signal| trap(signal) do $stderr.puts "Cancelling" @@ -164,12 +164,7 @@ class Puppet::Application::Kick < Puppet::Application end def setup - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end - + super # Now parse the config Puppet.parse_config diff --git a/lib/puppet/application/master.rb b/lib/puppet/application/master.rb index fde4749..b99425f 100644 --- a/lib/puppet/application/master.rb +++ b/lib/puppet/application/master.rb @@ -5,11 +5,11 @@ class Puppet::Application::Master < Puppet::Application should_parse_config run_mode :master - option("--debug", "-d") - option("--verbose", "-v") - # internal option, only to be used by ext/rack/config.ru - option("--rack") + option("--rack") do + # we set :daemonize to true so we don't log to the console + options[:daemonize] = true + end option("--compile host", "-c host") do |arg| options[:node] = arg @@ -26,6 +26,7 @@ class Puppet::Application::Master < Puppet::Application end def preinit + super trap(:INT) do $stderr.puts "Cancelling startup" exit(0) @@ -118,23 +119,7 @@ class Puppet::Application::Master < Puppet::Application end def setup - # Handle the logging settings. - if options[:debug] or options[:verbose] - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end - - unless Puppet[:daemonize] or options[:rack] - Puppet::Util::Log.newdestination(:console) - options[:setdest] = true - end - end - - Puppet::Util::Log.newdestination(:syslog) unless options[:setdest] - - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? + super Puppet.settings.use :main, :master, :ssl diff --git a/lib/puppet/application/queue.rb b/lib/puppet/application/queue.rb index 239f6b2..0b0603a 100644 --- a/lib/puppet/application/queue.rb +++ b/lib/puppet/application/queue.rb @@ -25,18 +25,8 @@ class Puppet::Application::Queue < Puppet::Application $stderr.puts "Caught SIGTERM; shutting down" exit(0) end - - { - :verbose => false, - :debug => false - }.each do |opt,val| - options[opt] = val - end end - option("--debug","-d") - option("--verbose","-v") - def main require 'puppet/indirector/catalog/queue' # provides Puppet::Indirector::Queue.subscribe Puppet.notice "Starting puppetqd #{Puppet.version}" @@ -57,26 +47,12 @@ class Puppet::Application::Queue < Puppet::Application Thread.list.each { |thread| thread.join } end - # Handle the logging settings. - def setup_logs - if options[:debug] or options[:verbose] - Puppet::Util::Log.newdestination(:console) - if options[:debug] - Puppet::Util::Log.level = :debug - else - Puppet::Util::Log.level = :info - end - end - end - def setup unless Puppet.features.stomp? raise ArgumentError, "Could not load the 'stomp' library, which must be present for queueing to work. You must install the required library." end - setup_logs - - exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs? + super require 'puppet/resource/catalog' Puppet::Resource::Catalog.terminus_class = :active_record diff --git a/lib/puppet/application/resource.rb b/lib/puppet/application/resource.rb index f55caa5..bf17ea1 100644 --- a/lib/puppet/application/resource.rb +++ b/lib/puppet/application/resource.rb @@ -7,13 +7,12 @@ class Puppet::Application::Resource < Puppet::Application attr_accessor :host, :extra_params def preinit + super @extra_params = [] @host = nil Facter.loadfacts end - option("--debug","-d") - option("--verbose","-v") option("--edit","-e") option("--host HOST","-H") do |arg| @@ -104,15 +103,10 @@ class Puppet::Application::Resource < Puppet::Application end def setup + super Puppet::Util::Log.newdestination(:console) # Now parse the config Puppet.parse_config - - if options[:debug] - Puppet::Util::Log.level = :debug - elsif options[:verbose] - Puppet::Util::Log.level = :info - end end end diff --git a/spec/unit/application/agent_spec.rb b/spec/unit/application/agent_spec.rb index ff504ee..1448334 100755 --- a/spec/unit/application/agent_spec.rb +++ b/spec/unit/application/agent_spec.rb @@ -96,7 +96,7 @@ describe Puppet::Application::Agent do @puppetd.command_line.stubs(:args).returns([]) end - [:centrallogging, :disable, :enable, :debug, :fqdn, :test, :verbose, :digest].each do |option| + [:centrallogging, :disable, :enable, :fqdn, :test, :digest].each do |option| it "should declare handle_#{option} method" do @puppetd.should respond_to("handle_#{option}".to_sym) end @@ -217,52 +217,6 @@ describe Puppet::Application::Agent do end end - it "should call setup_logs" do - @puppetd.expects(:setup_logs) - @puppetd.setup - end - - describe "when setting up logs" do - before :each do - Puppet::Util::Log.stubs(:newdestination) - end - - it "should set log level to debug if --debug was passed" do - @puppetd.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Util::Log.expects(:level=).with(:debug) - - @puppetd.setup_logs - end - - it "should set log level to info if --verbose was passed" do - @puppetd.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Util::Log.expects(:level=).with(:info) - - @puppetd.setup_logs - end - - [:verbose, :debug].each do |level| - it "should set console as the log destination with level #{level}" do - @puppetd.options.stubs(:[]).with(level).returns(true) - - Puppet::Util::Log.expects(:newdestination).with(:console) - - @puppetd.setup_logs - end - end - - it "should set syslog as the log destination if no --logdest" do - @puppetd.options.stubs(:[]).with(:setdest).returns(false) - - Puppet::Util::Log.expects(:newdestination).with(:syslog) - - @puppetd.setup_logs - end - - end - it "should print puppet config if asked to in Puppet config" do @puppetd.stubs(:exit) Puppet.settings.stubs(:print_configs?).returns(true) diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb index 4e17442..99d5a12 100755 --- a/spec/unit/application/apply_spec.rb +++ b/spec/unit/application/apply_spec.rb @@ -13,7 +13,7 @@ describe Puppet::Application::Apply do Puppet::Util::Log.stubs(:level=) end - [:debug,:loadclasses,:verbose,:use_nodes,:detailed_exitcodes].each do |option| + [:loadclasses,:use_nodes,:detailed_exitcodes].each do |option| it "should declare handle_#{option} method" do @apply.should respond_to("handle_#{option}".to_sym) end @@ -83,22 +83,6 @@ describe Puppet::Application::Apply do @apply.setup end - it "should set log level to debug if --debug was passed" do - @apply.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Log.expects(:level=).with(:debug) - - @apply.setup - end - - it "should set log level to info if --verbose was passed" do - @apply.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Log.expects(:level=).with(:info) - - @apply.setup - end - it "should print puppet config if asked to in Puppet config" do @apply.stubs(:exit) Puppet.settings.stubs(:print_configs?).returns(true) diff --git a/spec/unit/application/cert_spec.rb b/spec/unit/application/cert_spec.rb index 4663fc9..cd6e1b0 100755 --- a/spec/unit/application/cert_spec.rb +++ b/spec/unit/application/cert_spec.rb @@ -29,20 +29,6 @@ describe Puppet::Application::Cert do end end - it "should set log level to info with the --verbose option" do - - Puppet::Log.expects(:level=).with(:info) - - @cert_app.handle_verbose(0) - end - - it "should set log level to debug with the --debug option" do - - Puppet::Log.expects(:level=).with(:debug) - - @cert_app.handle_debug(0) - end - it "should set the fingerprint digest with the --digest option" do @cert_app.handle_digest(:digest) diff --git a/spec/unit/application/doc_spec.rb b/spec/unit/application/doc_spec.rb index 55da5e3..3d344be 100755 --- a/spec/unit/application/doc_spec.rb +++ b/spec/unit/application/doc_spec.rb @@ -56,7 +56,7 @@ describe Puppet::Application::Doc do end describe "when handling options" do - [:all, :outputdir, :verbose, :debug, :charset].each do |option| + [:all, :outputdir, :charset].each do |option| it "should declare handle_#{option} method" do @doc.should respond_to("handle_#{option}".to_sym) end @@ -140,7 +140,10 @@ describe Puppet::Application::Doc do it "should call setup_rdoc in rdoc mode" do @doc.options.stubs(:[]).with(:mode).returns(:rdoc) - + # duritong is not sure why this needs to be stubbed as well + @doc.options.stubs(:[]).with(:debug) + @doc.options.stubs(:[]).with(:verbose) + @doc.options.stubs(:[]).with(:setdest) @doc.expects(:setup_rdoc) @doc.setup @@ -148,7 +151,10 @@ describe Puppet::Application::Doc do it "should call setup_reference if not rdoc" do @doc.options.stubs(:[]).with(:mode).returns(:test) - + # duritong is not sure why this needs to be stubbed as well + @doc.options.stubs(:[]).with(:debug) + @doc.options.stubs(:[]).with(:verbose) + @doc.options.stubs(:[]).with(:setdest) @doc.expects(:setup_reference) @doc.setup @@ -230,37 +236,6 @@ describe Puppet::Application::Doc do @doc.setup_rdoc end - - it "should set log level to debug if --debug" do - @doc.options.stubs(:[]).with(:debug).returns(true) - Puppet::Util::Log.expects(:level=).with(:debug) - - @doc.setup_rdoc - end - - it "should set log level to info if --verbose" do - @doc.options.stubs(:[]).with(:verbose).returns(true) - Puppet::Util::Log.expects(:level=).with(:info) - - @doc.setup_rdoc - end - - it "should set log destination to console if --verbose" do - @doc.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Util::Log.expects(:newdestination).with(:console) - - @doc.setup_rdoc - end - - it "should set log destination to console if --debug" do - @doc.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Util::Log.expects(:newdestination).with(:console) - - @doc.setup_rdoc - end - end end diff --git a/spec/unit/application/filebucket_spec.rb b/spec/unit/application/filebucket_spec.rb index e6272f1..954c2a0 100644 --- a/spec/unit/application/filebucket_spec.rb +++ b/spec/unit/application/filebucket_spec.rb @@ -26,7 +26,7 @@ describe Puppet::Application::Filebucket do @filebucket.should respond_to(:restore) end - [:bucket, :debug, :local, :remote, :verbose].each do |option| + [:bucket, :local, :remote].each do |option| it "should declare handle_#{option} method" do @filebucket.should respond_to("handle_#{option}".to_sym) end @@ -61,22 +61,6 @@ describe Puppet::Application::Filebucket do @filebucket.setup end - it "should set log level to debug if --debug was passed" do - @filebucket.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Log.expects(:level=).with(:debug) - - @filebucket.setup - end - - it "should set log level to info if --verbose was passed" do - @filebucket.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Log.expects(:level=).with(:info) - - @filebucket.setup - end - it "should Parse puppet config" do Puppet.expects(:parse_config) diff --git a/spec/unit/application/master_spec.rb b/spec/unit/application/master_spec.rb index 074249a..1324176 100644 --- a/spec/unit/application/master_spec.rb +++ b/spec/unit/application/master_spec.rb @@ -73,17 +73,6 @@ describe Puppet::Application::Master do end - [:debug,:verbose].each do |option| - it "should declare handle_#{option} method" do - @master.should respond_to("handle_#{option}".to_sym) - end - - it "should store argument value when calling handle_#{option}" do - @master.options.expects(:[]=).with(option, 'arg') - @master.send("handle_#{option}".to_sym, 'arg') - end - end - describe "when applying options" do before do @master.command_line.stubs(:args).returns([]) @@ -101,6 +90,12 @@ describe Puppet::Application::Master do @master.handle_logdest("console") end + it "should set :daemonize to true with --rack" do + @master.command_line.stubs(:args).returns(%w{--rack}) + @master.options.expects(:[]=).with(:daemonize,true) + @master.parse_options + end + it "should parse the log destination from ARGV" do @master.command_line.stubs(:args).returns(%w{--logdest /my/file}) @@ -123,36 +118,6 @@ describe Puppet::Application::Master do @master.options.stubs(:[]).with(any_parameters) end - it "should set log level to debug if --debug was passed" do - @master.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Log.expects(:level=).with(:debug) - - @master.setup - end - - it "should set log level to info if --verbose was passed" do - @master.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Log.expects(:level=).with(:info) - - @master.setup - end - - it "should set console as the log destination if no --logdest and --daemonize" do - @master.stubs(:[]).with(:daemonize).returns(:false) - - Puppet::Log.expects(:newdestination).with(:syslog) - - @master.setup - end - - it "should set syslog as the log destination if no --logdest and not --daemonize" do - Puppet::Log.expects(:newdestination).with(:syslog) - - @master.setup - end - it "should set syslog as the log destination if --rack" do @master.options.stubs(:[]).with(:rack).returns(:true) diff --git a/spec/unit/application/queue_spec.rb b/spec/unit/application/queue_spec.rb index bd0d53a..4468c2d 100755 --- a/spec/unit/application/queue_spec.rb +++ b/spec/unit/application/queue_spec.rb @@ -39,18 +39,6 @@ describe Puppet::Application::Queue do @queue.preinit end - it "should init :verbose to false" do - @queue.preinit - - @queue.options[:verbose].should be_false - end - - it "should init :debug to false" do - @queue.preinit - - @queue.options[:debug].should be_false - end - it "should create a Daemon instance and copy ARGV to it" do ARGV.expects(:dup).returns "eh" daemon = mock("daemon") @@ -60,20 +48,6 @@ describe Puppet::Application::Queue do end end - describe "when handling options" do - - [:debug, :verbose].each do |option| - it "should declare handle_#{option} method" do - @queue.should respond_to("handle_#{option}".to_sym) - end - - it "should store argument value when calling handle_#{option}" do - @queue.options.expects(:[]=).with(option, 'arg') - @queue.send("handle_#{option}".to_sym, 'arg') - end - end - end - describe "during setup" do before :each do @queue.options.stubs(:[]) @@ -106,43 +80,6 @@ describe Puppet::Application::Queue do lambda { @queue.setup }.should raise_error(SystemExit) end - it "should call setup_logs" do - @queue.expects(:setup_logs) - @queue.setup - end - - describe "when setting up logs" do - before :each do - Puppet::Util::Log.stubs(:newdestination) - end - - it "should set log level to debug if --debug was passed" do - @queue.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Util::Log.expects(:level=).with(:debug) - - @queue.setup_logs - end - - it "should set log level to info if --verbose was passed" do - @queue.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Util::Log.expects(:level=).with(:info) - - @queue.setup_logs - end - - [:verbose, :debug].each do |level| - it "should set console as the log destination with level #{level}" do - @queue.options.stubs(:[]).with(level).returns(true) - - Puppet::Util::Log.expects(:newdestination).with(:console) - - @queue.setup_logs - end - end - end - it "should configure the Catalog class to use ActiveRecord" do Puppet::Resource::Catalog.expects(:terminus_class=).with(:active_record) diff --git a/spec/unit/application/resource_spec.rb b/spec/unit/application/resource_spec.rb index b6c52b1..25a0227 100755 --- a/spec/unit/application/resource_spec.rb +++ b/spec/unit/application/resource_spec.rb @@ -57,15 +57,13 @@ describe Puppet::Application::Resource do describe "when handling options" do - [:debug, :verbose, :edit].each do |option| - it "should declare handle_#{option} method" do - @resource.should respond_to("handle_#{option}".to_sym) - end + it "should declare handle_edit method" do + @resource.should respond_to("handle_edit".to_sym) + end - it "should store argument value when calling handle_#{option}" do - @resource.options.expects(:[]=).with(option, 'arg') - @resource.send("handle_#{option}".to_sym, 'arg') - end + it "should store argument value when calling handle_edit" do + @resource.options.expects(:[]=).with(:edit, 'arg') + @resource.send("handle_edit".to_sym, 'arg') end it "should set options[:host] to given host" do @@ -107,23 +105,6 @@ describe Puppet::Application::Resource do @resource.setup end - it "should set log level to debug if --debug was passed" do - @resource.options.stubs(:[]).with(:debug).returns(true) - - Puppet::Log.expects(:level=).with(:debug) - - @resource.setup - end - - it "should set log level to info if --verbose was passed" do - @resource.options.stubs(:[]).with(:debug).returns(false) - @resource.options.stubs(:[]).with(:verbose).returns(true) - - Puppet::Log.expects(:level=).with(:info) - - @resource.setup - end - it "should Parse puppet config" do Puppet.expects(:parse_config) diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb index f68a7e2..98e4ed6 100755 --- a/spec/unit/application_spec.rb +++ b/spec/unit/application_spec.rb @@ -245,6 +245,18 @@ describe Puppet::Application do end end + describe "when handling options" do + before do + @app.command_line.stubs(:args).returns([]) + end + + [:debug, :verbose].each do |option| + it "should declare handle_#{option} method" do + @app.should respond_to("handle_#{option}".to_sym) + end + end + end + describe "when dealing with an argument not declared directly by the application" do it "should pass it to handle_unknown if this method exists" do Puppet.settings.stubs(:optparse_addargs).returns([["--not-handled", :REQUIRED]]) @@ -312,27 +324,57 @@ describe Puppet::Application do before :each do @app.stubs(:should_parse_config?).returns(false) @app.options.stubs(:[]) + Puppet::Util::Log.stubs(:newdestination) + end + + it "should honor setdest option" do + @app.options.stubs(:[]).with(:setdest).returns(false) + + Puppet::Util::Log.expects(:newdestination).with(:syslog) + + @app.setup end - [ :debug, :verbose ].each do |level| - it "should honor option #{level}" do + { :debug => :debug, :verbose => :info }.each do |opt,level| + it "should set log level to #{opt} if --#{opt} was passed" do + @app.options.stubs(:[]).with(opt).returns(true) + Puppet::Util::Log.expects(:level=).with(level) + + @app.setup + end + + it "should not set console as the log destination with level #{level} if daemonized" do @app.options.stubs(:[]).with(level).returns(true) - Puppet::Util::Log.stubs(:newdestination) + Puppet.stubs(:[]).with(:daemonize).returns(true) + Puppet::Util::Log.expects(:newdestination).with(:console).never - Puppet::Util::Log.expects(:level=).with(level == :verbose ? :info : :debug) + @app.setup + end + it "should set console as the log destination with level #{level} if not daemonized" do + @app.options.stubs(:[]).with(level).returns(true) + Puppet.stubs(:[]).with(:daemonize).returns(false) + Puppet::Util::Log.expects(:newdestination).with(:console).never @app.setup end end - it "should honor setdest option" do + it "should set syslog as the log destination if no --logdest" do @app.options.stubs(:[]).with(:setdest).returns(false) - Puppet::Util::Log.expects(:newdestination).with(:syslog) @app.setup end + end + describe "when calling preinit" do + [:debug,:verbose].each do |opt| + it "should init :#{opt} to false" do + @app.preinit + + @app.options[opt].should be_false + end + end end describe "when running" do -- 1.7.3.4 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
