]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/unit/puppet/type/file_line_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / unit / puppet / type / file_line_spec.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper'
3 require 'tempfile'
4 describe Puppet::Type.type(:file_line) do
5   let :file_line do
6     Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => '/tmp/path')
7   end
8   it 'should accept a line and path' do
9     file_line[:line] = 'my_line'
10     expect(file_line[:line]).to eq('my_line')
11     file_line[:path] = '/my/path'
12     expect(file_line[:path]).to eq('/my/path')
13   end
14   it 'should accept a match regex' do
15     file_line[:match] = '^foo.*$'
16     expect(file_line[:match]).to eq('^foo.*$')
17   end
18   it 'should accept a match regex that does not match the specified line' do
19     expect {
20       Puppet::Type.type(:file_line).new(
21           :name   => 'foo',
22           :path   => '/my/path',
23           :line   => 'foo=bar',
24           :match  => '^bar=blah$'
25     )}.not_to raise_error
26   end
27   it 'should accept a match regex that does match the specified line' do
28     expect {
29       Puppet::Type.type(:file_line).new(
30           :name   => 'foo',
31           :path   => '/my/path',
32           :line   => 'foo=bar',
33           :match  => '^\s*foo=.*$'
34       )}.not_to raise_error
35   end
36   it 'should accept posix filenames' do
37     file_line[:path] = '/tmp/path'
38     expect(file_line[:path]).to eq('/tmp/path')
39   end
40   it 'should not accept unqualified path' do
41     expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
42   end
43   it 'should require that a line is specified' do
44     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
45   end
46   it 'should require that a file is specified' do
47     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
48   end
49   it 'should default to ensure => present' do
50     expect(file_line[:ensure]).to eq :present
51   end
52
53   it "should autorequire the file it manages" do
54     catalog = Puppet::Resource::Catalog.new
55     file = Puppet::Type.type(:file).new(:name => "/tmp/path")
56     catalog.add_resource file
57     catalog.add_resource file_line
58
59     relationship = file_line.autorequire.find do |rel|
60       (rel.source.to_s == "File[/tmp/path]") and (rel.target.to_s == file_line.to_s)
61     end
62     expect(relationship).to be_a Puppet::Relationship
63   end
64
65   it "should not autorequire the file it manages if it is not managed" do
66     catalog = Puppet::Resource::Catalog.new
67     catalog.add_resource file_line
68     expect(file_line.autorequire).to be_empty
69   end
70 end