3 provider_class = Puppet::Type.type(:file_line).provider(:ruby)
4 # These tests fail on windows when run as part of the rake task. Individually they pass
5 describe provider_class, :unless => Puppet::Util::Platform.windows? do
6 include PuppetlabsSpec::Files
9 tmpfilename('file_line_test')
18 Puppet::Type::File_line.new({
25 provider_class.new(resource)
29 File.open(tmpfile, 'w') do |fh|
34 describe 'line parameter' do
35 context 'when line exists' do
36 let(:content) { 'foo' }
38 it 'detects the line' do
39 expect(provider).to be_exists
42 context 'when line does not exist' do
43 let(:content) { 'foo bar' }
45 it 'requests changes' do
46 expect(provider).not_to be_exists
48 it 'appends the line' do
50 expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo")
55 describe 'match parameter' do
56 let(:params) { { :match => '^bar' } }
58 describe 'does not match line - line does not exist - replacing' do
59 let(:content) { "foo bar\nbar" }
61 it 'requests changes' do
62 expect(provider).not_to be_exists
64 it 'replaces the match' do
66 expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo")
70 describe 'does not match line - line does not exist - appending' do
71 let(:params) { super().merge(:replace => false) }
72 let(:content) { "foo bar\nbar" }
74 it 'does not request changes' do
75 expect(provider).to be_exists
79 context 'when does not match line - line exists' do
80 let(:content) { "foo\nbar" }
82 it 'detects the line' do
83 expect(provider).to be_exists
87 context 'when matches line - line exists' do
88 let(:params) { { :match => '^foo' } }
89 let(:content) { "foo\nbar" }
91 it 'detects the line' do
92 expect(provider).to be_exists
96 context 'when matches line - line does not exist' do
97 let(:params) { { :match => '^foo' } }
98 let(:content) { "foo bar\nbar" }
100 it 'requests changes' do
101 expect(provider).not_to be_exists
103 it 'replaces the match' do
105 expect(File.read(tmpfile).chomp).to eq("foo\nbar")
110 describe 'append_on_no_match' do
113 :append_on_no_match => false,
118 context 'when matching' do
119 let(:content) { "foo1\nbar" }
121 it 'requests changes' do
122 expect(provider).not_to be_exists
124 it 'replaces the match' do
126 expect(File.read(tmpfile).chomp).to eql("foo\nbar")
129 context 'when not matching' do
130 let(:content) { "foo3\nbar" }
132 it 'does not affect the file' do
133 expect(provider).to be_exists
138 describe 'replace_all_matches_not_matching_line' do
139 context 'when replace is false' do
142 :replace_all_matches_not_matching_line => true,
147 it 'raises an error' do
148 expect { provider.exists? }.to raise_error(Puppet::Error, %r{replace must be true})
152 context 'when match matches line - when there are more matches than lines' do
155 :replace_all_matches_not_matching_line => true,
160 let(:content) { "foo\nfoo bar\nbar\nfoo baz" }
162 it 'requests changes' do
163 expect(provider).not_to be_exists
165 it 'replaces the matches' do
167 expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nbar\nfoo")
171 context 'when match matches line - when there are the same matches and lines' do
174 :replace_all_matches_not_matching_line => true,
179 let(:content) { "foo\nfoo\nbar" }
181 it 'does not request changes' do
182 expect(provider).to be_exists
186 context 'when match does not match line - when there are more matches than lines' do
189 :replace_all_matches_not_matching_line => true,
194 let(:content) { "foo\nfoo bar\nbar\nbar baz" }
196 it 'requests changes' do
197 expect(provider).not_to be_exists
199 it 'replaces the matches' do
201 expect(File.read(tmpfile).chomp).to eql("foo\nfoo bar\nfoo\nfoo")
205 context 'when match does not match line - when there are the same matches and lines' do
208 :replace_all_matches_not_matching_line => true,
213 let(:content) { "foo\nfoo\nbar\nbar baz" }
215 it 'requests changes' do
216 expect(provider).not_to be_exists
218 it 'replaces the matches' do
220 expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nfoo\nfoo")
225 context 'when match does not match line - when there are no matches' do
228 :replace_all_matches_not_matching_line => true,
233 let(:content) { "foo\nfoo bar" }
235 it 'does not request changes' do
236 expect(provider).to be_exists
240 context 'when match does not match line - when there are no matches or lines' do
243 :replace_all_matches_not_matching_line => true,
248 let(:content) { 'foo bar' }
250 it 'requests changes' do
251 expect(provider).not_to be_exists
253 it 'appends the line' do
255 expect(File.read(tmpfile).chomp).to eql("foo bar\nfoo")