Please review pull request #501: Ticket/2.7.x/12645 face actions should have the ability to set specific exit codes opened by (kelseyhightower)

Description:

Before this patch Face actions do not have the ability to set specific
exit codes. The only way to set a non-zero exit code is to raise an
exception, which sets the exit code to 1.

This patch provides the ability for Face actions to set specific exit
codes by simply calling Kernel::exit.

Example:

exit(2)

Calling Kernel::exit raises a SystemExit exception which we rescue in
the Puppet::Application::FaceBase class -- the base class of all
Faces, which updates the status variable used to set the exit code later
on.


This patch also includes updated specs related to this change.

  • Opened: Wed Feb 15 05:19:09 UTC 2012
  • Based on: puppetlabs:2.7.x (b9655fea47c5eefaa6f4768b750e634aeb063aca)
  • Requested merge: kelseyhightower:ticket/2.7.x/12645_face_actions_should_have_the_ability_to_set_specific_exit_codes (4b4daec4d0e641bc414dc9870b282ecafe402dac)

Diff follows:

diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb
index 14ce67b..f8437cf 100644
--- a/lib/puppet/application/face_base.rb
+++ b/lib/puppet/application/face_base.rb
@@ -243,6 +243,19 @@ def main
     puts render(result, arguments) unless result.nil?
     status = true
 
+  # We need an easy way for the action to set a specific exit code, so we
+  # rescue SystemExit here; This allows each action to set the desired exit
+  # code by simply calling Kernel::exit.  eg:
+  #
+  #   exit(2)
+  #
+  # Note: we don't actually do anything here, we only set the status with the
+  # desired exit code.
+  #
+  # --kelsey 2012-02-14
+  rescue SystemExit => detail
+    status = detail.status
+
   rescue Exception => detail
     puts detail.backtrace if Puppet[:trace]
     Puppet.err detail.to_s
diff --git a/spec/lib/puppet/face/basetest.rb b/spec/lib/puppet/face/basetest.rb
index e5c9a9d..af51435 100755
--- a/spec/lib/puppet/face/basetest.rb
+++ b/spec/lib/puppet/face/basetest.rb
@@ -43,4 +43,9 @@
     summary "return the count of arguments given"
     when_invoked do |*args| args.length - 1 end
   end
+
+  action :with_specific_exit_code do
+    summary "just call exit with the desired exit code"
+    when_invoked do |options| exit(5) end
+  end
 end
diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb
index d72187b..0e58184 100755
--- a/spec/unit/application/face_base_spec.rb
+++ b/spec/unit/application/face_base_spec.rb
@@ -257,6 +257,11 @@ class Puppet::Application::FaceBase::Basetest < Puppet::Application::FaceBase
       app.action = "" :return_raise
       expect { app.main }.not_to exit_with 0
     end
+
+    it "should use the exit code set by the action" do
+      app.action = "" :with_specific_exit_code
+      expect { app.main }.to exit_with 5
+    end
   end
 
   describe "#render" do

    

--
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.

Reply via email to