File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -9,9 +9,7 @@ def create
99 if resource [ :match ]
1010 handle_create_with_match
1111 elsif resource [ :after ]
12- handle_create_with_position :after
13- elsif resource [ :before ]
14- handle_create_with_position :before
12+ handle_create_with_after
1513 else
1614 append_line
1715 end
@@ -51,29 +49,29 @@ def handle_create_with_match()
5149 end
5250 end
5351
54- def handle_create_with_position ( position )
55- regex = resource [ position ] ? Regexp . new ( resource [ position ] ) : nil
52+ def handle_create_with_after
53+ regex = Regexp . new ( resource [ :after ] )
5654
5755 count = lines . count { |l | l . match ( regex ) }
5856
5957 case count
60- when 1 # find the line to put our line before/ after
58+ when 1 # find the line to put our line after
6159 File . open ( resource [ :path ] , 'w' ) do |fh |
6260 lines . each do |l |
63- fh . puts ( l ) if position == :after
61+ fh . puts ( l )
6462 if regex . match ( l ) then
6563 fh . puts ( resource [ :line ] )
6664 end
67- fh . puts ( l ) if position == :before
6865 end
6966 end
7067 when 0 # append the line to the end of the file
7168 append_line
7269 else
73- raise Puppet ::Error , "#{ count } lines match pattern '#{ resource [ position ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
70+ raise Puppet ::Error , "#{ count } lines match pattern '#{ resource [ :after ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
7471 end
7572 end
7673
74+ ##
7775 # append the line to the file.
7876 #
7977 # @api private
Original file line number Diff line number Diff line change 4646 desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)'
4747 end
4848
49- newparam ( :before ) do
50- desc 'An optional value used to specify the line before which we will add any new lines. (Existing lines are added in place)'
51- end
52-
5349 newparam ( :line ) do
5450 desc 'The line to be appended to the file located by the path parameter.'
5551 end
Original file line number Diff line number Diff line change 183183 end
184184 end
185185 end
186-
187- describe 'using before' do
188- let :resource do
189- Puppet ::Type ::File_line . new (
190- {
191- :name => 'foo' ,
192- :path => @tmpfile ,
193- :line => 'inserted = line' ,
194- :before => '^foo1' ,
195- }
196- )
197- end
198-
199- let :provider do
200- provider_class . new ( resource )
201- end
202-
203- context 'with one line matching the before expression' do
204- before :each do
205- File . open ( @tmpfile , 'w' ) do |fh |
206- fh . write ( "foo1\n foo = blah\n foo2\n foo = baz" )
207- end
208- end
209-
210- it 'inserts the specified line before the line matching the "before" expression' do
211- provider . create
212- File . read ( @tmpfile ) . chomp . should eql ( "inserted = line\n foo1\n foo = blah\n foo2\n foo = baz" )
213- end
214- end
215-
216- context 'with two lines matching the before expression' do
217- before :each do
218- File . open ( @tmpfile , 'w' ) do |fh |
219- fh . write ( "foo1\n foo = blah\n foo2\n foo1\n foo = baz" )
220- end
221- end
222-
223- it 'errors out stating "One or no line must match the pattern"' do
224- expect { provider . create } . to raise_error ( Puppet ::Error , /One or no line must match the pattern/ )
225- end
226- end
227-
228- context 'with no lines matching the after expression' do
229- let :content do
230- "foo3\n foo = blah\n foo2\n foo = baz\n "
231- end
232-
233- before :each do
234- File . open ( @tmpfile , 'w' ) do |fh |
235- fh . write ( content )
236- end
237- end
238-
239- it 'appends the specified line to the file' do
240- provider . create
241- File . read ( @tmpfile ) . should eq ( content << resource [ :line ] << "\n " )
242- end
243- end
244- end
245186 end
246187
247188 context "when removing" do
Original file line number Diff line number Diff line change 1515 file_line [ :match ] = '^foo.*$'
1616 file_line [ :match ] . should == '^foo.*$'
1717 end
18- it 'should accept an after regex' do
19- file_line [ :after ] = '^foo.*$'
20- file_line [ :after ] . should == '^foo.*$'
21- end
22- it 'should accept a before regex' do
23- file_line [ :before ] = '^foo.*$'
24- file_line [ :before ] . should == '^foo.*$'
25- end
2618 it 'should not accept a match regex that does not match the specified line' do
2719 expect {
2820 Puppet ::Type . type ( :file_line ) . new (
You can’t perform that action at this time.
0 commit comments