Skip to content

Commit 9340597

Browse files
committed
add more tests
1 parent 87d2b2a commit 9340597

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

spec/unit/puppet/provider/ini_setting/ruby_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,5 +1674,61 @@ def self.file_path
16741674
provider.create
16751675
validate_file(expected_content_four, tmpfile)
16761676
end
1677+
1678+
expected_content_five = <<-EOS
1679+
[section]
1680+
bar = barvalue
1681+
1682+
[section2]
1683+
foo = value
1684+
bar = barvalue
1685+
foo = value2
1686+
1687+
[section3]
1688+
EOS
1689+
1690+
it 'removes a multi-value setting with ensure => absent' do
1691+
resource = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section', setting: 'foo', ensure: 'absent'))
1692+
provider = described_class.new(resource)
1693+
expect(provider.exists?).to be true
1694+
provider.destroy
1695+
validate_file(expected_content_five, tmpfile)
1696+
end
1697+
1698+
expected_content_six = <<-EOS
1699+
[section]
1700+
foo = foovalue
1701+
foo = foovalue2
1702+
foo = foovalue3
1703+
foo = foovalue4
1704+
bar = barvalue
1705+
1706+
[section2]
1707+
foo = value
1708+
bar = barvalue
1709+
foo = value2
1710+
1711+
[section3]
1712+
baz = newvalue
1713+
EOS
1714+
1715+
it 'correctly updates section3 line numbers after modifying section with multi-values' do
1716+
# First reduce section's foo from 4 values to 1, then add to section3
1717+
resource1 = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section', setting: 'foo', value: 'foovalue'))
1718+
provider1 = described_class.new(resource1)
1719+
provider1.create
1720+
1721+
# Re-read the file with new provider to add setting to section3
1722+
resource2 = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section3', setting: 'baz', value: 'newvalue'))
1723+
provider2 = described_class.new(resource2)
1724+
provider2.create
1725+
1726+
# Now reset file and do both operations to verify section line tracking
1727+
File.write(tmpfile, orig_content)
1728+
resource3 = Puppet::Type::Ini_setting.new(common_params.merge(section: 'section3', setting: 'baz', value: 'newvalue'))
1729+
provider3 = described_class.new(resource3)
1730+
provider3.create
1731+
validate_file(expected_content_six, tmpfile)
1732+
end
16771733
end
16781734
end

spec/unit/puppet/provider/ini_subsetting/ruby_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,60 @@ def validate_file(expected_content, tmpfile)
360360
expect(validate_file(expected_content_two, tmpfile)).to be_truthy
361361
end
362362
end
363+
364+
context 'when the parent setting has multiple values (same key repeated)' do
365+
let(:common_params) do
366+
{
367+
title: 'ini_subsetting_multivalue_test',
368+
path: tmpfile,
369+
section: 'main',
370+
key_val_separator: '=',
371+
}
372+
end
373+
374+
let(:orig_content) do
375+
<<-INIFILE
376+
[main]
377+
setting="value1 value2"
378+
setting="value3 value4"
379+
other=data
380+
INIFILE
381+
end
382+
383+
it 'reads subsetting from the first occurrence of a multi-value key' do
384+
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'setting', subsetting: 'value1', quote_char: '"'))
385+
provider = described_class.new(resource)
386+
expect(provider.exists?).to eq ''
387+
end
388+
389+
# NOTE: When ini_subsetting modifies a key that has multiple values,
390+
# it collapses them to a single value (the first one, modified).
391+
# This is expected behavior per the multi-value PR design.
392+
expected_content_one = <<-INIFILE
393+
[main]
394+
setting="value1 value2 newval"
395+
other=data
396+
INIFILE
397+
398+
it 'modifies the first occurrence and collapses multi-value keys when adding a subsetting' do
399+
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'setting', subsetting: 'newval', value: '', quote_char: '"'))
400+
provider = described_class.new(resource)
401+
expect(provider.exists?).to be_nil
402+
provider.create
403+
validate_file(expected_content_one, tmpfile)
404+
end
405+
406+
expected_content_two = <<-INIFILE
407+
[main]
408+
setting="value2"
409+
other=data
410+
INIFILE
411+
412+
it 'removes a subsetting and collapses multi-value keys' do
413+
resource = Puppet::Type::Ini_subsetting.new(common_params.merge(setting: 'setting', subsetting: 'value1', quote_char: '"'))
414+
provider = described_class.new(resource)
415+
provider.destroy
416+
validate_file(expected_content_two, tmpfile)
417+
end
418+
end
363419
end

0 commit comments

Comments
 (0)