]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/spec/unit/puppet/util/ini_file_spec.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / spec / unit / puppet / util / ini_file_spec.rb
1 require 'spec_helper'
2 require 'stringio'
3 require 'puppet/util/ini_file'
4
5 describe Puppet::Util::IniFile do
6   let(:subject) { Puppet::Util::IniFile.new("/my/ini/file/path") }
7
8   before :each do
9     File.should_receive(:file?).with("/my/ini/file/path") { true }
10     described_class.should_receive(:readlines).once.with("/my/ini/file/path") do
11       sample_content
12     end
13   end
14
15   context "when parsing a file" do
16     let(:sample_content) {
17       template = <<-EOS
18 # This is a comment
19 [section1]
20 ; This is also a comment
21 foo=foovalue
22
23 bar = barvalue
24 baz =
25 [section2]
26
27 foo= foovalue2
28 baz=bazvalue
29  ; commented = out setting
30     #another comment
31  ; yet another comment
32  zot = multi word value
33  xyzzy['thing1']['thing2']=xyzzyvalue
34  l=git log
35       EOS
36       template.split("\n")
37     }
38
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
42     end
43
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"]
47     end
48
49     it "should expose settings for sections" do
50       subject.get_settings("section1").should == {
51         "bar" => "barvalue",
52         "baz" => "",
53         "foo" => "foovalue"
54       }
55
56       subject.get_settings("section2").should == {
57         "baz" => "bazvalue",
58         "foo" => "foovalue2",
59         "l" => "git log",
60         "xyzzy['thing1']['thing2']" => "xyzzyvalue",
61         "zot" => "multi word value"
62       }
63     end
64
65   end
66
67   context "when parsing a file whose first line is a section" do
68     let(:sample_content) {
69       template = <<-EOS
70 [section1]
71 ; This is a comment
72 foo=foovalue
73       EOS
74       template.split("\n")
75     }
76
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
80     end
81
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"]
85     end
86
87     it "should expose settings for sections" do
88       subject.get_value("section1", "foo").should == "foovalue"
89     end
90
91   end
92
93   context "when parsing a file with a 'global' section" do
94     let(:sample_content) {
95       template = <<-EOS
96 foo = bar
97 [section1]
98 ; This is a comment
99 foo=foovalue
100       EOS
101       template.split("\n")
102     }
103
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
107     end
108
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"]
112     end
113
114     it "should expose settings for sections" do
115       subject.get_value("", "foo").should == "bar"
116       subject.get_value("section1", "foo").should == "foovalue"
117     end
118   end
119
120   context "when updating a file with existing empty values" do
121     let(:sample_content) {
122       template = <<-EOS
123 [section1]
124 foo=
125 #bar=
126 #xyzzy['thing1']['thing2']='xyzzyvalue'
127       EOS
128       template.split("\n")
129     }
130
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"
135     end
136
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"
144     end
145
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"
150     end
151   end
152
153   context 'the file has quotation marks in its section names' do
154     let(:sample_content) do
155       template = <<-EOS
156 [branch "master"]
157         remote = origin
158         merge = refs/heads/master
159
160 [alias]
161 to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
162 [branch "production"]
163         remote = origin
164         merge = refs/heads/production
165       EOS
166       template.split("\n")
167     end
168
169     it 'should parse the sections' do
170       subject.section_names.should match_array ['',
171                                                 'branch "master"',
172                                                 'alias',
173                                                 'branch "production"'
174       ]
175     end
176   end
177
178   context 'Samba INI file with dollars in section names' do
179     let(:sample_content) do
180       template = <<-EOS
181       [global]
182         workgroup = FELLOWSHIP
183         ; ...
184         idmap config * : backend = tdb
185
186       [printers]
187         comment = All Printers
188         ; ...
189         browseable = No
190
191       [print$]
192         comment = Printer Drivers
193         path = /var/lib/samba/printers
194
195       [Shares]
196         path = /home/shares
197         read only = No
198         guest ok = Yes
199       EOS
200       template.split("\n")
201     end
202
203     it "should parse the correct section_names" do
204       subject.section_names.should match_array [
205         '',
206         'global',
207         'printers',
208         'print$',
209         'Shares'
210       ]
211     end
212   end
213
214   context 'section names with forward slashes in them' do
215     let(:sample_content) do
216       template = <<-EOS
217 [monitor:///var/log/*.log]
218 disabled = test_value
219       EOS
220       template.split("\n")
221     end
222
223     it "should parse the correct section_names" do
224       subject.section_names.should match_array [
225         '',
226         'monitor:///var/log/*.log'
227       ]
228     end
229   end
230
231   context 'KDE Configuration with braces in setting names' do
232     let(:sample_content) do
233       template = <<-EOS
234       [khotkeys]
235 _k_friendly_name=khotkeys
236 {5465e8c7-d608-4493-a48f-b99d99fdb508}=Print,none,PrintScreen
237 {d03619b6-9b3c-48cc-9d9c-a2aadb485550}=Search,none,Search
238 EOS
239       template.split("\n")
240     end
241
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"
245     end
246   end
247
248   context 'Configuration with colons in setting names' do
249     let(:sample_content) do
250       template = <<-EOS
251       [Drive names]
252 A:=5.25" Floppy
253 B:=3.5" Floppy
254 C:=Winchester
255 EOS
256       template.split("\n")
257     end
258
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'
263     end
264   end
265
266   context 'Configuration with spaces in setting names' do
267     let(:sample_content) do
268       template = <<-EOS
269       [global]
270         # log files split per-machine:
271         log file = /var/log/samba/log.%m
272
273         kerberos method = system keytab
274         passdb backend = tdbsam
275         security = ads
276 EOS
277       template.split("\n")
278     end
279
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'
285     end
286   end
287 end