Also adds rspec tests for generate().

Signed-off-by: Paul Lathrop <[email protected]>
---
 lib/puppet/parser/functions/generate.rb |    6 +++-
 spec/unit/parser/functions/generate.rb  |   41 +++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100755 spec/unit/parser/functions/generate.rb

diff --git a/lib/puppet/parser/functions/generate.rb 
b/lib/puppet/parser/functions/generate.rb
index 18fe883..55161df 100644
--- a/lib/puppet/parser/functions/generate.rb
+++ b/lib/puppet/parser/functions/generate.rb
@@ -1,6 +1,6 @@
 # Runs an external command and returns the results
 Puppet::Parser::Functions::newfunction(:generate, :type => :rvalue,
-        :doc => "Calls an external command on the Puppet master and returns 
+        :doc => "Calls an external command on the Puppet master and returns
         the results of the command.  Any arguments are passed to the external 
command as
         arguments.  If the generator does not exit with return code of 0,
         the generator is considered to have failed and a parse error is
@@ -26,7 +26,9 @@ Puppet::Parser::Functions::newfunction(:generate, :type => 
:rvalue,
             end
 
             begin
-                output = Puppet::Util.execute(args)
+                Dir.chdir(File.dirname(args[0])) do
+                    output = Puppet::Util.execute(args)
+                end
             rescue Puppet::ExecutionFailure => detail
                 raise Puppet::ParseError, "Failed to execute generator %s: %s" 
%
                     [args[0], detail]
diff --git a/spec/unit/parser/functions/generate.rb 
b/spec/unit/parser/functions/generate.rb
new file mode 100755
index 0000000..05c2cb7
--- /dev/null
+++ b/spec/unit/parser/functions/generate.rb
@@ -0,0 +1,41 @@
+#! /usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe "the generate function" do
+
+    before :each do
+        @scope = Puppet::Parser::Scope.new()
+    end
+
+    it "should exist" do
+        Puppet::Parser::Functions.function("generate").should == 
"function_generate"
+    end
+
+    it "should accept a fully-qualified path as a command" do
+        command = File::SEPARATOR + "command"
+        Puppet::Util.expects(:execute).with([command]).returns("yay")
+        lambda { @scope.function_generate([command]) }.should_not 
raise_error(Puppet::ParseError)
+    end
+
+    it "should not accept a relative path as a command" do
+        command = "command"
+        lambda { @scope.function_generate([command]) }.should 
raise_error(Puppet::ParseError)
+    end
+
+    # Really not sure how to implement this test, just sure it needs
+    # to be implemented.
+    it "should not accept a command containing illegal characters"
+
+    it "should not accept a command containing '..'" do
+        command = File::SEPARATOR + "command" + File::SEPARATOR + ".." + 
File::SEPARATOR
+        lambda { @scope.function_generate([command]) }.should 
raise_error(Puppet::ParseError)
+    end
+
+    it "should execute the generate script with the correct working directory" 
do
+        command = File::SEPARATOR + "command"
+        Dir.expects(:chdir).with(File.dirname(command)).yields
+        Puppet::Util.expects(:execute).with([command]).returns("yay")
+        lambda { @scope.function_generate([command]) }.should_not 
raise_error(Puppet::ParseError)
+    end
+end
-- 
1.7.0

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