|
46 | 46 | @tmpfile = tmp.path |
47 | 47 | tmp.close! |
48 | 48 | @resource = Puppet::Type::File_line.new( |
49 | | - { |
50 | | - :name => 'foo', |
51 | | - :path => @tmpfile, |
52 | | - :line => 'foo = bar', |
53 | | - :match => '^foo\s*=.*$', |
54 | | - } |
| 49 | + { |
| 50 | + :name => 'foo', |
| 51 | + :path => @tmpfile, |
| 52 | + :line => 'foo = bar', |
| 53 | + :match => '^foo\s*=.*$', |
| 54 | + } |
55 | 55 | ) |
56 | 56 | @provider = provider_class.new(@resource) |
57 | 57 | end |
|
69 | 69 | it 'should replace all lines that matches' do |
70 | 70 | @resource = Puppet::Type::File_line.new( |
71 | 71 | { |
72 | | - :name => 'foo', |
73 | | - :path => @tmpfile, |
74 | | - :line => 'foo = bar', |
75 | | - :match => '^foo\s*=.*$', |
76 | | - :multiple => true |
| 72 | + :name => 'foo', |
| 73 | + :path => @tmpfile, |
| 74 | + :line => 'foo = bar', |
| 75 | + :match => '^foo\s*=.*$', |
| 76 | + :multiple => true, |
77 | 77 | } |
78 | 78 | ) |
79 | 79 | @provider = provider_class.new(@resource) |
|
89 | 89 | expect { |
90 | 90 | @resource = Puppet::Type::File_line.new( |
91 | 91 | { |
92 | | - :name => 'foo', |
93 | | - :path => @tmpfile, |
94 | | - :line => 'foo = bar', |
95 | | - :match => '^foo\s*=.*$', |
96 | | - :multiple => 'asgadga' |
| 92 | + :name => 'foo', |
| 93 | + :path => @tmpfile, |
| 94 | + :line => 'foo = bar', |
| 95 | + :match => '^foo\s*=.*$', |
| 96 | + :multiple => 'asgadga', |
97 | 97 | } |
98 | 98 | ) |
99 | 99 | }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./) |
|
140 | 140 | let :provider do |
141 | 141 | provider_class.new(resource) |
142 | 142 | end |
143 | | - |
| 143 | + context 'match and after set' do |
| 144 | + shared_context 'resource_create' do |
| 145 | + let(:match) { '^foo2$' } |
| 146 | + let(:after) { '^foo1$' } |
| 147 | + let(:resource) { |
| 148 | + Puppet::Type::File_line.new( |
| 149 | + { |
| 150 | + :name => 'foo', |
| 151 | + :path => @tmpfile, |
| 152 | + :line => 'inserted = line', |
| 153 | + :after => after, |
| 154 | + :match => match, |
| 155 | + } |
| 156 | + ) |
| 157 | + } |
| 158 | + end |
| 159 | + before :each do |
| 160 | + File.open(@tmpfile, 'w') do |fh| |
| 161 | + fh.write("foo1\nfoo2\nfoo = baz") |
| 162 | + end |
| 163 | + end |
| 164 | + describe 'inserts at match' do |
| 165 | + include_context 'resource_create' |
| 166 | + it { |
| 167 | + provider.create |
| 168 | + expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz") |
| 169 | + } |
| 170 | + end |
| 171 | + describe 'inserts a new line after when no match' do |
| 172 | + include_context 'resource_create' do |
| 173 | + let(:match) { '^nevergoingtomatch$' } |
| 174 | + end |
| 175 | + it { |
| 176 | + provider.create |
| 177 | + expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz") |
| 178 | + } |
| 179 | + end |
| 180 | + describe 'append to end of file if no match for both after and match' do |
| 181 | + include_context 'resource_create' do |
| 182 | + let(:match) { '^nevergoingtomatch$' } |
| 183 | + let(:after) { '^stillneverafter' } |
| 184 | + end |
| 185 | + it { |
| 186 | + provider.create |
| 187 | + expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line") |
| 188 | + } |
| 189 | + end |
| 190 | + end |
144 | 191 | context 'with one line matching the after expression' do |
145 | 192 | before :each do |
146 | 193 | File.open(@tmpfile, 'w') do |fh| |
|
194 | 241 | @tmpfile = tmp.path |
195 | 242 | tmp.close! |
196 | 243 | @resource = Puppet::Type::File_line.new( |
197 | | - {:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' } |
| 244 | + { |
| 245 | + :name => 'foo', |
| 246 | + :path => @tmpfile, |
| 247 | + :line => 'foo', |
| 248 | + :ensure => 'absent', |
| 249 | + } |
198 | 250 | ) |
199 | 251 | @provider = provider_class.new(@resource) |
200 | 252 | end |
|
0 commit comments