150149b26af90f567cc4656ef93664221185261a
[mirror/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 :tmp_path do
6     if Puppet::Util::Platform.windows?
7       'C:\tmp\path'
8     else
9       '/tmp/path'
10     end
11   end
12   let :my_path do
13     if Puppet::Util::Platform.windows?
14       'C:\my\path'
15     else
16       '/my/path'
17     end
18   end
19   let :file_line do
20     Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => tmp_path)
21   end
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)
27   end
28   it 'should accept a match regex' do
29     file_line[:match] = '^foo.*$'
30     expect(file_line[:match]).to eq('^foo.*$')
31   end
32   it 'should accept a match regex that does not match the specified line' do
33     expect {
34       Puppet::Type.type(:file_line).new(
35           :name   => 'foo',
36           :path   => my_path,
37           :line   => 'foo=bar',
38           :match  => '^bar=blah$'
39     )}.not_to raise_error
40   end
41   it 'should accept a match regex that does match the specified line' do
42     expect {
43       Puppet::Type.type(:file_line).new(
44           :name   => 'foo',
45           :path   => my_path,
46           :line   => 'foo=bar',
47           :match  => '^\s*foo=.*$'
48       )}.not_to raise_error
49   end
50   it 'should accept utf8 characters' do
51     expect {
52       Puppet::Type.type(:file_line).new(
53           :name   => 'ƒồỗ',
54           :path   => my_path,
55           :line   => 'ƒồỗ=ьåя',
56           :match  => '^ьåя=βļάħ$'
57       )}.not_to raise_error
58   end
59   it 'should accept double byte characters' do
60     expect {
61       Puppet::Type.type(:file_line).new(
62           :name   => 'フーバー',
63           :path   => my_path,
64           :line   => 'この=それ',
65           :match  => '^この=ああ$'
66       )}.not_to raise_error
67   end
68   it 'should accept posix filenames' do
69     file_line[:path] = tmp_path
70     expect(file_line[:path]).to eq(tmp_path)
71   end
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/)
74   end
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/)
77   end
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
80   end
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/)
83   end
84   it 'should default to ensure => present' do
85     expect(file_line[:ensure]).to eq :present
86   end
87   it 'should default to replace => true' do
88     expect(file_line[:replace]).to eq :true
89   end
90   it 'should default to encoding => UTF-8' do
91     expect(file_line[:encoding]).to eq 'UTF-8'
92   end
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
95   end
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
101
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)
104     end
105     expect(relationship).to be_a Puppet::Relationship
106   end
107
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
112   end
113 end