File tree Expand file tree Collapse file tree 4 files changed +80
-7
lines changed
Expand file tree Collapse file tree 4 files changed +80
-7
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,9 @@ def create
99 if resource [ :match ]
1010 handle_create_with_match
1111 elsif resource [ :after ]
12- handle_create_with_after
12+ handle_create_with_position :after
13+ elsif resource [ :before ]
14+ handle_create_with_position :before
1315 else
1416 append_line
1517 end
@@ -49,29 +51,29 @@ def handle_create_with_match()
4951 end
5052 end
5153
52- def handle_create_with_after
53- regex = Regexp . new ( resource [ :after ] )
54+ def handle_create_with_position ( position )
55+ regex = resource [ position ] ? Regexp . new ( resource [ position ] ) : nil
5456
5557 count = lines . count { |l | l . match ( regex ) }
5658
5759 case count
58- when 1 # find the line to put our line after
60+ when 1 # find the line to put our line before/ after
5961 File . open ( resource [ :path ] , 'w' ) do |fh |
6062 lines . each do |l |
61- fh . puts ( l )
63+ fh . puts ( l ) if position == :after
6264 if regex . match ( l ) then
6365 fh . puts ( resource [ :line ] )
6466 end
67+ fh . puts ( l ) if position == :before
6568 end
6669 end
6770 when 0 # append the line to the end of the file
6871 append_line
6972 else
70- raise Puppet ::Error , "#{ count } lines match pattern '#{ resource [ :after ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
73+ raise Puppet ::Error , "#{ count } lines match pattern '#{ resource [ position ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
7174 end
7275 end
7376
74- ##
7577 # append the line to the file.
7678 #
7779 # @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+
4953 newparam ( :line ) do
5054 desc 'The line to be appended to the file located by the path parameter.'
5155 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
186245 end
187246
188247 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
1826 it 'should not accept a match regex that does not match the specified line' do
1927 expect {
2028 Puppet ::Type . type ( :file_line ) . new (
You can’t perform that action at this time.
0 commit comments