1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
require 'spec_helper'
require 'puppet/util/inifile'
describe Puppet::Util::IniConfig::Section do
subject { described_class.new('testsection', '/some/imaginary/file') }
describe "determining if the section is dirty" do
it "is not dirty on creation" do
expect(subject).to_not be_dirty
end
it "is dirty if a key is changed" do
subject['hello'] = 'world'
expect(subject).to be_dirty
end
it "is dirty if the section has been explicitly marked as dirty" do
subject.mark_dirty
expect(subject).to be_dirty
end
it "is dirty if the section is marked for deletion" do
subject.destroy = true
expect(subject).to be_dirty
end
it "is clean if the section has been explicitly marked as clean" do
subject['hello'] = 'world'
subject.mark_clean
expect(subject).to_not be_dirty
end
end
describe "reading an entry" do
it "returns nil if the key is not present" do
expect(subject['hello']).to be_nil
end
it "returns the value if the key is specified" do
subject.entries << ['hello', 'world']
expect(subject['hello']).to eq 'world'
end
it "ignores comments when looking for a match" do
subject.entries << '#this = comment'
expect(subject['#this']).to be_nil
end
end
describe "formatting the section" do
it "prefixes the output with the section header" do
expect(subject.format).to eq "[testsection]\n"
end
it "restores comments and blank lines" do
subject.entries << "#comment\n"
subject.entries << " "
expect(subject.format).to eq(
"[testsection]\n" +
"#comment\n" +
" "
)
end
it "adds all keys that have values" do
subject.entries << ['somekey', 'somevalue']
expect(subject.format).to eq("[testsection]\nsomekey=somevalue\n")
end
it "excludes keys that have a value of nil" do
subject.entries << ['empty', nil]
expect(subject.format).to eq("[testsection]\n")
end
it "preserves the order of the section" do
subject.entries << ['firstkey', 'firstval']
subject.entries << "# I am a comment, hear me roar\n"
subject.entries << ['secondkey', 'secondval']
expect(subject.format).to eq(
"[testsection]\n" +
"firstkey=firstval\n" +
"# I am a comment, hear me roar\n" +
"secondkey=secondval\n"
)
end
it "is empty if the section is marked for deletion" do
subject.entries << ['firstkey', 'firstval']
subject.destroy = true
expect(subject.format).to eq('')
end
end
end
describe Puppet::Util::IniConfig::PhysicalFile do
subject { described_class.new('/some/nonexistent/file') }
let(:first_sect) do
sect = Puppet::Util::IniConfig::Section.new('firstsection', '/some/imaginary/file')
sect.entries << "# comment\n" << ['onefish', 'redfish'] << "\n"
sect
end
let(:second_sect) do
sect = Puppet::Util::IniConfig::Section.new('secondsection', '/some/imaginary/file')
sect.entries << ['twofish', 'bluefish']
sect
end
describe "when reading a file" do
it "raises an error if the file does not exist" do
subject.filetype.stubs(:read)
expect {
subject.read
}.to raise_error(%r[Cannot read nonexistent file .*/some/nonexistent/file])
end
it "passes the contents of the file to #parse" do
subject.filetype.stubs(:read).returns "[section]"
subject.expects(:parse).with("[section]")
subject.read
end
end
describe "when parsing a file" do
describe "parsing sections" do
it "creates new sections the first time that the section is found" do
text = "[mysect]\n"
subject.parse(text)
expect(subject.contents).to have(1).items
sect = subject.contents[0]
expect(sect.name).to eq "mysect"
end
it "raises an error if a section is redefined in the file" do
text = "[mysect]\n[mysect]\n"
expect {
subject.parse(text)
}.to raise_error(Puppet::Util::IniConfig::IniParseError,
/Section "mysect" is already defined, cannot redefine/)
end
it "raises an error if a section is redefined in the file collection" do
subject.file_collection = stub('file collection', :get_section => true)
text = "[mysect]\n[mysect]\n"
expect {
subject.parse(text)
}.to raise_error(Puppet::Util::IniConfig::IniParseError,
/Section "mysect" is already defined, cannot redefine/)
end
end
describe "parsing properties" do
it "raises an error if the property is not within a section" do
text = "key=val\n"
expect {
subject.parse(text)
}.to raise_error(Puppet::Util::IniConfig::IniParseError,
/Property with key "key" outside of a section/)
end
it "adds the property to the current section" do
text = "[main]\nkey=val\n"
subject.parse(text)
expect(subject.contents).to have(1).items
sect = subject.contents[0]
expect(sect['key']).to eq "val"
end
end
describe "parsing line continuations" do
it "adds the continued line to the last parsed property" do
text = "[main]\nkey=val\n moreval"
subject.parse(text)
expect(subject.contents).to have(1).items
sect = subject.contents[0]
expect(sect['key']).to eq "val\n moreval"
end
end
describe "parsing comments and whitespace" do
it "treats # as a comment leader" do
text = "# octothorpe comment"
subject.parse(text)
expect(subject.contents).to eq ["# octothorpe comment"]
end
it "treats ; as a comment leader" do
text = "; semicolon comment"
subject.parse(text)
expect(subject.contents).to eq ["; semicolon comment"]
end
it "treates 'rem' as a comment leader" do
text = "rem rapid eye movement comment"
subject.parse(text)
expect(subject.contents).to eq ["rem rapid eye movement comment"]
end
it "stores comments and whitespace in a section in the correct section" do
text = "[main]\n; main section comment"
subject.parse(text)
sect = subject.get_section("main")
expect(sect.entries).to eq ["; main section comment"]
end
end
end
it "can return all sections" do
text = "[first]\n" +
"; comment\n" +
"[second]\n" +
"key=value"
subject.parse(text)
sections = subject.sections
expect(sections).to have(2).items
expect(sections[0].name).to eq "first"
expect(sections[1].name).to eq "second"
end
it "can retrieve a specific section" do
text = "[first]\n" +
"; comment\n" +
"[second]\n" +
"key=value"
subject.parse(text)
section = subject.get_section("second")
expect(section.name).to eq "second"
expect(section["key"]).to eq "value"
end
describe "formatting" do
it "concatenates each formatted section in order" do
subject.contents << first_sect << second_sect
expected = "[firstsection]\n" +
"# comment\n" +
"onefish=redfish\n" +
"\n" +
"[secondsection]\n" +
"twofish=bluefish\n"
expect(subject.format).to eq expected
end
it "includes comments that are not within a section" do
subject.contents << "# This comment is not in a section\n" << first_sect << second_sect
expected = "# This comment is not in a section\n" +
"[firstsection]\n" +
"# comment\n" +
"onefish=redfish\n" +
"\n" +
"[secondsection]\n" +
"twofish=bluefish\n"
expect(subject.format).to eq expected
end
it "excludes sections that are marked to be destroyed" do
subject.contents << first_sect << second_sect
first_sect.destroy = true
expected = "[secondsection]\n" + "twofish=bluefish\n"
expect(subject.format).to eq expected
end
end
describe "storing the file" do
describe "with empty contents" do
describe "and destroy_empty is true" do
before { subject.destroy_empty = true }
it "removes the file if there are no sections" do
File.expects(:unlink)
subject.store
end
it "removes the file if all sections are marked to be destroyed" do
subject.contents << first_sect << second_sect
first_sect.destroy = true
second_sect.destroy = true
File.expects(:unlink)
subject.store
end
it "doesn't remove the file if not all sections are marked to be destroyed" do
subject.contents << first_sect << second_sect
first_sect.destroy = true
second_sect.destroy = false
File.expects(:unlink).never
subject.filetype.stubs(:write)
subject.store
end
end
it "rewrites the file if destroy_empty is false" do
subject.contents << first_sect << second_sect
first_sect.destroy = true
second_sect.destroy = true
File.expects(:unlink).never
subject.stubs(:format).returns "formatted"
subject.filetype.expects(:write).with("formatted")
subject.store
end
end
it "rewrites the file if any section is dirty" do
subject.contents << first_sect << second_sect
first_sect.mark_dirty
second_sect.mark_clean
subject.stubs(:format).returns "formatted"
subject.filetype.expects(:write).with("formatted")
subject.store
end
it "doesn't modify the file if all sections are clean" do
subject.contents << first_sect << second_sect
first_sect.mark_clean
second_sect.mark_clean
subject.stubs(:format).returns "formatted"
subject.filetype.expects(:write).never
subject.store
end
end
end
describe Puppet::Util::IniConfig::FileCollection do
let(:path_a) { '/some/nonexistent/file/a' }
let(:path_b) { '/some/nonexistent/file/b' }
let(:file_a) { Puppet::Util::IniConfig::PhysicalFile.new(path_a) }
let(:file_b) { Puppet::Util::IniConfig::PhysicalFile.new(path_b) }
let(:sect_a1) { Puppet::Util::IniConfig::Section.new('sect_a1', path_a) }
let(:sect_a2) { Puppet::Util::IniConfig::Section.new('sect_a2', path_a) }
let(:sect_b1) { Puppet::Util::IniConfig::Section.new('sect_b1', path_b) }
let(:sect_b2) { Puppet::Util::IniConfig::Section.new('sect_b2', path_b) }
before do
file_a.contents << sect_a1 << sect_a2
file_b.contents << sect_b1 << sect_b2
end
describe "reading a file" do
let(:stub_file) { stub('Physical file') }
it "creates a new PhysicalFile and uses that to read the file" do
stub_file.expects(:read)
stub_file.expects(:file_collection=)
Puppet::Util::IniConfig::PhysicalFile.expects(:new).with(path_a).returns stub_file
subject.read(path_a)
end
it "stores the PhysicalFile and the path to the file" do
stub_file.stubs(:read)
stub_file.stubs(:file_collection=)
Puppet::Util::IniConfig::PhysicalFile.stubs(:new).with(path_a).returns stub_file
subject.read(path_a)
path, physical_file = subject.files.first
expect(path).to eq(path_a)
expect(physical_file).to eq stub_file
end
end
describe "storing all files" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "stores all files in the collection" do
file_a.expects(:store).once
file_b.expects(:store).once
subject.store
end
end
describe "iterating over sections" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "yields every section from every file" do
[sect_a1, sect_a2, sect_b1, sect_b2].each do |sect|
sect.expects(:touch).once
end
subject.each_section do |sect|
sect.touch
end
end
end
describe "iterating over files" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "yields the path to every file in the collection" do
seen = []
subject.each_file do |file|
seen << file
end
expect(seen).to include(path_a)
expect(seen).to include(path_b)
end
end
describe "retrieving a specific section" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "retrieves the first section defined" do
expect(subject.get_section('sect_b1')).to eq sect_b1
end
it "returns nil if there was no section with the given name" do
expect(subject.get_section('nope')).to be_nil
end
it "allows #[] to be used as an alias to #get_section" do
expect(subject['b2']).to eq subject.get_section('b2')
end
end
describe "checking if a section has been defined" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "is true if a section with the given name is defined" do
expect(subject.include?('sect_a1')).to be_true
end
it "is false if a section with the given name can't be found" do
expect(subject.include?('nonexistent')).to be_false
end
end
describe "adding a new section" do
before do
subject.files[path_a] = file_a
subject.files[path_b] = file_b
end
it "adds the section to the appropriate file" do
file_a.expects(:add_section).with('newsect')
subject.add_section('newsect', path_a)
end
end
end
|