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