[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 0.25.5-639-g8f94f35

test branch puppet-dev at googlegroups.com
Wed Jul 14 10:33:21 UTC 2010


The following commit has been merged in the upstream branch:
commit 5b64d3b798b79357f98f100fc027d73518320ddf
Author: Jesse Wolfe <jes5199 at gmail.com>
Date:   Mon Apr 26 14:30:12 2010 -0700

    feature #2276 Single Executable: optparser should get CommandLine#args instead of ARGV
    
    Signed-off-by: Jesse Wolfe <jes5199 at gmail.com>

diff --git a/lib/puppet/application.rb b/lib/puppet/application.rb
index f72714b..c0bfe8f 100644
--- a/lib/puppet/application.rb
+++ b/lib/puppet/application.rb
@@ -198,26 +198,18 @@ class Puppet::Application
                     self.options["#{long}".to_sym] = value
                 end
             end
-            @opt_parser_commands ||= []
-            @opt_parser_commands << [options, fname]
+            self.option_parser_commands << [options, fname]
         end
 
         def banner(banner = nil)
-            @banner = banner unless banner.nil?
+            @banner ||= banner
         end
 
-        def new_option_parser( target )
-            @banner ||= nil
-
-            opt_parser = OptionParser.new(@banner)
-
-            @opt_parser_commands ||= []
-            @opt_parser_commands.each do |options, fname|
-                opt_parser.on(*options) do |value|
-                    target.send(fname, value)
-                end
-            end
-            opt_parser
+        def option_parser_commands
+            @option_parser_commands ||= (
+                superclass.respond_to?(:option_parser_commands) ? superclass.option_parser_commands.dup : []
+            )
+            @option_parser_commands
         end
 
         def find(name)
@@ -229,7 +221,7 @@ class Puppet::Application
         end
     end
 
-    attr_reader :options, :opt_parser, :command_line
+    attr_reader :options, :command_line
 
     # Every app responds to --version
     option("--version", "-V") do |arg|
@@ -250,9 +242,22 @@ class Puppet::Application
     def preinit
     end
 
+    def option_parser
+        return @option_parser if defined? @option_parser
+
+        @option_parser = OptionParser.new(self.class.banner)
+
+        self.class.option_parser_commands.each do |options, fname|
+            @option_parser.on(*options) do |value|
+                self.send(fname, value)
+            end
+        end
+        @option_parser.default_argv = self.command_line.args
+        @option_parser
+    end
+
     def initialize(command_line = nil)
         @command_line = command_line || Puppet::Util::CommandLine.new
-        @opt_parser = self.class.new_option_parser( self )
 
         @options = {}
     end
@@ -297,14 +302,14 @@ class Puppet::Application
 
         # convert them to OptionParser format
         optparse_opt.each do |option|
-            @opt_parser.on(*option) do |arg|
+            self.option_parser.on(*option) do |arg|
                 handlearg(option[0], arg)
             end
         end
 
         # scan command line argument
         begin
-            @opt_parser.parse!
+            self.option_parser.parse!
         rescue OptionParser::ParseError => detail
             $stderr.puts detail
             $stderr.puts "Try '#{$0} --help'"
diff --git a/spec/unit/application.rb b/spec/unit/application.rb
index c18aa19..daaed0c 100755
--- a/spec/unit/application.rb
+++ b/spec/unit/application.rb
@@ -167,15 +167,40 @@ describe Puppet::Application do
     describe "when parsing command-line options" do
 
         before :each do
-            @argv_bak = ARGV.dup
-            ARGV.clear
+            @app.command_line.stubs(:args).returns([])
 
             Puppet.settings.stubs(:optparse_addargs).returns([])
         end
 
-        after :each do
-            ARGV.clear
-            ARGV << @argv_bak
+        it "should create a new option parser when needed" do
+            option_parser = stub "option parser"
+            option_parser.stubs(:on)
+            option_parser.stubs(:default_argv=)
+            OptionParser.expects(:new).returns(option_parser).once
+            @app.option_parser.should == option_parser
+            @app.option_parser.should == option_parser
+        end
+
+        it "should pass the banner to the option parser" do
+            option_parser = stub "option parser"
+            option_parser.stubs(:on)
+            option_parser.stubs(:default_argv=)
+            @app.class.instance_eval do
+                banner "banner"
+            end
+
+            OptionParser.expects(:new).with("banner").returns(option_parser)
+
+            @app.option_parser
+        end
+
+        it "should set the optionparser's args to the command line args" do
+            option_parser = stub "option parser"
+            option_parser.stubs(:on)
+            option_parser.expects(:default_argv=).with(%w{ fake args })
+            @app.command_line.stubs(:args).returns(%w{ fake args })
+            OptionParser.expects(:new).returns(option_parser)
+            @app.option_parser
         end
 
         it "should get options from Puppet.settings.optparse_addargs" do
@@ -187,13 +212,13 @@ describe Puppet::Application do
         it "should add Puppet.settings options to OptionParser" do
             Puppet.settings.stubs(:optparse_addargs).returns( [["--option","-o", "Funny Option"]])
 
-            @app.opt_parser.expects(:on).with { |*arg| arg == ["--option","-o", "Funny Option"] }
+            @app.option_parser.expects(:on).with { |*arg| arg == ["--option","-o", "Funny Option"] }
 
             @app.parse_options
         end
 
         it "should ask OptionParser to parse the command-line argument" do
-            @app.opt_parser.expects(:parse!)
+            @app.option_parser.expects(:parse!)
 
             @app.parse_options
         end
@@ -225,7 +250,7 @@ describe Puppet::Application do
         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"]])
-                @app.opt_parser.stubs(:on).yields("value")
+                @app.option_parser.stubs(:on).yields("value")
 
                 @app.expects(:handle_unknown).with("--not-handled", "value").returns(true)
 
@@ -234,7 +259,7 @@ describe Puppet::Application do
 
             it "should pass it to Puppet.settings if handle_unknown says so" do
                 Puppet.settings.stubs(:optparse_addargs).returns([["--topuppet"]])
-                @app.opt_parser.stubs(:on).yields("value")
+                @app.option_parser.stubs(:on).yields("value")
 
                 @app.stubs(:handle_unknown).with("--topuppet", "value").returns(false)
 
@@ -244,7 +269,7 @@ describe Puppet::Application do
 
             it "should pass it to Puppet.settings if there is no handle_unknown method" do
                 Puppet.settings.stubs(:optparse_addargs).returns([["--topuppet"]])
-                @app.opt_parser.stubs(:on).yields("value")
+                @app.option_parser.stubs(:on).yields("value")
 
                 @app.stubs(:respond_to?).returns(false)
 
@@ -276,7 +301,7 @@ describe Puppet::Application do
 
         it "should exit if OptionParser raises an error" do
             $stderr.stubs(:puts)
-            @app.opt_parser.stubs(:parse!).raises(OptionParser::ParseError.new("blah blah"))
+            @app.option_parser.stubs(:parse!).raises(OptionParser::ParseError.new("blah blah"))
 
             @app.expects(:exit)
 
@@ -444,7 +469,7 @@ describe Puppet::Application do
                     @app.class.option("--[no-]test3","-t") do
                     end
 
-                    @app.class.new_option_parser( @app )
+                    @app.option_parser
                 end
 
                 it "should pass a block that calls our defined method" do
@@ -456,7 +481,7 @@ describe Puppet::Application do
                     @app.class.option("--test4","-t") do
                     end
 
-                    @app.class.new_option_parser( @app )
+                    @app.option_parser
                 end
             end
 
@@ -467,7 +492,7 @@ describe Puppet::Application do
 
                     @app.class.option("--test4","-t")
 
-                    @app.class.new_option_parser( @app )
+                    @app.option_parser
                 end
 
                 it "should give to OptionParser a block that adds the the value to the options array" do
@@ -478,7 +503,7 @@ describe Puppet::Application do
 
                     @app.class.option("--test4","-t")
 
-                    @app.class.new_option_parser( @app )
+                    @app.option_parser
                 end
             end
         end
diff --git a/spec/unit/application/agent.rb b/spec/unit/application/agent.rb
index fbcc61b..2439fea 100755
--- a/spec/unit/application/agent.rb
+++ b/spec/unit/application/agent.rb
@@ -92,13 +92,7 @@ describe Puppet::Application::Agent do
 
     describe "when handling options" do
         before do
-            @old_argv = ARGV.dup
-            ARGV.clear
-        end
-
-        after do
-            ARGV.clear
-            @old_argv.each { |a| ARGV << a }
+            @puppetd.command_line.stubs(:args).returns([])
         end
 
         [:centrallogging, :disable, :enable, :debug, :fqdn, :test, :verbose, :digest].each do |option|
@@ -154,8 +148,8 @@ describe Puppet::Application::Agent do
             @puppetd.handle_logdest("console")
         end
 
-        it "should parse the log destination from ARGV" do
-            ARGV << "--logdest" << "/my/file"
+        it "should parse the log destination from the command line" do
+            @puppetd.command_line.stubs(:args).returns(%w[--logdest /my/file])
 
             Puppet::Util::Log.expects(:newdestination).with("/my/file")
 
diff --git a/spec/unit/application/master.rb b/spec/unit/application/master.rb
index 2672fdb..362769e 100644
--- a/spec/unit/application/master.rb
+++ b/spec/unit/application/master.rb
@@ -80,13 +80,7 @@ describe Puppet::Application::Master do
 
     describe "when applying options" do
         before do
-            @old_argv = ARGV.dup
-            ARGV.clear
-        end
-
-        after do
-            ARGV.clear
-            @old_argv.each { |a| ARGV << a }
+            @master.command_line.stubs(:args).returns([])
         end
 
         it "should set the log destination with --logdest" do
@@ -102,7 +96,7 @@ describe Puppet::Application::Master do
         end
 
         it "should parse the log destination from ARGV" do
-            ARGV << "--logdest" << "/my/file"
+            @master.command_line.stubs(:args).returns(%w[--logdest /my/file])
 
             Puppet::Util::Log.expects(:newdestination).with("/my/file")
 

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list