blob: 0d8f199a4ae212483570bc038b7f347ce3de72a1 [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',
17 r'Section\with$weird%characters[' '\t',
18 r'Spacey Bar',
19 ],
Fred Drake95b96d32001-02-12 17:23:20 +000020 "unexpected list of section names")
Fred Drake8ef67672000-09-27 22:45:25 +000021
22 # The use of spaces in the section names serves as a regression test for
23 # SourceForge bug #115357.
Fred Drake8ef67672000-09-27 22:45:25 +000024 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
Fred Drake95b96d32001-02-12 17:23:20 +000025 verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
26 verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
27 verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
Fred Drake8ef67672000-09-27 22:45:25 +000028
Fred Drake95b96d32001-02-12 17:23:20 +000029 verify('__name__' not in cf.options("Foo Bar"),
30 '__name__ "option" should not be exposed by the API!')
Fred Drake8ef67672000-09-27 22:45:25 +000031
Fred Drake3d5f7e82000-12-04 16:30:40 +000032 # Make sure the right things happen for remove_option();
33 # added to include check for SourceForge bug #123324:
Fred Drake95b96d32001-02-12 17:23:20 +000034 verify(cf.remove_option('Foo Bar', 'foo'),
35 "remove_option() failed to report existance of option")
36 verify(not cf.has_option('Foo Bar', 'foo'),
37 "remove_option() failed to remove option")
38 verify(not cf.remove_option('Foo Bar', 'foo'),
39 "remove_option() failed to report non-existance of option"
40 " that was removed")
Fred Drake3d5f7e82000-12-04 16:30:40 +000041 try:
42 cf.remove_option('No Such Section', 'foo')
43 except ConfigParser.NoSectionError:
44 pass
45 else:
46 raise TestFailed(
47 "remove_option() failed to report non-existance of option"
48 " that never existed")
49
50
Fred Drake3c823aa2001-02-26 21:55:34 +000051def case_sensitivity():
52 print "Testing case sensitivity..."
53 cf = ConfigParser.ConfigParser()
54 cf.add_section("A")
55 cf.add_section("a")
56 L = cf.sections()
57 L.sort()
58 verify(L == ["A", "a"])
59 cf.set("a", "B", "value")
60 verify(cf.options("a") == ["b"])
61 verify(cf.get("a", "b", raw=1) == "value",
62 "could not locate option, expecting case-insensitive option names")
63 verify(cf.has_option("a", "b"))
64 cf.set("A", "A-B", "A-B value")
65 for opt in ("a-b", "A-b", "a-B", "A-B"):
66 verify(cf.has_option("A", opt),
67 "has_option() returned false for option which should exist")
68 verify(cf.options("A") == ["a-b"])
69 verify(cf.options("a") == ["b"])
70 cf.remove_option("a", "B")
71 verify(cf.options("a") == [])
72
Fred Drakebeb67132001-07-06 17:22:48 +000073 # SF bug #432369:
74 cf = ConfigParser.ConfigParser()
75 sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
76 cf.readfp(sio)
77 verify(cf.options("MySection") == ["option"])
78 verify(cf.get("MySection", "Option") == "first line\nsecond line")
79
Fred Drake3c823aa2001-02-26 21:55:34 +000080
Fred Drake8ef67672000-09-27 22:45:25 +000081def interpolation(src):
Fred Drake8ef67672000-09-27 22:45:25 +000082 print "Testing value interpolation..."
83 cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
84 sio = StringIO.StringIO(src)
85 cf.readfp(sio)
Fred Drake95b96d32001-02-12 17:23:20 +000086 verify(cf.get("Foo", "getname") == "Foo")
87 verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
88 verify(cf.get("Foo", "bar9")
89 == "something with lots of interpolation (9 steps)")
90 verify(cf.get("Foo", "bar10")
91 == "something with lots of interpolation (10 steps)")
Fred Drake8ef67672000-09-27 22:45:25 +000092 expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
93
Fred Drake95b96d32001-02-12 17:23:20 +000094
Fred Drake8ef67672000-09-27 22:45:25 +000095def parse_errors():
Fred Drake95b96d32001-02-12 17:23:20 +000096 print "Testing parse errors..."
Fred Drake8ef67672000-09-27 22:45:25 +000097 expect_parse_error(ConfigParser.ParsingError,
98 """[Foo]\n extra-spaces: splat\n""")
99 expect_parse_error(ConfigParser.ParsingError,
100 """[Foo]\n extra-spaces= splat\n""")
101 expect_parse_error(ConfigParser.ParsingError,
102 """[Foo]\noption-without-value\n""")
103 expect_parse_error(ConfigParser.ParsingError,
104 """[Foo]\n:value-without-option-name\n""")
105 expect_parse_error(ConfigParser.ParsingError,
106 """[Foo]\n=value-without-option-name\n""")
107 expect_parse_error(ConfigParser.MissingSectionHeaderError,
108 """No Section!\n""")
109
Fred Drake95b96d32001-02-12 17:23:20 +0000110
Fred Drake8ef67672000-09-27 22:45:25 +0000111def query_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000112 print "Testing query interface..."
113 cf = ConfigParser.ConfigParser()
Fred Drake95b96d32001-02-12 17:23:20 +0000114 verify(cf.sections() == [],
115 "new ConfigParser should have no defined sections")
116 verify(not cf.has_section("Foo"),
117 "new ConfigParser should have no acknowledged sections")
Fred Drake8ef67672000-09-27 22:45:25 +0000118 try:
119 cf.options("Foo")
120 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000121 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000122 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000123 raise TestFailed(
124 "Failed to catch expected NoSectionError from options()")
Fred Drake8ef67672000-09-27 22:45:25 +0000125 try:
126 cf.set("foo", "bar", "value")
127 except ConfigParser.NoSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000128 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000129 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000130 raise TestFailed("Failed to catch expected NoSectionError from set()")
Fred Drake8ef67672000-09-27 22:45:25 +0000131 expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
132 cf.add_section("foo")
133 expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
134
Fred Drake95b96d32001-02-12 17:23:20 +0000135
Fred Drake8ef67672000-09-27 22:45:25 +0000136def weird_errors():
Fred Drake8ef67672000-09-27 22:45:25 +0000137 print "Testing miscellaneous error conditions..."
138 cf = ConfigParser.ConfigParser()
139 cf.add_section("Foo")
140 try:
141 cf.add_section("Foo")
142 except ConfigParser.DuplicateSectionError, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000143 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000144 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000145 raise TestFailed("Failed to catch expected DuplicateSectionError")
146
Fred Drake8ef67672000-09-27 22:45:25 +0000147
148def expect_get_error(cf, exctype, section, option, raw=0):
149 try:
150 cf.get(section, option, raw=raw)
151 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000152 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000153 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000154 raise TestFailed("Failed to catch expected " + exctype.__name__)
155
Fred Drake8ef67672000-09-27 22:45:25 +0000156
157def expect_parse_error(exctype, src):
158 cf = ConfigParser.ConfigParser()
159 sio = StringIO.StringIO(src)
160 try:
161 cf.readfp(sio)
162 except exctype, e:
Fred Drake95b96d32001-02-12 17:23:20 +0000163 pass
Fred Drake8ef67672000-09-27 22:45:25 +0000164 else:
Fred Drake95b96d32001-02-12 17:23:20 +0000165 raise TestFailed("Failed to catch expected " + exctype.__name__)
166
Fred Drake8ef67672000-09-27 22:45:25 +0000167
168basic(r"""
169[Foo Bar]
170foo=bar
171[Spacey Bar]
Fred Drake004d5e62000-10-23 17:22:08 +0000172foo = bar
Fred Drake8ef67672000-09-27 22:45:25 +0000173[Commented Bar]
174foo: bar ; comment
Fred Drakecc1f9512001-02-14 15:30:31 +0000175[Section\with$weird%characters[""" '\t' r"""]
Fred Drake95b96d32001-02-12 17:23:20 +0000176[Internationalized Stuff]
177foo[bg]: Bulgarian
178foo=Default
179foo[en]=English
180foo[de]=Deutsch
Fred Drake8ef67672000-09-27 22:45:25 +0000181""")
Fred Drake3c823aa2001-02-26 21:55:34 +0000182case_sensitivity()
Fred Drake8ef67672000-09-27 22:45:25 +0000183interpolation(r"""
184[Foo]
185bar=something %(with1)s interpolation (1 step)
186bar9=something %(with9)s lots of interpolation (9 steps)
187bar10=something %(with10)s lots of interpolation (10 steps)
188bar11=something %(with11)s lots of interpolation (11 steps)
189with11=%(with10)s
190with10=%(with9)s
191with9=%(with8)s
192with8=%(with7)s
193with7=%(with6)s
194with6=%(with5)s
195with5=%(with4)s
196with4=%(with3)s
197with3=%(with2)s
198with2=%(with1)s
199with1=with
200
201[Mutual Recursion]
202foo=%(bar)s
203bar=%(foo)s
204""")
205parse_errors()
206query_errors()
207weird_errors()