add a new function called tfile
[puppet_common.git] / spec / unit / parser / functions / tfile.rb
1 #! /usr/bin/env ruby
2
3 require File.dirname(__FILE__) + '/../../../spec_helper'
4 require 'mocha'
5
6 describe "the tfile function" do
7
8   before :each do
9     @scope = Puppet::Parser::Scope.new
10   end
11
12   it "should exist" do
13     Puppet::Parser::Functions.function("tfile").should == "function_tfile"
14   end
15
16   it "should raise a ParseError if there is less than 1 arguments" do
17     lambda { @scope.function_tfile([]) }.should( raise_error(Puppet::ParseError))
18   end
19
20   it "should raise a ParseError if there is more than 1 arguments" do
21     lambda { @scope.function_tfile(["bar", "gazonk"]) }.should( raise_error(Puppet::ParseError))
22   end
23
24   describe "when executed properly" do
25
26     before :each do
27        Puppet::Parser::Scope.any_instance.stubs(:function_file).with(['/some_path/aa']).returns("foo1\nfoo2\n")
28     end
29
30     it "should return the content of the file by calling the puppet file function" do
31       File.stubs(:exists?).with('/some_path/aa').returns(true)
32       result = @scope.function_tfile(['/some_path/aa'])
33       result.should == "foo1\nfoo2\n"
34     end
35
36     it "should touch a file if it does not exist" do
37       File.stubs(:exists?).with('/some_path/aa').returns(false)
38       File.stubs(:directory?).with('/some_path').returns(true)
39       FileUtils.expects(:touch).with('/some_path/aa')
40       result = @scope.function_tfile(['/some_path/aa'])
41       result.should == "foo1\nfoo2\n"
42     end
43
44     it "should create the path if it does not exist" do
45       File.stubs(:exists?).with('/some_path/aa').returns(false)
46       File.stubs(:directory?).with('/some_path').returns(false)
47       Puppet::Util.expects(:recmkdir).with("/some_path",0700)
48       FileUtils.expects(:touch).with('/some_path/aa')
49       result = @scope.function_tfile(['/some_path/aa'])
50       result.should == "foo1\nfoo2\n"
51     end
52   end
53
54 end