File: ini_file_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (550 lines) | stat: -rw-r--r-- 13,957 bytes parent folder | download | duplicates (2)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
require 'spec_helper'
require 'stringio'

require 'puppet/settings/ini_file'

describe Puppet::Settings::IniFile do
  # different UTF-8 widths
  # 1-byte A
  # 2-byte ۿ - http://www.fileformat.info/info/unicode/char/06ff/index.htm - 0xDB 0xBF / 219 191
  # 3-byte ᚠ - http://www.fileformat.info/info/unicode/char/16A0/index.htm - 0xE1 0x9A 0xA0 / 225 154 160
  # 4-byte 𠜎 - http://www.fileformat.info/info/unicode/char/2070E/index.htm - 0xF0 0xA0 0x9C 0x8E / 240 160 156 142
  let (:mixed_utf8) { "A\u06FF\u16A0\u{2070E}" } # Aۿᚠ𠜎

  it "preserves the file when no changes are made" do
    original_config = <<-CONFIG
    # comment
    [section]
    name = value
    CONFIG
    config_fh = a_config_file_containing(original_config)

    Puppet::Settings::IniFile.update(config_fh) do; end

    expect(config_fh.string).to eq original_config
  end

  it "adds a set name and value to an empty file" do
    config_fh = a_config_file_containing("")

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("the_section", "name", "value")
    end

    expect(config_fh.string).to eq "[the_section]\nname = value\n"
  end

  it "adds a UTF-8 name and value to an empty file" do
    config_fh = a_config_file_containing("")

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("the_section", mixed_utf8, mixed_utf8.reverse)
    end

    expect(config_fh.string).to eq "[the_section]\n#{mixed_utf8} = #{mixed_utf8.reverse}\n"
  end

  it "adds a [main] section to a file when it's needed" do
    config_fh = a_config_file_containing(<<-CONF)
    [section]
    name = different value
    CONF


    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("main", "name", "value")
    end

    expect(config_fh.string).to eq(<<-CONF)
[main]
name = value
    [section]
    name = different value
    CONF
  end

  it "can update values within a UTF-8 section of an existing file" do
    config_fh = a_config_file_containing(<<-CONF)
    [#{mixed_utf8}]
    foo = default
    CONF

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set(mixed_utf8, 'foo', 'bar')
    end

    expect(config_fh.string).to eq(<<-CONF)
    [#{mixed_utf8}]
    foo = bar
    CONF
  end

  it "preserves comments when writing a new name and value" do
    config_fh = a_config_file_containing("# this is a comment")

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("the_section", "name", "value")
    end

    expect(config_fh.string).to eq "# this is a comment\n[the_section]\nname = value\n"
  end

  it "updates existing names and values in place" do
    config_fh = a_config_file_containing(<<-CONFIG)
    # this is the preceding comment
     [section]
    name = original value
    # this is the trailing comment
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "name", "changed value")
    end

    expect(config_fh.string).to eq <<-CONFIG
    # this is the preceding comment
     [section]
    name = changed value
    # this is the trailing comment
    CONFIG
  end

  it "updates existing empty settings" do
    config_fh = a_config_file_containing(<<-CONFIG)
    # this is the preceding comment
     [section]
    name = 
    # this is the trailing comment
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "name", "changed value")
    end

    expect(config_fh.string).to eq <<-CONFIG
    # this is the preceding comment
     [section]
    name = changed value
    # this is the trailing comment
    CONFIG
  end

  it "can set empty settings" do
    config_fh = a_config_file_containing(<<-CONFIG)
    # this is the preceding comment
     [section]
    name = original value
    # this is the trailing comment
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "name", "")
    end

    expect(config_fh.string).to eq <<-CONFIG
    # this is the preceding comment
     [section]
    name = 
    # this is the trailing comment
    CONFIG
  end

  it "updates existing UTF-8 name / values in place" do
    config_fh = a_config_file_containing(<<-CONFIG)
    # this is the preceding comment
     [section]
    ascii = foo
    A\u06FF\u16A0\u{2070E} = bar
    # this is the trailing comment
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "ascii", mixed_utf8)
      config.set("section", mixed_utf8, mixed_utf8.reverse)
    end

    expect(config_fh.string).to eq <<-CONFIG
    # this is the preceding comment
     [section]
    ascii = #{mixed_utf8}
    #{mixed_utf8} = #{mixed_utf8.reverse}
    # this is the trailing comment
    CONFIG
  end

  it "updates only the value in the selected section" do
    config_fh = a_config_file_containing(<<-CONFIG)
    [other_section]
    name = does not change
    [section]
    name = original value
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "name", "changed value")
    end

    expect(config_fh.string).to eq <<-CONFIG
    [other_section]
    name = does not change
    [section]
    name = changed value
    CONFIG
  end

  it "considers settings found outside a section to be in section 'main'" do
    config_fh = a_config_file_containing(<<-CONFIG)
    name = original value
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("main", "name", "changed value")
    end

    expect(config_fh.string).to eq <<-CONFIG
[main]
    name = changed value
    CONFIG
  end

  it "adds new settings to an existing section" do
    config_fh = a_config_file_containing(<<-CONFIG)
    [section]
    original = value

    # comment about 'other' section
    [other]
    dont = change
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "updated", "new")
    end

    expect(config_fh.string).to eq <<-CONFIG
    [section]
    original = value
updated = new

    # comment about 'other' section
    [other]
    dont = change
    CONFIG
  end

  it "adds a new setting into an existing, yet empty section" do
    config_fh = a_config_file_containing(<<-CONFIG)
    [section]
    [other]
    dont = change
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "updated", "new")
    end

    expect(config_fh.string).to eq <<-CONFIG
    [section]
updated = new
    [other]
    dont = change
    CONFIG
  end

  it "finds settings when the section is split up" do
    config_fh = a_config_file_containing(<<-CONFIG)
    [section]
    name = original value
    [different]
    name = other value
    [section]
    other_name = different original value
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "name", "changed value")
      config.set("section", "other_name", "other changed value")
    end

    expect(config_fh.string).to eq <<-CONFIG
    [section]
    name = changed value
    [different]
    name = other value
    [section]
    other_name = other changed value
    CONFIG
  end

  it "adds a new setting to the appropriate section, when it would be added behind a setting with an identical value in a preceeding section" do
    config_fh = a_config_file_containing(<<-CONFIG)
    [different]
    name = some value
    [section]
    name = some value
    CONFIG

    Puppet::Settings::IniFile.update(config_fh) do |config|
      config.set("section", "new", "new value")
    end

    expect(config_fh.string).to eq <<-CONFIG
    [different]
    name = some value
    [section]
    name = some value
new = new value
    CONFIG
  end

  context 'config with no main section' do
    it 'file does not change when there are no sections or entries' do
      config_fh = a_config_file_containing(<<-CONFIG)
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'missing')
      end

      expect(config_fh.string).to eq <<-CONFIG
      CONFIG
    end

    it 'when there is only 1 entry we can delete it' do
      config_fh = a_config_file_containing(<<-CONFIG)
      base = value
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      CONFIG
    end

    it 'we delete 1 entry from the default section and add the [main] section header' do
      config_fh = a_config_file_containing(<<-CONFIG)
      base = value
      other = another value
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
[main]
      other = another value
      CONFIG
    end

    it 'we add [main] to the config file when attempting to delete a setting in another section' do
      config_fh = a_config_file_containing(<<-CONF)
      name = different value
      CONF

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('section', 'name')
      end

      expect(config_fh.string).to eq(<<-CONF)
[main]
      name = different value
      CONF
    end
  end

  context 'config with 1 section' do
    it 'file does not change when entry to delete does not exist' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = value
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'missing')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      base = value
      CONFIG
    end

    it 'deletes the 1 entry in the section' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = DELETING
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      CONFIG
    end

    it 'deletes the entry and leaves another entry' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = DELETING
      after = value to keep
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      after = value to keep
      CONFIG
    end

    it 'deletes the entry while leaving other entries' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      before = value to keep before
      base = DELETING
      after = value to keep
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      before = value to keep before
      after = value to keep
      CONFIG
    end

    it 'when there are two entries of the same setting name delete one of them' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = value
      base = value
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('main', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      base = value
      CONFIG
    end
  end

  context 'with 2 sections' do
    it 'file does not change when entry to delete does not exist' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = value
      [section]
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('section', 'missing')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      base = value
      [section]
      CONFIG
    end

    it 'deletes the 1 entry in the specified section' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      base = value
      [section]
      base = value
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('section', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      base = value
      [section]
      CONFIG
    end

    it 'deletes the entry while leaving other entries' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      before = value also staying
      base = value staying
      after = value to keep
      [section]
      before value in section keeping
      base = DELETING
      after = value to keep
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('section', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      before = value also staying
      base = value staying
      after = value to keep
      [section]
      before value in section keeping
      after = value to keep
      CONFIG
    end
  end

  context 'with 2 sections' do
    it 'deletes the entry while leaving other entries' do
      config_fh = a_config_file_containing(<<-CONFIG)
      [main]
      before = value also staying
      base = value staying
      after = value to keep
      [section]
      before value in section keeping
      base = DELETING
      after = value to keep
      [otherSection]
      before value in section keeping
      base = value to keep really
      after = value to keep
      CONFIG

      Puppet::Settings::IniFile.update(config_fh) do |config|
        config.delete('section', 'base')
      end

      expect(config_fh.string).to eq <<-CONFIG
      [main]
      before = value also staying
      base = value staying
      after = value to keep
      [section]
      before value in section keeping
      after = value to keep
      [otherSection]
      before value in section keeping
      base = value to keep really
      after = value to keep
      CONFIG
    end
  end

  def a_config_file_containing(text)
    StringIO.new(text.encode(Encoding::UTF_8))
  end
end