From bda25ac0872a101b59d8ab473218ff0f14bb7433 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 18 Jan 2013 21:29:29 +0100 Subject: validate_cmd: Make sure tmpfile is always closed and unlinked --- lib/puppet/parser/functions/validate_cmd.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions/validate_cmd.rb') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index e7793c3..00fe1ae 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -28,11 +28,14 @@ module Puppet::Parser::Functions # Test content in a temporary file tmpfile = Tempfile.new("validate_cmd") - tmpfile.write(content) - tmpfile.close - output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` - r = $? - File.delete(tmpfile.path) + begin + tmpfile.write(content) + output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` + r = $? + ensure + tmpfile.close + tmpfile.unlink + end if output msg += "\nOutput is:\n#{output}" end -- cgit v1.2.3 From 683ac8f8aa5f349ece98165d92a8d271b99b855e Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Tue, 5 Feb 2013 09:01:48 +0100 Subject: validate_cmd: Use Puppet::Util.execute --- lib/puppet/parser/functions/validate_cmd.rb | 13 ++++++------- spec/unit/puppet/parser/functions/validate_cmd_spec.rb | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'lib/puppet/parser/functions/validate_cmd.rb') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 00fe1ae..4f6a766 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -1,3 +1,5 @@ +require 'puppet/util/execution' + module Puppet::Parser::Functions newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args| Perform validation of a string with an external command. @@ -30,15 +32,12 @@ module Puppet::Parser::Functions tmpfile = Tempfile.new("validate_cmd") begin tmpfile.write(content) - output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` - r = $? + Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + rescue Puppet::ExecutionFailure => detail + msg += "\n#{detail}" + raise Puppet::ParseError, msg ensure - tmpfile.close tmpfile.unlink end - if output - msg += "\nOutput is:\n#{output}" - end - raise Puppet::ParseError, (msg) unless r == 0 end end diff --git a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb index 0730a59..69ea7f4 100644 --- a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb @@ -74,7 +74,7 @@ describe Puppet::Parser::Functions.function(:validate_cmd) do describe "Test output message" do it "validate_cmd('whatever', 'kthnksbye') should fail" do - expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.*not found/ + expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.* returned 1/ end end end -- cgit v1.2.3 From 69248dfd8ab09f6d78054d10c7162bb18ec040e5 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Thu, 7 Feb 2013 08:56:52 +0100 Subject: validate_cmd(): Use Puppet::Util::Execution.execute when available --- lib/puppet/parser/functions/validate_cmd.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions/validate_cmd.rb') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 4f6a766..344a80c 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -32,7 +32,11 @@ module Puppet::Parser::Functions tmpfile = Tempfile.new("validate_cmd") begin tmpfile.write(content) - Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + if Puppet::Util::Execution.respond_to?('execute') + Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}") + else + Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + end rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" raise Puppet::ParseError, msg -- cgit v1.2.3