blob: 6a6fecbfbb3a697a10b0da58e291c102b149809f [file] [log] [blame]
Fred Drake8ef67672000-09-27 22:45:25 +00001import ConfigParser
2import StringIO
3
Fred Drake95b96d32001-02-12 17:23:20 +00004from 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 Drake3c823aa2001-02-26 21:55:34 +0000100
Fred Drake168bead2001-10-08 17:13:12 +0000101def boolean(src):
102 print "Testing interpretation of boolean Values..."
103 cf = ConfigParser.ConfigParser()
104 sio = StringIO.StringIO(src)
105 cf.readfp(sio)
106 for x in range(1, 5):
107 verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1)
108 for x in range(1, 5):
109 verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0)
110 for x in range(1, 5):
111 try:
112 cf.getboolean('BOOLTEST', 'e%d' % (x))
113 except ValueError:
114 pass
115 else:
116 raise TestFailed(
117 "getboolean() failed to report a non boolean value")
118
119
Fred Drake8ef67672000-09-27 22:45:25 +0000120def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +0000121 print "Testing value interpolation..."
122 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
123 sio = StringIO.StringIO(src)
124 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +0000125 verify(cf.get("Foo", "getname") == "Foo")
126 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
127 verify(cf.get("Foo", "bar9")
128 == "something with lots of interpolation (9 steps)")
129 verify(cf.get("Foo", "bar10")
130 == "something with lots of interpolation (10 steps)")
Fred Drake8ef67672000-09-27 22:45:25 +0000131 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
132
Fred Drake95b96d32001-02-12 17:23:20 +0000133
Fred Drake8ef67672000-09-27 22:45:25 +0000134def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +0000135 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +0000136 expect_parse_error(ConfigParser.ParsingError,
137 """[Foo]\n extra-spaces: splat\n""")
138 expect_parse_error(ConfigParser.ParsingError,
139 """[Foo]\n extra-spaces= splat\n""")
140 expect_parse_error(ConfigParser.ParsingError,
141 """[Foo]\noption-without-value\n""")
142 expect_parse_error(ConfigParser.ParsingError,
143 """[Foo]\n:value-without-option-name\n""")
144 expect_parse_error(ConfigParser.ParsingError,
145 """[Foo]\n=value-without-option-name\n""")
146 expect_parse_error(ConfigParser.MissingSectionHeaderError,
147 """No Section!\n""")
148
Fred Drake95b96d32001-02-12 17:23:20 +0000149
Fred Drake8ef67672000-09-27 22:45:25 +0000150def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000151 print "Testing query interface..."
152 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +0000153 verify(cf.sections() == [],
154 "new ConfigParser should have no defined sections")
155 verify(not cf.has_section("Foo"),
156 "new ConfigParser should have no acknowledged sections")
Fred Drake8ef67672000-09-27 22:45:25 +0000157 try:
158 cf.options("Foo")
159 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000160 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000161 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000162 raise TestFailed(
163 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +0000164 try:
165 cf.set("foo", "bar", "value")
166 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000167 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000168 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000169 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +0000170 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
171 cf.add_section("foo")
172 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
173
Fred Drake95b96d32001-02-12 17:23:20 +0000174
Fred Drake8ef67672000-09-27 22:45:25 +0000175def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000176 print "Testing miscellaneous error conditions..."
177 cf = ConfigParser.ConfigParser()
178 cf.add_section("Foo")
179 try:
180 cf.add_section("Foo")
181 except ConfigParser.DuplicateSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000182 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000183 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000184 raise TestFailed("Failed to catch expected DuplicateSectionError")
185
Fred Drake8ef67672000-09-27 22:45:25 +0000186
187def expect_get_error(cf, exctype, section, option, raw=0):
188 try:
189 cf.get(section, option, raw=raw)
190 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000191 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000192 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000193 raise TestFailed("Failed to catch expected " + exctype.__name__)
194
Fred Drake8ef67672000-09-27 22:45:25 +0000195
196def expect_parse_error(exctype, src):
197 cf = ConfigParser.ConfigParser()
198 sio = StringIO.StringIO(src)
199 try:
200 cf.readfp(sio)
201 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000202 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000203 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000204 raise TestFailed("Failed to catch expected " + exctype.__name__)
205
Fred Drake8ef67672000-09-27 22:45:25 +0000206
207basic(r"""
208[Foo Bar]
209foo=bar
210[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000211foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000212[Commented Bar]
213foo: bar ; comment
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000214[Long Line]
215foo: this line is much, much longer than my editor
216 likes it.
Fred Drakecc1f9512001-02-14 15:30:31 +0000217[Section\with$weird%characters[""" '\t' r"""]
Fred Drake95b96d32001-02-12 17:23:20 +0000218[Internationalized Stuff]
219foo[bg]: Bulgarian
220foo=Default
221foo[en]=English
222foo[de]=Deutsch
Fred Drake8ef67672000-09-27 22:45:25 +0000223""")
Andrew M. Kuchling1bf71172002-03-08 18:10:12 +0000224write("""[Long Line]
225foo: this line is much, much longer than my editor
226 likes it.
227[DEFAULT]
228foo: another very
229 long line""")
Fred Drake3c823aa2001-02-26 21:55:34 +0000230case_sensitivity()
Fred Drake168bead2001-10-08 17:13:12 +0000231boolean(r"""
232[BOOLTEST]
233T1=1
234T2=TRUE
235T3=True
236T4=oN
237T5=yes
238F1=0
239F2=FALSE
240F3=False
241F4=oFF
242F5=nO
243E1=2
244E2=foo
245E3=-1
246E4=0.1
247E5=FALSE AND MORE
248""")
Fred Drake8ef67672000-09-27 22:45:25 +0000249interpolation(r"""
250[Foo]
251bar=something %(with1)s interpolation (1 step)
252bar9=something %(with9)s lots of interpolation (9 steps)
253bar10=something %(with10)s lots of interpolation (10 steps)
254bar11=something %(with11)s lots of interpolation (11 steps)
255with11=%(with10)s
256with10=%(with9)s
257with9=%(with8)s
258with8=%(with7)s
259with7=%(with6)s
260with6=%(with5)s
261with5=%(with4)s
262with4=%(with3)s
263with3=%(with2)s
264with2=%(with1)s
265with1=with
266
267[Mutual Recursion]
268foo=%(bar)s
269bar=%(foo)s
270""")
271parse_errors()
272query_errors()
273weird_errors()