1 #! /usr/bin/env ruby -S rspec
4 describe Puppet::Type.type(:file_line) do
6 if Puppet::Util::Platform.windows?
13 if Puppet::Util::Platform.windows?
20 Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => tmp_path)
22 it 'should accept a line and path' do
23 file_line[:line] = 'my_line'
24 expect(file_line[:line]).to eq('my_line')
25 file_line[:path] = my_path
26 expect(file_line[:path]).to eq(my_path)
28 it 'should accept a match regex' do
29 file_line[:match] = '^foo.*$'
30 expect(file_line[:match]).to eq('^foo.*$')
32 it 'should accept a match regex that does not match the specified line' do
34 Puppet::Type.type(:file_line).new(
38 :match => '^bar=blah$'
41 it 'should accept a match regex that does match the specified line' do
43 Puppet::Type.type(:file_line).new(
47 :match => '^\s*foo=.*$'
50 it 'should accept utf8 characters' do
52 Puppet::Type.type(:file_line).new(
56 :match => '^ьåя=βļάħ$'
59 it 'should accept double byte characters' do
61 Puppet::Type.type(:file_line).new(
68 it 'should accept posix filenames' do
69 file_line[:path] = tmp_path
70 expect(file_line[:path]).to eq(tmp_path)
72 it 'should not accept unqualified path' do
73 expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
75 it 'should require that a line is specified' do
76 expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path) }.to raise_error(Puppet::Error, /line is a required attribute/)
78 it 'should not require that a line is specified when matching for absence' do
79 expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
81 it 'should require that a file is specified' do
82 expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /path is a required attribute/)
84 it 'should default to ensure => present' do
85 expect(file_line[:ensure]).to eq :present
87 it 'should default to replace => true' do
88 expect(file_line[:replace]).to eq :true
90 it 'should default to encoding => UTF-8' do
91 expect(file_line[:encoding]).to eq 'UTF-8'
93 it 'should accept encoding => iso-8859-1' do
94 expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error
96 it "should autorequire the file it manages" do
97 catalog = Puppet::Resource::Catalog.new
98 file = Puppet::Type.type(:file).new(:name => tmp_path)
99 catalog.add_resource file
100 catalog.add_resource file_line
102 relationship = file_line.autorequire.find do |rel|
103 (rel.source.to_s == "File[#{tmp_path}]") and (rel.target.to_s == file_line.to_s)
105 expect(relationship).to be_a Puppet::Relationship
108 it "should not autorequire the file it manages if it is not managed" do
109 catalog = Puppet::Resource::Catalog.new
110 catalog.add_resource file_line
111 expect(file_line.autorequire).to be_empty