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