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