Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / unit / puppet / type / file_line_spec.rb
1 require 'spec_helper'
2 require 'tempfile'
3 describe Puppet::Type.type(:file_line) do
4   let :tmp_path do
5     if Puppet::Util::Platform.windows?
6       'C:\tmp\path'
7     else
8       '/tmp/path'
9     end
10   end
11   let :my_path do
12     if Puppet::Util::Platform.windows?
13       'C:\my\path'
14     else
15       '/my/path'
16     end
17   end
18   let :file_line do
19     Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => tmp_path)
20   end
21
22   it 'accepts a line' do
23     file_line[:line] = 'my_line'
24     expect(file_line[:line]).to eq('my_line')
25   end
26   it 'accepts a path' do
27     file_line[:path] = my_path
28     expect(file_line[:path]).to eq(my_path)
29   end
30   it 'accepts a match regex' do
31     file_line[:match] = '^foo.*$'
32     expect(file_line[:match]).to eq('^foo.*$')
33   end
34
35   it 'accepts a match regex that does not match the specified line' do
36     expect {
37       Puppet::Type.type(:file_line).new(
38         :name => 'foo', :path => my_path, :line => 'foo=bar', :match => '^bar=blah$',
39       )
40     }.not_to raise_error
41   end
42   it 'accepts a match regex that does match the specified line' do
43     expect {
44       Puppet::Type.type(:file_line).new(
45         :name => 'foo', :path => my_path, :line => 'foo=bar', :match => '^\s*foo=.*$',
46       )
47     }.not_to raise_error
48   end
49   it 'accepts utf8 characters' do
50     expect {
51       Puppet::Type.type(:file_line).new(
52         :name => 'ƒồỗ', :path => my_path, :line => 'ƒồỗ=ьåя', :match => '^ьåя=βļάħ$',
53       )
54     }.not_to raise_error
55   end
56   it 'accepts double byte characters' do
57     expect {
58       Puppet::Type.type(:file_line).new(
59         :name => 'フーバー', :path => my_path, :line => 'この=それ', :match => '^この=ああ$',
60       )
61     }.not_to raise_error
62   end
63   it 'accepts posix filenames' do
64     file_line[:path] = tmp_path
65     expect(file_line[:path]).to eq(tmp_path)
66   end
67   it 'does not accept unqualified path' do
68     expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, %r{File paths must be fully qualified})
69   end
70   it 'requires that a line is specified' do
71     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path) }.to raise_error(Puppet::Error, %r{line is a required attribute})
72   end
73   it 'does not require that a line is specified when matching for absence' do
74     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error # rubocop:disable Metrics/LineLength
75   end
76   it 'although if a line is specified anyway when matching for absence it still works and the line is silently ignored' do
77     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :line => 'i_am_irrelevant', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error # rubocop:disable Metrics/LineLength
78   end
79   it 'requires that a file is specified' do
80     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, %r{path is a required attribute})
81   end
82   it 'defaults to ensure => present' do
83     expect(file_line[:ensure]).to eq :present
84   end
85   it 'defaults to replace => true' do
86     expect(file_line[:replace]).to eq :true
87   end
88   it 'defaults to encoding => UTF-8' do
89     expect(file_line[:encoding]).to eq 'UTF-8'
90   end
91   it 'accepts encoding => iso-8859-1' do
92     expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error
93   end
94
95   it 'autorequires the file it manages' do
96     catalog = Puppet::Resource::Catalog.new
97     file = Puppet::Type.type(:file).new(:name => tmp_path)
98     catalog.add_resource file
99     catalog.add_resource file_line
100     relationship = file_line.autorequire.find do |rel|
101       (rel.source.to_s == "File[#{tmp_path}]") && (rel.target.to_s == file_line.to_s)
102     end
103     expect(relationship).to be_a Puppet::Relationship
104   end
105
106   it 'does not autorequire the file it manages if it is not managed' do
107     catalog = Puppet::Resource::Catalog.new
108     catalog.add_resource file_line
109     expect(file_line.autorequire).to be_empty
110   end
111 end