3 require 'puppet/util/ini_file'
5 describe Puppet::Util::IniFile do
6 let(:subject) { Puppet::Util::IniFile.new("/my/ini/file/path") }
9 File.should_receive(:file?).with("/my/ini/file/path") { true }
10 described_class.should_receive(:readlines).once.with("/my/ini/file/path") do
15 context "when parsing a file" do
16 let(:sample_content) {
20 ; This is also a comment
29 ; commented = out setting
32 zot = multi word value
33 xyzzy['thing1']['thing2']=xyzzyvalue
39 it "should parse the correct number of sections" do
40 # there is always a "global" section, so our count should be 3.
41 subject.section_names.length.should == 3
44 it "should parse the correct section_names" do
45 # there should always be a "global" section named "" at the beginning of the list
46 subject.section_names.should == ["", "section1", "section2"]
49 it "should expose settings for sections" do
50 subject.get_settings("section1").should == {
56 subject.get_settings("section2").should == {
60 "xyzzy['thing1']['thing2']" => "xyzzyvalue",
61 "zot" => "multi word value"
67 context "when parsing a file whose first line is a section" do
68 let(:sample_content) {
77 it "should parse the correct number of sections" do
78 # there is always a "global" section, so our count should be 2.
79 subject.section_names.length.should == 2
82 it "should parse the correct section_names" do
83 # there should always be a "global" section named "" at the beginning of the list
84 subject.section_names.should == ["", "section1"]
87 it "should expose settings for sections" do
88 subject.get_value("section1", "foo").should == "foovalue"
93 context "when parsing a file with a 'global' section" do
94 let(:sample_content) {
104 it "should parse the correct number of sections" do
105 # there is always a "global" section, so our count should be 2.
106 subject.section_names.length.should == 2
109 it "should parse the correct section_names" do
110 # there should always be a "global" section named "" at the beginning of the list
111 subject.section_names.should == ["", "section1"]
114 it "should expose settings for sections" do
115 subject.get_value("", "foo").should == "bar"
116 subject.get_value("section1", "foo").should == "foovalue"
120 context "when updating a file with existing empty values" do
121 let(:sample_content) {
126 #xyzzy['thing1']['thing2']='xyzzyvalue'
131 it "should properly update uncommented values" do
132 subject.get_value("section1", "far").should == nil
133 subject.set_value("section1", "foo", "foovalue")
134 subject.get_value("section1", "foo").should == "foovalue"
137 it "should properly update commented values" do
138 subject.get_value("section1", "bar").should == nil
139 subject.set_value("section1", "bar", "barvalue")
140 subject.get_value("section1", "bar").should == "barvalue"
141 subject.get_value("section1", "xyzzy['thing1']['thing2']").should == nil
142 subject.set_value("section1", "xyzzy['thing1']['thing2']", "xyzzyvalue")
143 subject.get_value("section1", "xyzzy['thing1']['thing2']").should == "xyzzyvalue"
146 it "should properly add new empty values" do
147 subject.get_value("section1", "baz").should == nil
148 subject.set_value("section1", "baz", "bazvalue")
149 subject.get_value("section1", "baz").should == "bazvalue"
153 context 'the file has quotation marks in its section names' do
154 let(:sample_content) do
158 merge = refs/heads/master
161 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
162 [branch "production"]
164 merge = refs/heads/production
169 it 'should parse the sections' do
170 subject.section_names.should match_array ['',
173 'branch "production"'
178 context 'Samba INI file with dollars in section names' do
179 let(:sample_content) do
182 workgroup = FELLOWSHIP
184 idmap config * : backend = tdb
187 comment = All Printers
192 comment = Printer Drivers
193 path = /var/lib/samba/printers
203 it "should parse the correct section_names" do
204 subject.section_names.should match_array [
214 context 'section names with forward slashes in them' do
215 let(:sample_content) do
217 [monitor:///var/log/*.log]
218 disabled = test_value
223 it "should parse the correct section_names" do
224 subject.section_names.should match_array [
226 'monitor:///var/log/*.log'
231 context 'KDE Configuration with braces in setting names' do
232 let(:sample_content) do
235 _k_friendly_name=khotkeys
236 {5465e8c7-d608-4493-a48f-b99d99fdb508}=Print,none,PrintScreen
237 {d03619b6-9b3c-48cc-9d9c-a2aadb485550}=Search,none,Search
242 it "should expose settings for sections" do
243 subject.get_value("khotkeys", "{5465e8c7-d608-4493-a48f-b99d99fdb508}").should == "Print,none,PrintScreen"
244 subject.get_value("khotkeys", "{d03619b6-9b3c-48cc-9d9c-a2aadb485550}").should == "Search,none,Search"
248 context 'Configuration with colons in setting names' do
249 let(:sample_content) do
259 it "should expose settings for sections" do
260 subject.get_value("Drive names", "A:").should eq '5.25" Floppy'
261 subject.get_value("Drive names", "B:").should eq '3.5" Floppy'
262 subject.get_value("Drive names", "C:").should eq 'Winchester'
266 context 'Configuration with spaces in setting names' do
267 let(:sample_content) do
270 # log files split per-machine:
271 log file = /var/log/samba/log.%m
273 kerberos method = system keytab
274 passdb backend = tdbsam
280 it "should expose settings for sections" do
281 subject.get_value("global", "log file").should eq '/var/log/samba/log.%m'
282 subject.get_value("global", "kerberos method").should eq 'system keytab'
283 subject.get_value("global", "passdb backend").should eq 'tdbsam'
284 subject.get_value("global", "security").should eq 'ads'