blob: 90f7255540dd07652992f5732e95074940676e26 [file] [log] [blame]
Fred Drake8ef67672000-09-27 22:45:25 +00001import ConfigParser
2import StringIO
3
Barry Warsaw408b6d32002-07-30 23:27:12 +00004from test.test_support import TestFailed, verify
Fred Drake3d5f7e82000-12-04 16:30:40 +00005
6
Fred Drake8ef67672000-09-27 22:45:25 +00007def basic(src):
Fred Drake8ef67672000-09-27 22:45:25 +00008 print "Testing basic accessors..."
9 cf = ConfigParser.ConfigParser()
10 sio = StringIO.StringIO(src)
11 cf.readfp(sio)
12 L = cf.sections()
13 L.sort()
Fred Drakecc1f9512001-02-14 15:30:31 +000014 verify(L == [r'Commented Bar',
15 r'Foo Bar',
16 r'Internationalized Stuff',
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +000017 r'Long Line',
Fred Drakecc1f9512001-02-14 15:30:31 +000018 r'Section\with$weird%characters[' '\t',
Fred Drake176916a2002-09-27 16:21:18 +000019 r'Spaces',
Fred Drakecc1f9512001-02-14 15:30:31 +000020 r'Spacey Bar',
21 ],
Fred Drake95b96d32001-02-12 17:23:20 +000022 "unexpected list of section names")
Fred Drake8ef67672000-09-27 22:45:25 +000023
24 # The use of spaces in the section names serves as a regression test for
25 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000026 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake95b96d32001-02-12 17:23:20 +000027 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
28 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
29 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
Fred Drake176916a2002-09-27 16:21:18 +000030 verify(cf.get('Spaces', 'key with spaces', raw=1) == 'value')
31 verify(cf.get('Spaces', 'another with spaces', raw=1) == 'splat!')
Fred Drake8ef67672000-09-27 22:45:25 +000032
Fred Drake95b96d32001-02-12 17:23:20 +000033 verify('__name__' not in cf.options("Foo Bar"),
34 '__name__ "option" should not be exposed by the API!')
Fred Drake8ef67672000-09-27 22:45:25 +000035
Fred Drake3d5f7e82000-12-04 16:30:40 +000036 # Make sure the right things happen for remove_option();
37 # added to include check for SourceForge bug #123324:
Fred Drake95b96d32001-02-12 17:23:20 +000038 verify(cf.remove_option('Foo Bar', 'foo'),
39 "remove_option() failed to report existance of option")
40 verify(not cf.has_option('Foo Bar', 'foo'),
41 "remove_option() failed to remove option")
42 verify(not cf.remove_option('Foo Bar', 'foo'),
43 "remove_option() failed to report non-existance of option"
44 " that was removed")
Fred Drake3d5f7e82000-12-04 16:30:40 +000045 try:
46 cf.remove_option('No Such Section', 'foo')
47 except ConfigParser.NoSectionError:
48 pass
49 else:
50 raise TestFailed(
51 "remove_option() failed to report non-existance of option"
52 " that never existed")
53
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +000054 verify(cf.get('Long Line', 'foo', raw=1) ==
55 'this line is much, much longer than my editor\nlikes it.')
Fred Drake3d5f7e82000-12-04 16:30:40 +000056
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +000057
58def write(src):
59 print "Testing writing of files..."
60 cf = ConfigParser.ConfigParser()
61 sio = StringIO.StringIO(src)
62 cf.readfp(sio)
63 output = StringIO.StringIO()
64 cf.write(output)
65 verify(output, """[DEFAULT]
66foo = another very
67 long line
68
69[Long Line]
70foo = this line is much, much longer than my editor
71 likes it.
72""")
Tim Peters863ac442002-04-16 01:38:40 +000073
Fred Drake3c823aa2001-02-26 21:55:34 +000074def case_sensitivity():
75 print "Testing case sensitivity..."
76 cf = ConfigParser.ConfigParser()
77 cf.add_section("A")
78 cf.add_section("a")
79 L = cf.sections()
80 L.sort()
81 verify(L == ["A", "a"])
82 cf.set("a", "B", "value")
83 verify(cf.options("a") == ["b"])
84 verify(cf.get("a", "b", raw=1) == "value",
85 "could not locate option, expecting case-insensitive option names")
86 verify(cf.has_option("a", "b"))
87 cf.set("A", "A-B", "A-B value")
88 for opt in ("a-b", "A-b", "a-B", "A-B"):
89 verify(cf.has_option("A", opt),
90 "has_option() returned false for option which should exist")
91 verify(cf.options("A") == ["a-b"])
92 verify(cf.options("a") == ["b"])
93 cf.remove_option("a", "B")
94 verify(cf.options("a") == [])
95
Fred Drakebeb67132001-07-06 17:22:48 +000096 # SF bug #432369:
97 cf = ConfigParser.ConfigParser()
98 sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
99 cf.readfp(sio)
100 verify(cf.options("MySection") == ["option"])
101 verify(cf.get("MySection", "Option") == "first line\nsecond line")
102
Fred Drake309db062002-09-27 15:35:23 +0000103 # SF bug #561822:
104 cf = ConfigParser.ConfigParser(defaults={"key":"value"})
105 cf.readfp(StringIO.StringIO("[section]\nnekey=nevalue\n"))
106 verify(cf.has_option("section", "Key"))
107
Fred Drake3c823aa2001-02-26 21:55:34 +0000108
Fred Drake168bead2001-10-08 17:13:12 +0000109def boolean(src):
110 print "Testing interpretation of boolean Values..."
111 cf = ConfigParser.ConfigParser()
112 sio = StringIO.StringIO(src)
113 cf.readfp(sio)
114 for x in range(1, 5):
115 verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1)
116 for x in range(1, 5):
117 verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0)
118 for x in range(1, 5):
119 try:
120 cf.getboolean('BOOLTEST', 'e%d' % (x))
121 except ValueError:
122 pass
123 else:
124 raise TestFailed(
125 "getboolean() failed to report a non boolean value")
126
127
Fred Drake8ef67672000-09-27 22:45:25 +0000128def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +0000129 print "Testing value interpolation..."
130 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
131 sio = StringIO.StringIO(src)
132 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +0000133 verify(cf.get("Foo", "getname") == "Foo")
134 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
135 verify(cf.get("Foo", "bar9")
136 == "something with lots of interpolation (9 steps)")
137 verify(cf.get("Foo", "bar10")
138 == "something with lots of interpolation (10 steps)")
Fred Drake8ef67672000-09-27 22:45:25 +0000139 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
140
Fred Drake95b96d32001-02-12 17:23:20 +0000141
Fred Drake8ef67672000-09-27 22:45:25 +0000142def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +0000143 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +0000144 expect_parse_error(ConfigParser.ParsingError,
145 """[Foo]\n extra-spaces: splat\n""")
146 expect_parse_error(ConfigParser.ParsingError,
147 """[Foo]\n extra-spaces= splat\n""")
148 expect_parse_error(ConfigParser.ParsingError,
149 """[Foo]\noption-without-value\n""")
150 expect_parse_error(ConfigParser.ParsingError,
151 """[Foo]\n:value-without-option-name\n""")
152 expect_parse_error(ConfigParser.ParsingError,
153 """[Foo]\n=value-without-option-name\n""")
154 expect_parse_error(ConfigParser.MissingSectionHeaderError,
155 """No Section!\n""")
156
Fred Drake95b96d32001-02-12 17:23:20 +0000157
Fred Drake8ef67672000-09-27 22:45:25 +0000158def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000159 print "Testing query interface..."
160 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +0000161 verify(cf.sections() == [],
162 "new ConfigParser should have no defined sections")
163 verify(not cf.has_section("Foo"),
164 "new ConfigParser should have no acknowledged sections")
Fred Drake8ef67672000-09-27 22:45:25 +0000165 try:
166 cf.options("Foo")
167 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000168 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000169 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000170 raise TestFailed(
171 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +0000172 try:
173 cf.set("foo", "bar", "value")
174 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000175 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000176 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000177 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +0000178 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
179 cf.add_section("foo")
180 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
181
Fred Drake95b96d32001-02-12 17:23:20 +0000182
Fred Drake8ef67672000-09-27 22:45:25 +0000183def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000184 print "Testing miscellaneous error conditions..."
185 cf = ConfigParser.ConfigParser()
186 cf.add_section("Foo")
187 try:
188 cf.add_section("Foo")
189 except ConfigParser.DuplicateSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000190 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000191 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000192 raise TestFailed("Failed to catch expected DuplicateSectionError")
193
Fred Drake8ef67672000-09-27 22:45:25 +0000194
195def expect_get_error(cf, exctype, section, option, raw=0):
196 try:
197 cf.get(section, option, raw=raw)
198 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000199 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000200 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000201 raise TestFailed("Failed to catch expected " + exctype.__name__)
202
Fred Drake8ef67672000-09-27 22:45:25 +0000203
204def expect_parse_error(exctype, src):
205 cf = ConfigParser.ConfigParser()
206 sio = StringIO.StringIO(src)
207 try:
208 cf.readfp(sio)
209 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000210 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000211 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000212 raise TestFailed("Failed to catch expected " + exctype.__name__)
213
Fred Drake8ef67672000-09-27 22:45:25 +0000214
215basic(r"""
216[Foo Bar]
217foo=bar
218[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000219foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000220[Commented Bar]
221foo: bar ; comment
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000222[Long Line]
223foo: this line is much, much longer than my editor
224 likes it.
Fred Drakecc1f9512001-02-14 15:30:31 +0000225[Section\with$weird%characters[""" '\t' r"""]
Fred Drake95b96d32001-02-12 17:23:20 +0000226[Internationalized Stuff]
227foo[bg]: Bulgarian
228foo=Default
229foo[en]=English
230foo[de]=Deutsch
Fred Drake176916a2002-09-27 16:21:18 +0000231[Spaces]
232key with spaces : value
233another with spaces = splat!
Fred Drake8ef67672000-09-27 22:45:25 +0000234""")
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000235write("""[Long Line]
236foo: this line is much, much longer than my editor
237 likes it.
238[DEFAULT]
239foo: another very
240 long line""")
Fred Drake3c823aa2001-02-26 21:55:34 +0000241case_sensitivity()
Fred Drake168bead2001-10-08 17:13:12 +0000242boolean(r"""
243[BOOLTEST]
244T1=1
245T2=TRUE
246T3=True
247T4=oN
248T5=yes
249F1=0
250F2=FALSE
251F3=False
252F4=oFF
253F5=nO
254E1=2
255E2=foo
256E3=-1
257E4=0.1
258E5=FALSE AND MORE
259""")
Fred Drake8ef67672000-09-27 22:45:25 +0000260interpolation(r"""
261[Foo]
262bar=something %(with1)s interpolation (1 step)
263bar9=something %(with9)s lots of interpolation (9 steps)
264bar10=something %(with10)s lots of interpolation (10 steps)
265bar11=something %(with11)s lots of interpolation (11 steps)
266with11=%(with10)s
267with10=%(with9)s
268with9=%(with8)s
269with8=%(with7)s
270with7=%(with6)s
271with6=%(with5)s
272with5=%(with4)s
273with4=%(with3)s
274with3=%(with2)s
275with2=%(with1)s
276with1=with
277
278[Mutual Recursion]
279foo=%(bar)s
280bar=%(foo)s
281""")
282parse_errors()
283query_errors()
284weird_errors()