summaryrefslogtreecommitdiff
path: root/spec/unit/parser/functions/file_spec.rb
blob: a69d5234a55b6fafadc9b1b48fbc8e8e86b7e10f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'puppet_spec/files'

describe "the 'file' function" do
  include PuppetSpec::Files

  before :all do
    Puppet::Parser::Functions.autoloader.loadall
  end

  let :node     do Puppet::Node.new('localhost') end
  let :compiler do Puppet::Parser::Compiler.new(node) end
  let :scope    do Puppet::Parser::Scope.new(:compiler => compiler) end

  it "should exist" do
    Puppet::Parser::Functions.function("file").should == "function_file"
  end

  def with_file_content(content)
    path = tmpfile('file-function')
    file = File.new(path, 'w')
    file.sync = true
    file.print content
    yield path
  end

  it "should read a file" do
    with_file_content('file content') do |name|
      scope.function_file([name]).should == "file content"
    end
  end

  it "should return the first file if given two files" do
    with_file_content('one') do |one|
      with_file_content('two') do |two|
        scope.function_file([one, two]).should == "one"
      end
    end
  end

  it "should not fail when some files are absent" do
    expect {
      with_file_content('one') do |one|
        scope.function_file([make_absolute("/should-not-exist"), one]).should == 'one'
      end
    }.should_not raise_error
  end

  it "should fail when all files are absent" do
    expect { scope.function_file(['one']) }.to raise_error Puppet::ParseError
  end
end