blob: 17f5ea68d4a12f8a64e1047002d10891b00cb502 [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',
19 r'Spacey Bar',
20 ],
Fred Drake95b96d32001-02-12 17:23:20 +000021 "unexpected list of section names")
Fred Drake8ef67672000-09-27 22:45:25 +000022
23 # The use of spaces in the section names serves as a regression test for
24 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000025 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake95b96d32001-02-12 17:23:20 +000026 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
27 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
28 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
Fred Drake8ef67672000-09-27 22:45:25 +000029
Fred Drake95b96d32001-02-12 17:23:20 +000030 verify('__name__' not in cf.options("Foo Bar"),
31 '__name__ "option" should not be exposed by the API!')
Fred Drake8ef67672000-09-27 22:45:25 +000032
Fred Drake3d5f7e82000-12-04 16:30:40 +000033 # Make sure the right things happen for remove_option();
34 # added to include check for SourceForge bug #123324:
Fred Drake95b96d32001-02-12 17:23:20 +000035 verify(cf.remove_option('Foo Bar', 'foo'),
36 "remove_option() failed to report existance of option")
37 verify(not cf.has_option('Foo Bar', 'foo'),
38 "remove_option() failed to remove option")
39 verify(not cf.remove_option('Foo Bar', 'foo'),
40 "remove_option() failed to report non-existance of option"
41 " that was removed")
Fred Drake3d5f7e82000-12-04 16:30:40 +000042 try:
43 cf.remove_option('No Such Section', 'foo')
44 except ConfigParser.NoSectionError:
45 pass
46 else:
47 raise TestFailed(
48 "remove_option() failed to report non-existance of option"
49 " that never existed")
50
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +000051 verify(cf.get('Long Line', 'foo', raw=1) ==
52 'this line is much, much longer than my editor\nlikes it.')
Fred Drake3d5f7e82000-12-04 16:30:40 +000053
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +000054
55def write(src):
56 print "Testing writing of files..."
57 cf = ConfigParser.ConfigParser()
58 sio = StringIO.StringIO(src)
59 cf.readfp(sio)
60 output = StringIO.StringIO()
61 cf.write(output)
62 verify(output, """[DEFAULT]
63foo = another very
64 long line
65
66[Long Line]
67foo = this line is much, much longer than my editor
68 likes it.
69""")
Tim Peters863ac442002-04-16 01:38:40 +000070
Fred Drake3c823aa2001-02-26 21:55:34 +000071def case_sensitivity():
72 print "Testing case sensitivity..."
73 cf = ConfigParser.ConfigParser()
74 cf.add_section("A")
75 cf.add_section("a")
76 L = cf.sections()
77 L.sort()
78 verify(L == ["A", "a"])
79 cf.set("a", "B", "value")
80 verify(cf.options("a") == ["b"])
81 verify(cf.get("a", "b", raw=1) == "value",
82 "could not locate option, expecting case-insensitive option names")
83 verify(cf.has_option("a", "b"))
84 cf.set("A", "A-B", "A-B value")
85 for opt in ("a-b", "A-b", "a-B", "A-B"):
86 verify(cf.has_option("A", opt),
87 "has_option() returned false for option which should exist")
88 verify(cf.options("A") == ["a-b"])
89 verify(cf.options("a") == ["b"])
90 cf.remove_option("a", "B")
91 verify(cf.options("a") == [])
92
Fred Drakebeb67132001-07-06 17:22:48 +000093 # SF bug #432369:
94 cf = ConfigParser.ConfigParser()
95 sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
96 cf.readfp(sio)
97 verify(cf.options("MySection") == ["option"])
98 verify(cf.get("MySection", "Option") == "first line\nsecond line")
99
Fred Drake309db062002-09-27 15:35:23 +0000100 # SF bug #561822:
101 cf = ConfigParser.ConfigParser(defaults={"key":"value"})
102 cf.readfp(StringIO.StringIO("[section]\nnekey=nevalue\n"))
103 verify(cf.has_option("section", "Key"))
104
Fred Drake3c823aa2001-02-26 21:55:34 +0000105
Fred Drake168bead2001-10-08 17:13:12 +0000106def boolean(src):
107 print "Testing interpretation of boolean Values..."
108 cf = ConfigParser.ConfigParser()
109 sio = StringIO.StringIO(src)
110 cf.readfp(sio)
111 for x in range(1, 5):
112 verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1)
113 for x in range(1, 5):
114 verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0)
115 for x in range(1, 5):
116 try:
117 cf.getboolean('BOOLTEST', 'e%d' % (x))
118 except ValueError:
119 pass
120 else:
121 raise TestFailed(
122 "getboolean() failed to report a non boolean value")
123
124
Fred Drake8ef67672000-09-27 22:45:25 +0000125def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +0000126 print "Testing value interpolation..."
127 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
128 sio = StringIO.StringIO(src)
129 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +0000130 verify(cf.get("Foo", "getname") == "Foo")
131 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
132 verify(cf.get("Foo", "bar9")
133 == "something with lots of interpolation (9 steps)")
134 verify(cf.get("Foo", "bar10")
135 == "something with lots of interpolation (10 steps)")
Fred Drake8ef67672000-09-27 22:45:25 +0000136 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
137
Fred Drake95b96d32001-02-12 17:23:20 +0000138
Fred Drake8ef67672000-09-27 22:45:25 +0000139def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +0000140 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +0000141 expect_parse_error(ConfigParser.ParsingError,
142 """[Foo]\n extra-spaces: splat\n""")
143 expect_parse_error(ConfigParser.ParsingError,
144 """[Foo]\n extra-spaces= splat\n""")
145 expect_parse_error(ConfigParser.ParsingError,
146 """[Foo]\noption-without-value\n""")
147 expect_parse_error(ConfigParser.ParsingError,
148 """[Foo]\n:value-without-option-name\n""")
149 expect_parse_error(ConfigParser.ParsingError,
150 """[Foo]\n=value-without-option-name\n""")
151 expect_parse_error(ConfigParser.MissingSectionHeaderError,
152 """No Section!\n""")
153
Fred Drake95b96d32001-02-12 17:23:20 +0000154
Fred Drake8ef67672000-09-27 22:45:25 +0000155def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000156 print "Testing query interface..."
157 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +0000158 verify(cf.sections() == [],
159 "new ConfigParser should have no defined sections")
160 verify(not cf.has_section("Foo"),
161 "new ConfigParser should have no acknowledged sections")
Fred Drake8ef67672000-09-27 22:45:25 +0000162 try:
163 cf.options("Foo")
164 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000165 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000166 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000167 raise TestFailed(
168 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +0000169 try:
170 cf.set("foo", "bar", "value")
171 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000172 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000173 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000174 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +0000175 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
176 cf.add_section("foo")
177 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
178
Fred Drake95b96d32001-02-12 17:23:20 +0000179
Fred Drake8ef67672000-09-27 22:45:25 +0000180def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000181 print "Testing miscellaneous error conditions..."
182 cf = ConfigParser.ConfigParser()
183 cf.add_section("Foo")
184 try:
185 cf.add_section("Foo")
186 except ConfigParser.DuplicateSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000187 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000188 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000189 raise TestFailed("Failed to catch expected DuplicateSectionError")
190
Fred Drake8ef67672000-09-27 22:45:25 +0000191
192def expect_get_error(cf, exctype, section, option, raw=0):
193 try:
194 cf.get(section, option, raw=raw)
195 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000196 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000197 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000198 raise TestFailed("Failed to catch expected " + exctype.__name__)
199
Fred Drake8ef67672000-09-27 22:45:25 +0000200
201def expect_parse_error(exctype, src):
202 cf = ConfigParser.ConfigParser()
203 sio = StringIO.StringIO(src)
204 try:
205 cf.readfp(sio)
206 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000207 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000208 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000209 raise TestFailed("Failed to catch expected " + exctype.__name__)
210
Fred Drake8ef67672000-09-27 22:45:25 +0000211
212basic(r"""
213[Foo Bar]
214foo=bar
215[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000216foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000217[Commented Bar]
218foo: bar ; comment
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000219[Long Line]
220foo: this line is much, much longer than my editor
221 likes it.
Fred Drakecc1f9512001-02-14 15:30:31 +0000222[Section\with$weird%characters[""" '\t' r"""]
Fred Drake95b96d32001-02-12 17:23:20 +0000223[Internationalized Stuff]
224foo[bg]: Bulgarian
225foo=Default
226foo[en]=English
227foo[de]=Deutsch
Fred Drake8ef67672000-09-27 22:45:25 +0000228""")
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000229write("""[Long Line]
230foo: this line is much, much longer than my editor
231 likes it.
232[DEFAULT]
233foo: another very
234 long line""")
Fred Drake3c823aa2001-02-26 21:55:34 +0000235case_sensitivity()
Fred Drake168bead2001-10-08 17:13:12 +0000236boolean(r"""
237[BOOLTEST]
238T1=1
239T2=TRUE
240T3=True
241T4=oN
242T5=yes
243F1=0
244F2=FALSE
245F3=False
246F4=oFF
247F5=nO
248E1=2
249E2=foo
250E3=-1
251E4=0.1
252E5=FALSE AND MORE
253""")
Fred Drake8ef67672000-09-27 22:45:25 +0000254interpolation(r"""
255[Foo]
256bar=something %(with1)s interpolation (1 step)
257bar9=something %(with9)s lots of interpolation (9 steps)
258bar10=something %(with10)s lots of interpolation (10 steps)
259bar11=something %(with11)s lots of interpolation (11 steps)
260with11=%(with10)s
261with10=%(with9)s
262with9=%(with8)s
263with8=%(with7)s
264with7=%(with6)s
265with6=%(with5)s
266with5=%(with4)s
267with4=%(with3)s
268with3=%(with2)s
269with2=%(with1)s
270with1=with
271
272[Mutual Recursion]
273foo=%(bar)s
274bar=%(foo)s
275""")
276parse_errors()
277query_errors()
278weird_errors()