Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 1 | import collections |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 2 | import configparser |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 3 | import io |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 4 | import os |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 5 | import unittest |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 6 | import textwrap |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 9 | |
Raymond Hettinger | f80680d | 2008-02-06 00:07:11 +0000 | [diff] [blame] | 10 | class SortedDict(collections.UserDict): |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | def items(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 13 | return sorted(self.data.items()) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | |
| 15 | def keys(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 16 | return sorted(self.data.keys()) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 17 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 18 | def values(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 19 | return [i[1] for i in self.items()] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | |
| 21 | def iteritems(self): return iter(self.items()) |
| 22 | def iterkeys(self): return iter(self.keys()) |
| 23 | __iter__ = iterkeys |
| 24 | def itervalues(self): return iter(self.values()) |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 25 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 27 | class CfgParserTestCaseClass(unittest.TestCase): |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 28 | allow_no_value = False |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 29 | delimiters = ('=', ':') |
| 30 | comment_prefixes = (';', '#') |
| 31 | empty_lines_in_values = True |
| 32 | dict_type = configparser._default_dict |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 33 | strict = False |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 34 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 35 | def newconfig(self, defaults=None): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 36 | arguments = dict( |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 37 | defaults=defaults, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 38 | allow_no_value=self.allow_no_value, |
| 39 | delimiters=self.delimiters, |
| 40 | comment_prefixes=self.comment_prefixes, |
| 41 | empty_lines_in_values=self.empty_lines_in_values, |
| 42 | dict_type=self.dict_type, |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 43 | strict=self.strict, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 44 | ) |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 45 | return self.config_class(**arguments) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 46 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 47 | def fromstring(self, string, defaults=None): |
| 48 | cf = self.newconfig(defaults) |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 49 | cf.read_string(string) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 50 | return cf |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 52 | class BasicTestCase(CfgParserTestCaseClass): |
| 53 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 54 | def basic_test(self, cf): |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 55 | L = cf.sections() |
| 56 | L.sort() |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 57 | E = ['Commented Bar', |
| 58 | 'Foo Bar', |
| 59 | 'Internationalized Stuff', |
| 60 | 'Long Line', |
| 61 | 'Section\\with$weird%characters[\t', |
| 62 | 'Spaces', |
| 63 | 'Spacey Bar', |
| 64 | 'Spacey Bar From The Beginning', |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 65 | ] |
| 66 | if self.allow_no_value: |
| 67 | E.append(r'NoValue') |
| 68 | E.sort() |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 69 | eq = self.assertEqual |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 70 | eq(L, E) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 71 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 72 | # The use of spaces in the section names serves as a |
| 73 | # regression test for SourceForge bug #583248: |
| 74 | # http://www.python.org/sf/583248 |
| 75 | eq(cf.get('Foo Bar', 'foo'), 'bar') |
| 76 | eq(cf.get('Spacey Bar', 'foo'), 'bar') |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 77 | eq(cf.get('Spacey Bar From The Beginning', 'foo'), 'bar') |
| 78 | eq(cf.get('Spacey Bar From The Beginning', 'baz'), 'qwe') |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 79 | eq(cf.get('Commented Bar', 'foo'), 'bar') |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 80 | eq(cf.get('Commented Bar', 'baz'), 'qwe') |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 81 | eq(cf.get('Spaces', 'key with spaces'), 'value') |
| 82 | eq(cf.get('Spaces', 'another with spaces'), 'splat!') |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 83 | if self.allow_no_value: |
| 84 | eq(cf.get('NoValue', 'option-without-value'), None) |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 85 | |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 86 | self.assertNotIn('__name__', cf.options("Foo Bar"), |
| 87 | '__name__ "option" should not be exposed by the API!') |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 88 | |
| 89 | # Make sure the right things happen for remove_option(); |
| 90 | # added to include check for SourceForge bug #123324: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 91 | self.assertTrue(cf.remove_option('Foo Bar', 'foo'), |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 92 | "remove_option() failed to report existence of option") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertFalse(cf.has_option('Foo Bar', 'foo'), |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 94 | "remove_option() failed to remove option") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 95 | self.assertFalse(cf.remove_option('Foo Bar', 'foo'), |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 96 | "remove_option() failed to report non-existence of option" |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 97 | " that was removed") |
| 98 | |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 99 | with self.assertRaises(configparser.NoSectionError) as cm: |
| 100 | cf.remove_option('No Such Section', 'foo') |
| 101 | self.assertEqual(cm.exception.args, ('No Such Section',)) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 102 | |
| 103 | eq(cf.get('Long Line', 'foo'), |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 104 | 'this line is much, much longer than my editor\nlikes it.') |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 105 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 106 | def test_basic(self): |
| 107 | config_string = """\ |
| 108 | [Foo Bar] |
| 109 | foo{0[0]}bar |
| 110 | [Spacey Bar] |
| 111 | foo {0[0]} bar |
| 112 | [Spacey Bar From The Beginning] |
| 113 | foo {0[0]} bar |
| 114 | baz {0[0]} qwe |
| 115 | [Commented Bar] |
| 116 | foo{0[1]} bar {1[1]} comment |
| 117 | baz{0[0]}qwe {1[0]}another one |
| 118 | [Long Line] |
| 119 | foo{0[1]} this line is much, much longer than my editor |
| 120 | likes it. |
| 121 | [Section\\with$weird%characters[\t] |
| 122 | [Internationalized Stuff] |
| 123 | foo[bg]{0[1]} Bulgarian |
| 124 | foo{0[0]}Default |
| 125 | foo[en]{0[0]}English |
| 126 | foo[de]{0[0]}Deutsch |
| 127 | [Spaces] |
| 128 | key with spaces {0[1]} value |
| 129 | another with spaces {0[0]} splat! |
| 130 | """.format(self.delimiters, self.comment_prefixes) |
| 131 | if self.allow_no_value: |
| 132 | config_string += ( |
| 133 | "[NoValue]\n" |
| 134 | "option-without-value\n" |
| 135 | ) |
| 136 | cf = self.fromstring(config_string) |
| 137 | self.basic_test(cf) |
| 138 | if self.strict: |
| 139 | with self.assertRaises(configparser.DuplicateOptionError): |
| 140 | cf.read_string(textwrap.dedent("""\ |
| 141 | [Duplicate Options Here] |
| 142 | option {0[0]} with a value |
| 143 | option {0[1]} with another value |
| 144 | """.format(self.delimiters))) |
| 145 | with self.assertRaises(configparser.DuplicateSectionError): |
| 146 | cf.read_string(textwrap.dedent("""\ |
| 147 | [And Now For Something] |
| 148 | completely different {0[0]} True |
| 149 | [And Now For Something] |
| 150 | the larch {0[1]} 1 |
| 151 | """.format(self.delimiters))) |
| 152 | else: |
| 153 | cf.read_string(textwrap.dedent("""\ |
| 154 | [Duplicate Options Here] |
| 155 | option {0[0]} with a value |
| 156 | option {0[1]} with another value |
| 157 | """.format(self.delimiters))) |
| 158 | |
| 159 | cf.read_string(textwrap.dedent("""\ |
| 160 | [And Now For Something] |
| 161 | completely different {0[0]} True |
| 162 | [And Now For Something] |
| 163 | the larch {0[1]} 1 |
| 164 | """.format(self.delimiters))) |
| 165 | |
| 166 | def test_basic_from_dict(self): |
| 167 | config = { |
| 168 | "Foo Bar": { |
| 169 | "foo": "bar", |
| 170 | }, |
| 171 | "Spacey Bar": { |
| 172 | "foo": "bar", |
| 173 | }, |
| 174 | "Spacey Bar From The Beginning": { |
| 175 | "foo": "bar", |
| 176 | "baz": "qwe", |
| 177 | }, |
| 178 | "Commented Bar": { |
| 179 | "foo": "bar", |
| 180 | "baz": "qwe", |
| 181 | }, |
| 182 | "Long Line": { |
| 183 | "foo": "this line is much, much longer than my editor\nlikes " |
| 184 | "it.", |
| 185 | }, |
| 186 | "Section\\with$weird%characters[\t": { |
| 187 | }, |
| 188 | "Internationalized Stuff": { |
| 189 | "foo[bg]": "Bulgarian", |
| 190 | "foo": "Default", |
| 191 | "foo[en]": "English", |
| 192 | "foo[de]": "Deutsch", |
| 193 | }, |
| 194 | "Spaces": { |
| 195 | "key with spaces": "value", |
| 196 | "another with spaces": "splat!", |
| 197 | } |
| 198 | } |
| 199 | if self.allow_no_value: |
| 200 | config.update({ |
| 201 | "NoValue": { |
| 202 | "option-without-value": None, |
| 203 | } |
| 204 | }) |
| 205 | cf = self.newconfig() |
| 206 | cf.read_dict(config) |
| 207 | self.basic_test(cf) |
| 208 | if self.strict: |
| 209 | with self.assertRaises(configparser.DuplicateOptionError): |
| 210 | cf.read_dict({ |
| 211 | "Duplicate Options Here": { |
| 212 | 'option': 'with a value', |
| 213 | 'OPTION': 'with another value', |
| 214 | }, |
| 215 | }) |
| 216 | else: |
| 217 | cf.read_dict({ |
| 218 | "Duplicate Options Here": { |
| 219 | 'option': 'with a value', |
| 220 | 'OPTION': 'with another value', |
| 221 | }, |
| 222 | }) |
| 223 | |
| 224 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 225 | def test_case_sensitivity(self): |
| 226 | cf = self.newconfig() |
| 227 | cf.add_section("A") |
| 228 | cf.add_section("a") |
| 229 | L = cf.sections() |
| 230 | L.sort() |
| 231 | eq = self.assertEqual |
| 232 | eq(L, ["A", "a"]) |
| 233 | cf.set("a", "B", "value") |
| 234 | eq(cf.options("a"), ["b"]) |
| 235 | eq(cf.get("a", "b"), "value", |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 236 | "could not locate option, expecting case-insensitive option names") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 237 | self.assertTrue(cf.has_option("a", "b")) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 238 | cf.set("A", "A-B", "A-B value") |
| 239 | for opt in ("a-b", "A-b", "a-B", "A-B"): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 240 | self.assertTrue( |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 241 | cf.has_option("A", opt), |
| 242 | "has_option() returned false for option which should exist") |
| 243 | eq(cf.options("A"), ["a-b"]) |
| 244 | eq(cf.options("a"), ["b"]) |
| 245 | cf.remove_option("a", "B") |
| 246 | eq(cf.options("a"), []) |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 247 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 248 | # SF bug #432369: |
| 249 | cf = self.fromstring( |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 250 | "[MySection]\nOption{} first line\n\tsecond line\n".format( |
| 251 | self.delimiters[0])) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 252 | eq(cf.options("MySection"), ["option"]) |
| 253 | eq(cf.get("MySection", "Option"), "first line\nsecond line") |
Fred Drake | beb6713 | 2001-07-06 17:22:48 +0000 | [diff] [blame] | 254 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 255 | # SF bug #561822: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 256 | cf = self.fromstring("[section]\n" |
| 257 | "nekey{}nevalue\n".format(self.delimiters[0]), |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 258 | defaults={"key":"value"}) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 259 | self.assertTrue(cf.has_option("section", "Key")) |
Fred Drake | 309db06 | 2002-09-27 15:35:23 +0000 | [diff] [blame] | 260 | |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 261 | |
David Goodger | 68a1abd | 2004-10-03 15:40:25 +0000 | [diff] [blame] | 262 | def test_default_case_sensitivity(self): |
| 263 | cf = self.newconfig({"foo": "Bar"}) |
| 264 | self.assertEqual( |
| 265 | cf.get("DEFAULT", "Foo"), "Bar", |
| 266 | "could not locate option, expecting case-insensitive option names") |
| 267 | cf = self.newconfig({"Foo": "Bar"}) |
| 268 | self.assertEqual( |
| 269 | cf.get("DEFAULT", "Foo"), "Bar", |
| 270 | "could not locate option, expecting case-insensitive defaults") |
| 271 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 272 | def test_parse_errors(self): |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 273 | cf = self.newconfig() |
| 274 | self.parse_error(cf, configparser.ParsingError, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 275 | "[Foo]\n" |
| 276 | "{}val-without-opt-name\n".format(self.delimiters[0])) |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 277 | self.parse_error(cf, configparser.ParsingError, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 278 | "[Foo]\n" |
| 279 | "{}val-without-opt-name\n".format(self.delimiters[1])) |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 280 | e = self.parse_error(cf, configparser.MissingSectionHeaderError, |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 281 | "No Section!\n") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 282 | self.assertEqual(e.args, ('<???>', 1, "No Section!\n")) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 283 | if not self.allow_no_value: |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 284 | e = self.parse_error(cf, configparser.ParsingError, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 285 | "[Foo]\n wrong-indent\n") |
| 286 | self.assertEqual(e.args, ('<???>',)) |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 287 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 288 | def parse_error(self, cf, exc, src): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 289 | sio = io.StringIO(src) |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 290 | with self.assertRaises(exc) as cm: |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 291 | cf.read_file(sio) |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 292 | return cm.exception |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 293 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 294 | def test_query_errors(self): |
| 295 | cf = self.newconfig() |
| 296 | self.assertEqual(cf.sections(), [], |
| 297 | "new ConfigParser should have no defined sections") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 298 | self.assertFalse(cf.has_section("Foo"), |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 299 | "new ConfigParser should have no acknowledged " |
| 300 | "sections") |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 301 | with self.assertRaises(configparser.NoSectionError): |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 302 | cf.options("Foo") |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 303 | with self.assertRaises(configparser.NoSectionError): |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 304 | cf.set("foo", "bar", "value") |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 305 | e = self.get_error(cf, configparser.NoSectionError, "foo", "bar") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 306 | self.assertEqual(e.args, ("foo",)) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 307 | cf.add_section("foo") |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 308 | e = self.get_error(cf, configparser.NoOptionError, "foo", "bar") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 309 | self.assertEqual(e.args, ("bar", "foo")) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 310 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 311 | def get_error(self, cf, exc, section, option): |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 312 | try: |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 313 | cf.get(section, option) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 314 | except exc as e: |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 315 | return e |
| 316 | else: |
| 317 | self.fail("expected exception type %s.%s" |
| 318 | % (exc.__module__, exc.__name__)) |
Fred Drake | 3af0eb8 | 2002-10-25 18:09:24 +0000 | [diff] [blame] | 319 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 320 | def test_boolean(self): |
| 321 | cf = self.fromstring( |
| 322 | "[BOOLTEST]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 323 | "T1{equals}1\n" |
| 324 | "T2{equals}TRUE\n" |
| 325 | "T3{equals}True\n" |
| 326 | "T4{equals}oN\n" |
| 327 | "T5{equals}yes\n" |
| 328 | "F1{equals}0\n" |
| 329 | "F2{equals}FALSE\n" |
| 330 | "F3{equals}False\n" |
| 331 | "F4{equals}oFF\n" |
| 332 | "F5{equals}nO\n" |
| 333 | "E1{equals}2\n" |
| 334 | "E2{equals}foo\n" |
| 335 | "E3{equals}-1\n" |
| 336 | "E4{equals}0.1\n" |
| 337 | "E5{equals}FALSE AND MORE".format(equals=self.delimiters[0]) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 338 | ) |
| 339 | for x in range(1, 5): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 340 | self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x)) |
| 341 | self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x)) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 342 | self.assertRaises(ValueError, |
| 343 | cf.getboolean, 'BOOLTEST', 'e%d' % x) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 344 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 345 | def test_weird_errors(self): |
| 346 | cf = self.newconfig() |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 347 | cf.add_section("Foo") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 348 | with self.assertRaises(configparser.DuplicateSectionError) as cm: |
| 349 | cf.add_section("Foo") |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 350 | e = cm.exception |
| 351 | self.assertEqual(str(e), "Section 'Foo' already exists") |
| 352 | self.assertEqual(e.args, ("Foo", None, None)) |
| 353 | |
| 354 | if self.strict: |
| 355 | with self.assertRaises(configparser.DuplicateSectionError) as cm: |
| 356 | cf.read_string(textwrap.dedent("""\ |
| 357 | [Foo] |
| 358 | will this be added{equals}True |
| 359 | [Bar] |
| 360 | what about this{equals}True |
| 361 | [Foo] |
| 362 | oops{equals}this won't |
| 363 | """.format(equals=self.delimiters[0])), source='<foo-bar>') |
| 364 | e = cm.exception |
| 365 | self.assertEqual(str(e), "While reading from <foo-bar> [line 5]: " |
| 366 | "section 'Foo' already exists") |
| 367 | self.assertEqual(e.args, ("Foo", '<foo-bar>', 5)) |
| 368 | |
| 369 | with self.assertRaises(configparser.DuplicateOptionError) as cm: |
| 370 | cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}}) |
| 371 | e = cm.exception |
| 372 | self.assertEqual(str(e), "While reading from <dict>: option 'opt' " |
| 373 | "in section 'Bar' already exists") |
| 374 | self.assertEqual(e.args, ("Bar", "opt", "<dict>", None)) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 375 | |
| 376 | def test_write(self): |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 377 | config_string = ( |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 378 | "[Long Line]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 379 | "foo{0[0]} this line is much, much longer than my editor\n" |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 380 | " likes it.\n" |
| 381 | "[DEFAULT]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 382 | "foo{0[1]} another very\n" |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 383 | " long line\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 384 | "[Long Line - With Comments!]\n" |
| 385 | "test {0[1]} we {comment} can\n" |
| 386 | " also {comment} place\n" |
| 387 | " comments {comment} in\n" |
| 388 | " multiline {comment} values" |
| 389 | "\n".format(self.delimiters, comment=self.comment_prefixes[0]) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 390 | ) |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 391 | if self.allow_no_value: |
| 392 | config_string += ( |
| 393 | "[Valueless]\n" |
| 394 | "option-without-value\n" |
| 395 | ) |
| 396 | |
| 397 | cf = self.fromstring(config_string) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 398 | output = io.StringIO() |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 399 | cf.write(output) |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 400 | expect_string = ( |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 401 | "[DEFAULT]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 402 | "foo {equals} another very\n" |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 403 | "\tlong line\n" |
| 404 | "\n" |
| 405 | "[Long Line]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 406 | "foo {equals} this line is much, much longer than my editor\n" |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 407 | "\tlikes it.\n" |
| 408 | "\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 409 | "[Long Line - With Comments!]\n" |
| 410 | "test {equals} we\n" |
| 411 | "\talso\n" |
| 412 | "\tcomments\n" |
| 413 | "\tmultiline\n" |
| 414 | "\n".format(equals=self.delimiters[0]) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 415 | ) |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 416 | if self.allow_no_value: |
| 417 | expect_string += ( |
| 418 | "[Valueless]\n" |
| 419 | "option-without-value\n" |
| 420 | "\n" |
| 421 | ) |
| 422 | self.assertEqual(output.getvalue(), expect_string) |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 423 | |
Fred Drake | abc086f | 2004-05-18 03:29:52 +0000 | [diff] [blame] | 424 | def test_set_string_types(self): |
| 425 | cf = self.fromstring("[sect]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 426 | "option1{eq}foo\n".format(eq=self.delimiters[0])) |
Fred Drake | abc086f | 2004-05-18 03:29:52 +0000 | [diff] [blame] | 427 | # Check that we don't get an exception when setting values in |
| 428 | # an existing section using strings: |
| 429 | class mystr(str): |
| 430 | pass |
| 431 | cf.set("sect", "option1", "splat") |
| 432 | cf.set("sect", "option1", mystr("splat")) |
| 433 | cf.set("sect", "option2", "splat") |
| 434 | cf.set("sect", "option2", mystr("splat")) |
Walter Dörwald | 5de48bd | 2007-06-11 21:38:39 +0000 | [diff] [blame] | 435 | cf.set("sect", "option1", "splat") |
| 436 | cf.set("sect", "option2", "splat") |
Fred Drake | abc086f | 2004-05-18 03:29:52 +0000 | [diff] [blame] | 437 | |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 438 | def test_read_returns_file_list(self): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 439 | if self.delimiters[0] != '=': |
| 440 | # skip reading the file if we're using an incompatible format |
| 441 | return |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 442 | file1 = support.findfile("cfgparser.1") |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 443 | # check when we pass a mix of readable and non-readable files: |
| 444 | cf = self.newconfig() |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 445 | parsed_files = cf.read([file1, "nonexistent-file"]) |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 446 | self.assertEqual(parsed_files, [file1]) |
| 447 | self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 448 | # check when we pass only a filename: |
| 449 | cf = self.newconfig() |
| 450 | parsed_files = cf.read(file1) |
| 451 | self.assertEqual(parsed_files, [file1]) |
| 452 | self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 453 | # check when we pass only missing files: |
| 454 | cf = self.newconfig() |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 455 | parsed_files = cf.read(["nonexistent-file"]) |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 456 | self.assertEqual(parsed_files, []) |
| 457 | # check when we pass no files: |
| 458 | cf = self.newconfig() |
| 459 | parsed_files = cf.read([]) |
| 460 | self.assertEqual(parsed_files, []) |
| 461 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 462 | # shared by subclasses |
| 463 | def get_interpolation_config(self): |
| 464 | return self.fromstring( |
| 465 | "[Foo]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 466 | "bar{equals}something %(with1)s interpolation (1 step)\n" |
| 467 | "bar9{equals}something %(with9)s lots of interpolation (9 steps)\n" |
| 468 | "bar10{equals}something %(with10)s lots of interpolation (10 steps)\n" |
| 469 | "bar11{equals}something %(with11)s lots of interpolation (11 steps)\n" |
| 470 | "with11{equals}%(with10)s\n" |
| 471 | "with10{equals}%(with9)s\n" |
| 472 | "with9{equals}%(with8)s\n" |
| 473 | "with8{equals}%(With7)s\n" |
| 474 | "with7{equals}%(WITH6)s\n" |
| 475 | "with6{equals}%(with5)s\n" |
| 476 | "With5{equals}%(with4)s\n" |
| 477 | "WITH4{equals}%(with3)s\n" |
| 478 | "with3{equals}%(with2)s\n" |
| 479 | "with2{equals}%(with1)s\n" |
| 480 | "with1{equals}with\n" |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 481 | "\n" |
| 482 | "[Mutual Recursion]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 483 | "foo{equals}%(bar)s\n" |
| 484 | "bar{equals}%(foo)s\n" |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 485 | "\n" |
| 486 | "[Interpolation Error]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 487 | "name{equals}%(reference)s\n".format(equals=self.delimiters[0]), |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 488 | # no definition for 'reference' |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 489 | defaults={"getname": "%(__name__)s"}) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 490 | |
Fred Drake | 98e3b29 | 2002-10-25 20:42:44 +0000 | [diff] [blame] | 491 | def check_items_config(self, expected): |
| 492 | cf = self.fromstring( |
| 493 | "[section]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 494 | "name {0[0]} value\n" |
| 495 | "key{0[1]} |%(name)s| \n" |
| 496 | "getdefault{0[1]} |%(default)s|\n" |
| 497 | "getname{0[1]} |%(__name__)s|".format(self.delimiters), |
Fred Drake | 98e3b29 | 2002-10-25 20:42:44 +0000 | [diff] [blame] | 498 | defaults={"default": "<default>"}) |
| 499 | L = list(cf.items("section")) |
| 500 | L.sort() |
| 501 | self.assertEqual(L, expected) |
| 502 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 503 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 504 | class StrictTestCase(BasicTestCase): |
| 505 | config_class = configparser.RawConfigParser |
| 506 | strict = True |
| 507 | |
| 508 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 509 | class ConfigParserTestCase(BasicTestCase): |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 510 | config_class = configparser.ConfigParser |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 511 | |
| 512 | def test_interpolation(self): |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 513 | rawval = { |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 514 | configparser.ConfigParser: ("something %(with11)s " |
| 515 | "lots of interpolation (11 steps)"), |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 516 | configparser.SafeConfigParser: "%(with1)s", |
| 517 | } |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 518 | cf = self.get_interpolation_config() |
| 519 | eq = self.assertEqual |
| 520 | eq(cf.get("Foo", "getname"), "Foo") |
| 521 | eq(cf.get("Foo", "bar"), "something with interpolation (1 step)") |
| 522 | eq(cf.get("Foo", "bar9"), |
| 523 | "something with lots of interpolation (9 steps)") |
| 524 | eq(cf.get("Foo", "bar10"), |
| 525 | "something with lots of interpolation (10 steps)") |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 526 | e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 527 | self.assertEqual(e.args, ("bar11", "Foo", rawval[self.config_class])) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 528 | |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 529 | def test_interpolation_missing_value(self): |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 530 | rawval = { |
| 531 | configparser.ConfigParser: '%(reference)s', |
| 532 | configparser.SafeConfigParser: '', |
| 533 | } |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 534 | cf = self.get_interpolation_config() |
| 535 | e = self.get_error(cf, configparser.InterpolationMissingOptionError, |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 536 | "Interpolation Error", "name") |
| 537 | self.assertEqual(e.reference, "reference") |
| 538 | self.assertEqual(e.section, "Interpolation Error") |
| 539 | self.assertEqual(e.option, "name") |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 540 | self.assertEqual(e.args, ('name', 'Interpolation Error', |
| 541 | rawval[self.config_class], 'reference')) |
Fred Drake | 5478219 | 2002-12-31 06:57:25 +0000 | [diff] [blame] | 542 | |
Fred Drake | 98e3b29 | 2002-10-25 20:42:44 +0000 | [diff] [blame] | 543 | def test_items(self): |
| 544 | self.check_items_config([('default', '<default>'), |
| 545 | ('getdefault', '|<default>|'), |
| 546 | ('getname', '|section|'), |
| 547 | ('key', '|value|'), |
| 548 | ('name', 'value')]) |
| 549 | |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 550 | def test_set_nonstring_types(self): |
| 551 | cf = self.newconfig() |
| 552 | cf.add_section('non-string') |
| 553 | cf.set('non-string', 'int', 1) |
| 554 | cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%(']) |
| 555 | cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1, |
| 556 | '%(list)': '%(list)'}) |
| 557 | cf.set('non-string', 'string_with_interpolation', '%(list)s') |
| 558 | self.assertEqual(cf.get('non-string', 'int', raw=True), 1) |
| 559 | self.assertRaises(TypeError, cf.get, 'non-string', 'int') |
| 560 | self.assertEqual(cf.get('non-string', 'list', raw=True), |
| 561 | [0, 1, 1, 2, 3, 5, 8, 13, '%(']) |
| 562 | self.assertRaises(TypeError, cf.get, 'non-string', 'list') |
| 563 | self.assertEqual(cf.get('non-string', 'dict', raw=True), |
| 564 | {'pi': 3.14159, '%(': 1, '%(list)': '%(list)'}) |
| 565 | self.assertRaises(TypeError, cf.get, 'non-string', 'dict') |
| 566 | self.assertEqual(cf.get('non-string', 'string_with_interpolation', |
| 567 | raw=True), '%(list)s') |
| 568 | self.assertRaises(ValueError, cf.get, 'non-string', |
| 569 | 'string_with_interpolation', raw=False) |
| 570 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 571 | class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase): |
| 572 | delimiters = (':=', '$') |
| 573 | comment_prefixes = ('//', '"') |
| 574 | |
| 575 | class MultilineValuesTestCase(BasicTestCase): |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 576 | config_class = configparser.ConfigParser |
| 577 | wonderful_spam = ("I'm having spam spam spam spam " |
| 578 | "spam spam spam beaked beans spam " |
| 579 | "spam spam and spam!").replace(' ', '\t\n') |
| 580 | |
| 581 | def setUp(self): |
| 582 | cf = self.newconfig() |
| 583 | for i in range(100): |
| 584 | s = 'section{}'.format(i) |
| 585 | cf.add_section(s) |
| 586 | for j in range(10): |
| 587 | cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam) |
| 588 | with open(support.TESTFN, 'w') as f: |
| 589 | cf.write(f) |
| 590 | |
| 591 | def tearDown(self): |
| 592 | os.unlink(support.TESTFN) |
| 593 | |
| 594 | def test_dominating_multiline_values(self): |
| 595 | # We're reading from file because this is where the code changed |
| 596 | # during performance updates in Python 3.2 |
| 597 | cf_from_file = self.newconfig() |
| 598 | with open(support.TESTFN) as f: |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 599 | cf_from_file.read_file(f) |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 600 | self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'), |
| 601 | self.wonderful_spam.replace('\t\n', '\n')) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 602 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 603 | class RawConfigParserTestCase(BasicTestCase): |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 604 | config_class = configparser.RawConfigParser |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 605 | |
| 606 | def test_interpolation(self): |
| 607 | cf = self.get_interpolation_config() |
| 608 | eq = self.assertEqual |
| 609 | eq(cf.get("Foo", "getname"), "%(__name__)s") |
| 610 | eq(cf.get("Foo", "bar"), |
| 611 | "something %(with1)s interpolation (1 step)") |
| 612 | eq(cf.get("Foo", "bar9"), |
| 613 | "something %(with9)s lots of interpolation (9 steps)") |
| 614 | eq(cf.get("Foo", "bar10"), |
| 615 | "something %(with10)s lots of interpolation (10 steps)") |
| 616 | eq(cf.get("Foo", "bar11"), |
| 617 | "something %(with11)s lots of interpolation (11 steps)") |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 618 | |
Fred Drake | 98e3b29 | 2002-10-25 20:42:44 +0000 | [diff] [blame] | 619 | def test_items(self): |
| 620 | self.check_items_config([('default', '<default>'), |
| 621 | ('getdefault', '|%(default)s|'), |
| 622 | ('getname', '|%(__name__)s|'), |
| 623 | ('key', '|%(name)s|'), |
| 624 | ('name', 'value')]) |
| 625 | |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 626 | def test_set_nonstring_types(self): |
| 627 | cf = self.newconfig() |
| 628 | cf.add_section('non-string') |
| 629 | cf.set('non-string', 'int', 1) |
| 630 | cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13]) |
| 631 | cf.set('non-string', 'dict', {'pi': 3.14159}) |
| 632 | self.assertEqual(cf.get('non-string', 'int'), 1) |
| 633 | self.assertEqual(cf.get('non-string', 'list'), |
| 634 | [0, 1, 1, 2, 3, 5, 8, 13]) |
| 635 | self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159}) |
Tim Peters | ab9b32c | 2004-10-03 18:35:19 +0000 | [diff] [blame] | 636 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 637 | class RawConfigParserTestCaseNonStandardDelimiters(RawConfigParserTestCase): |
| 638 | delimiters = (':=', '$') |
| 639 | comment_prefixes = ('//', '"') |
| 640 | |
| 641 | class RawConfigParserTestSambaConf(BasicTestCase): |
| 642 | config_class = configparser.RawConfigParser |
| 643 | comment_prefixes = ('#', ';', '//', '----') |
| 644 | empty_lines_in_values = False |
| 645 | |
| 646 | def test_reading(self): |
| 647 | smbconf = support.findfile("cfgparser.2") |
| 648 | # check when we pass a mix of readable and non-readable files: |
| 649 | cf = self.newconfig() |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 650 | parsed_files = cf.read([smbconf, "nonexistent-file"], encoding='utf-8') |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 651 | self.assertEqual(parsed_files, [smbconf]) |
| 652 | sections = ['global', 'homes', 'printers', |
| 653 | 'print$', 'pdf-generator', 'tmp', 'Agustin'] |
| 654 | self.assertEqual(cf.sections(), sections) |
| 655 | self.assertEqual(cf.get("global", "workgroup"), "MDKGROUP") |
| 656 | self.assertEqual(cf.getint("global", "max log size"), 50) |
| 657 | self.assertEqual(cf.get("global", "hosts allow"), "127.") |
| 658 | self.assertEqual(cf.get("tmp", "echo command"), "cat %s; rm %s") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 659 | |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 660 | class SafeConfigParserTestCase(ConfigParserTestCase): |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 661 | config_class = configparser.SafeConfigParser |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 662 | |
| 663 | def test_safe_interpolation(self): |
| 664 | # See http://www.python.org/sf/511737 |
| 665 | cf = self.fromstring("[section]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 666 | "option1{eq}xxx\n" |
| 667 | "option2{eq}%(option1)s/xxx\n" |
| 668 | "ok{eq}%(option1)s/%%s\n" |
| 669 | "not_ok{eq}%(option2)s/%%s".format( |
| 670 | eq=self.delimiters[0])) |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 671 | self.assertEqual(cf.get("section", "ok"), "xxx/%s") |
| 672 | self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s") |
| 673 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 674 | def test_set_malformatted_interpolation(self): |
| 675 | cf = self.fromstring("[sect]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 676 | "option1{eq}foo\n".format(eq=self.delimiters[0])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 677 | |
| 678 | self.assertEqual(cf.get('sect', "option1"), "foo") |
| 679 | |
| 680 | self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo") |
| 681 | self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%") |
| 682 | self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo") |
| 683 | |
| 684 | self.assertEqual(cf.get('sect', "option1"), "foo") |
| 685 | |
Georg Brandl | 1f9fa31 | 2009-04-27 16:42:58 +0000 | [diff] [blame] | 686 | # bug #5741: double percents are *not* malformed |
| 687 | cf.set("sect", "option2", "foo%%bar") |
| 688 | self.assertEqual(cf.get("sect", "option2"), "foo%bar") |
| 689 | |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 690 | def test_set_nonstring_types(self): |
| 691 | cf = self.fromstring("[sect]\n" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 692 | "option1{eq}foo\n".format(eq=self.delimiters[0])) |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 693 | # Check that we get a TypeError when setting non-string values |
| 694 | # in an existing section: |
| 695 | self.assertRaises(TypeError, cf.set, "sect", "option1", 1) |
| 696 | self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0) |
| 697 | self.assertRaises(TypeError, cf.set, "sect", "option1", object()) |
| 698 | self.assertRaises(TypeError, cf.set, "sect", "option2", 1) |
| 699 | self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0) |
| 700 | self.assertRaises(TypeError, cf.set, "sect", "option2", object()) |
| 701 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 702 | def test_add_section_default_1(self): |
| 703 | cf = self.newconfig() |
| 704 | self.assertRaises(ValueError, cf.add_section, "default") |
| 705 | |
| 706 | def test_add_section_default_2(self): |
| 707 | cf = self.newconfig() |
| 708 | self.assertRaises(ValueError, cf.add_section, "DEFAULT") |
| 709 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 710 | class SafeConfigParserTestCaseNonStandardDelimiters(SafeConfigParserTestCase): |
| 711 | delimiters = (':=', '$') |
| 712 | comment_prefixes = ('//', '"') |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 713 | |
| 714 | class SafeConfigParserTestCaseNoValue(SafeConfigParserTestCase): |
| 715 | allow_no_value = True |
| 716 | |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 717 | class SafeConfigParserTestCaseTrickyFile(CfgParserTestCaseClass): |
| 718 | config_class = configparser.SafeConfigParser |
| 719 | delimiters = {'='} |
| 720 | comment_prefixes = {'#'} |
| 721 | allow_no_value = True |
| 722 | |
| 723 | def test_cfgparser_dot_3(self): |
| 724 | tricky = support.findfile("cfgparser.3") |
| 725 | cf = self.newconfig() |
| 726 | self.assertEqual(len(cf.read(tricky, encoding='utf-8')), 1) |
| 727 | self.assertEqual(cf.sections(), ['strange', |
| 728 | 'corruption', |
| 729 | 'yeah, sections can be ' |
| 730 | 'indented as well', |
| 731 | 'another one!', |
| 732 | 'no values here', |
| 733 | 'tricky interpolation', |
| 734 | 'more interpolation']) |
| 735 | #self.assertEqual(cf.getint('DEFAULT', 'go', vars={'interpolate': '-1'}), |
| 736 | # -1) |
| 737 | self.assertEqual(len(cf.get('strange', 'other').split('\n')), 4) |
| 738 | self.assertEqual(len(cf.get('corruption', 'value').split('\n')), 10) |
| 739 | longname = 'yeah, sections can be indented as well' |
| 740 | self.assertFalse(cf.getboolean(longname, 'are they subsections')) |
| 741 | self.assertEquals(cf.get(longname, 'lets use some Unicode'), |
| 742 | '片仮名') |
| 743 | self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and |
| 744 | # `go` from DEFAULT |
| 745 | with self.assertRaises(configparser.InterpolationMissingOptionError): |
| 746 | cf.items('no values here') |
| 747 | self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this') |
| 748 | self.assertEqual(cf.get('tricky interpolation', 'lets'), |
| 749 | cf.get('tricky interpolation', 'go')) |
| 750 | self.assertEqual(cf.get('more interpolation', 'lets'), 'go shopping') |
| 751 | |
| 752 | def test_unicode_failure(self): |
| 753 | tricky = support.findfile("cfgparser.3") |
| 754 | cf = self.newconfig() |
| 755 | with self.assertRaises(UnicodeDecodeError): |
| 756 | cf.read(tricky, encoding='ascii') |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 757 | |
Fred Drake | 8844441 | 2010-09-03 04:22:36 +0000 | [diff] [blame] | 758 | |
| 759 | class Issue7005TestCase(unittest.TestCase): |
| 760 | """Test output when None is set() as a value and allow_no_value == False. |
| 761 | |
| 762 | http://bugs.python.org/issue7005 |
| 763 | |
| 764 | """ |
| 765 | |
| 766 | expected_output = "[section]\noption = None\n\n" |
| 767 | |
| 768 | def prepare(self, config_class): |
| 769 | # This is the default, but that's the point. |
| 770 | cp = config_class(allow_no_value=False) |
| 771 | cp.add_section("section") |
| 772 | cp.set("section", "option", None) |
| 773 | sio = io.StringIO() |
| 774 | cp.write(sio) |
| 775 | return sio.getvalue() |
| 776 | |
| 777 | def test_none_as_value_stringified(self): |
| 778 | output = self.prepare(configparser.ConfigParser) |
| 779 | self.assertEqual(output, self.expected_output) |
| 780 | |
| 781 | def test_none_as_value_stringified_raw(self): |
| 782 | output = self.prepare(configparser.RawConfigParser) |
| 783 | self.assertEqual(output, self.expected_output) |
| 784 | |
| 785 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 786 | class SortedTestCase(RawConfigParserTestCase): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 787 | dict_type = SortedDict |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 788 | |
| 789 | def test_sorted(self): |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 790 | cf = self.fromstring("[b]\n" |
| 791 | "o4=1\n" |
| 792 | "o3=2\n" |
| 793 | "o2=3\n" |
| 794 | "o1=4\n" |
| 795 | "[a]\n" |
| 796 | "k=v\n") |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 797 | output = io.StringIO() |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 798 | cf.write(output) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 799 | self.assertEquals(output.getvalue(), |
| 800 | "[a]\n" |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 801 | "k = v\n\n" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 802 | "[b]\n" |
| 803 | "o1 = 4\n" |
| 804 | "o2 = 3\n" |
| 805 | "o3 = 2\n" |
| 806 | "o4 = 1\n\n") |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 807 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 808 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 809 | class CompatibleTestCase(CfgParserTestCaseClass): |
| 810 | config_class = configparser.RawConfigParser |
| 811 | comment_prefixes = configparser.RawConfigParser._COMPATIBLE |
| 812 | |
| 813 | def test_comment_handling(self): |
| 814 | config_string = textwrap.dedent("""\ |
| 815 | [Commented Bar] |
| 816 | baz=qwe ; a comment |
| 817 | foo: bar # not a comment! |
| 818 | # but this is a comment |
| 819 | ; another comment |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 820 | quirk: this;is not a comment |
Georg Brandl | 7b280e9 | 2010-07-31 20:13:44 +0000 | [diff] [blame] | 821 | ; a space must precede an inline comment |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 822 | """) |
| 823 | cf = self.fromstring(config_string) |
| 824 | self.assertEqual(cf.get('Commented Bar', 'foo'), 'bar # not a comment!') |
| 825 | self.assertEqual(cf.get('Commented Bar', 'baz'), 'qwe') |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 826 | self.assertEqual(cf.get('Commented Bar', 'quirk'), 'this;is not a comment') |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 827 | |
| 828 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 829 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 830 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 831 | ConfigParserTestCase, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 832 | ConfigParserTestCaseNonStandardDelimiters, |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 833 | MultilineValuesTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 834 | RawConfigParserTestCase, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 835 | RawConfigParserTestCaseNonStandardDelimiters, |
| 836 | RawConfigParserTestSambaConf, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 837 | SafeConfigParserTestCase, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 838 | SafeConfigParserTestCaseNonStandardDelimiters, |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 839 | SafeConfigParserTestCaseNoValue, |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 840 | SafeConfigParserTestCaseTrickyFile, |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 841 | SortedTestCase, |
Fred Drake | 8844441 | 2010-09-03 04:22:36 +0000 | [diff] [blame] | 842 | Issue7005TestCase, |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 843 | StrictTestCase, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 844 | CompatibleTestCase, |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 845 | ) |
| 846 | |
Fred Drake | 3af0eb8 | 2002-10-25 18:09:24 +0000 | [diff] [blame] | 847 | |
Fred Drake | c6f2891 | 2002-10-25 19:40:49 +0000 | [diff] [blame] | 848 | if __name__ == "__main__": |
| 849 | test_main() |