Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 1 | import ConfigParser |
| 2 | import StringIO |
| 3 | |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 4 | from test.test_support import TestFailed, verify |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 5 | |
| 6 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 7 | def basic(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 8 | print "Testing basic accessors..." |
| 9 | cf = ConfigParser.ConfigParser() |
| 10 | sio = StringIO.StringIO(src) |
| 11 | cf.readfp(sio) |
| 12 | L = cf.sections() |
| 13 | L.sort() |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 14 | verify(L == [r'Commented Bar', |
| 15 | r'Foo Bar', |
| 16 | r'Internationalized Stuff', |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 17 | r'Long Line', |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 18 | r'Section\with$weird%characters[' '\t', |
| 19 | r'Spacey Bar', |
| 20 | ], |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 21 | "unexpected list of section names") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 22 | |
| 23 | # The use of spaces in the section names serves as a regression test for |
| 24 | # SourceForge bug #115357. |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 25 | # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357 |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 26 | verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar') |
| 27 | verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar') |
| 28 | verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 29 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 30 | verify('__name__' not in cf.options("Foo Bar"), |
| 31 | '__name__ "option" should not be exposed by the API!') |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 32 | |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 33 | # Make sure the right things happen for remove_option(); |
| 34 | # added to include check for SourceForge bug #123324: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 35 | verify(cf.remove_option('Foo Bar', 'foo'), |
| 36 | "remove_option() failed to report existance of option") |
| 37 | verify(not cf.has_option('Foo Bar', 'foo'), |
| 38 | "remove_option() failed to remove option") |
| 39 | verify(not cf.remove_option('Foo Bar', 'foo'), |
| 40 | "remove_option() failed to report non-existance of option" |
| 41 | " that was removed") |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 42 | try: |
| 43 | cf.remove_option('No Such Section', 'foo') |
| 44 | except ConfigParser.NoSectionError: |
| 45 | pass |
| 46 | else: |
| 47 | raise TestFailed( |
| 48 | "remove_option() failed to report non-existance of option" |
| 49 | " that never existed") |
| 50 | |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 51 | verify(cf.get('Long Line', 'foo', raw=1) == |
| 52 | 'this line is much, much longer than my editor\nlikes it.') |
Fred Drake | 3d5f7e8 | 2000-12-04 16:30:40 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 54 | |
| 55 | def write(src): |
| 56 | print "Testing writing of files..." |
| 57 | cf = ConfigParser.ConfigParser() |
| 58 | sio = StringIO.StringIO(src) |
| 59 | cf.readfp(sio) |
| 60 | output = StringIO.StringIO() |
| 61 | cf.write(output) |
| 62 | verify(output, """[DEFAULT] |
| 63 | foo = another very |
| 64 | long line |
| 65 | |
| 66 | [Long Line] |
| 67 | foo = this line is much, much longer than my editor |
| 68 | likes it. |
| 69 | """) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 70 | |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 71 | def case_sensitivity(): |
| 72 | print "Testing case sensitivity..." |
| 73 | cf = ConfigParser.ConfigParser() |
| 74 | cf.add_section("A") |
| 75 | cf.add_section("a") |
| 76 | L = cf.sections() |
| 77 | L.sort() |
| 78 | verify(L == ["A", "a"]) |
| 79 | cf.set("a", "B", "value") |
| 80 | verify(cf.options("a") == ["b"]) |
| 81 | verify(cf.get("a", "b", raw=1) == "value", |
| 82 | "could not locate option, expecting case-insensitive option names") |
| 83 | verify(cf.has_option("a", "b")) |
| 84 | cf.set("A", "A-B", "A-B value") |
| 85 | for opt in ("a-b", "A-b", "a-B", "A-B"): |
| 86 | verify(cf.has_option("A", opt), |
| 87 | "has_option() returned false for option which should exist") |
| 88 | verify(cf.options("A") == ["a-b"]) |
| 89 | verify(cf.options("a") == ["b"]) |
| 90 | cf.remove_option("a", "B") |
| 91 | verify(cf.options("a") == []) |
| 92 | |
Fred Drake | beb6713 | 2001-07-06 17:22:48 +0000 | [diff] [blame] | 93 | # SF bug #432369: |
| 94 | cf = ConfigParser.ConfigParser() |
| 95 | sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n") |
| 96 | cf.readfp(sio) |
| 97 | verify(cf.options("MySection") == ["option"]) |
| 98 | verify(cf.get("MySection", "Option") == "first line\nsecond line") |
| 99 | |
Fred Drake | 309db06 | 2002-09-27 15:35:23 +0000 | [diff] [blame] | 100 | # SF bug #561822: |
| 101 | cf = ConfigParser.ConfigParser(defaults={"key":"value"}) |
| 102 | cf.readfp(StringIO.StringIO("[section]\nnekey=nevalue\n")) |
| 103 | verify(cf.has_option("section", "Key")) |
| 104 | |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 105 | |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 106 | def boolean(src): |
| 107 | print "Testing interpretation of boolean Values..." |
| 108 | cf = ConfigParser.ConfigParser() |
| 109 | sio = StringIO.StringIO(src) |
| 110 | cf.readfp(sio) |
| 111 | for x in range(1, 5): |
| 112 | verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1) |
| 113 | for x in range(1, 5): |
| 114 | verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0) |
| 115 | for x in range(1, 5): |
| 116 | try: |
| 117 | cf.getboolean('BOOLTEST', 'e%d' % (x)) |
| 118 | except ValueError: |
| 119 | pass |
| 120 | else: |
| 121 | raise TestFailed( |
| 122 | "getboolean() failed to report a non boolean value") |
| 123 | |
| 124 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 125 | def interpolation(src): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 126 | print "Testing value interpolation..." |
| 127 | cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"}) |
| 128 | sio = StringIO.StringIO(src) |
| 129 | cf.readfp(sio) |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 130 | verify(cf.get("Foo", "getname") == "Foo") |
| 131 | verify(cf.get("Foo", "bar") == "something with interpolation (1 step)") |
| 132 | verify(cf.get("Foo", "bar9") |
| 133 | == "something with lots of interpolation (9 steps)") |
| 134 | verify(cf.get("Foo", "bar10") |
| 135 | == "something with lots of interpolation (10 steps)") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 136 | expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11") |
| 137 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 138 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 139 | def parse_errors(): |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 140 | print "Testing parse errors..." |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 141 | expect_parse_error(ConfigParser.ParsingError, |
| 142 | """[Foo]\n extra-spaces: splat\n""") |
| 143 | expect_parse_error(ConfigParser.ParsingError, |
| 144 | """[Foo]\n extra-spaces= splat\n""") |
| 145 | expect_parse_error(ConfigParser.ParsingError, |
| 146 | """[Foo]\noption-without-value\n""") |
| 147 | expect_parse_error(ConfigParser.ParsingError, |
| 148 | """[Foo]\n:value-without-option-name\n""") |
| 149 | expect_parse_error(ConfigParser.ParsingError, |
| 150 | """[Foo]\n=value-without-option-name\n""") |
| 151 | expect_parse_error(ConfigParser.MissingSectionHeaderError, |
| 152 | """No Section!\n""") |
| 153 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 154 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 155 | def query_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 156 | print "Testing query interface..." |
| 157 | cf = ConfigParser.ConfigParser() |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 158 | verify(cf.sections() == [], |
| 159 | "new ConfigParser should have no defined sections") |
| 160 | verify(not cf.has_section("Foo"), |
| 161 | "new ConfigParser should have no acknowledged sections") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 162 | try: |
| 163 | cf.options("Foo") |
| 164 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 165 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 166 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 167 | raise TestFailed( |
| 168 | "Failed to catch expected NoSectionError from options()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 169 | try: |
| 170 | cf.set("foo", "bar", "value") |
| 171 | except ConfigParser.NoSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 172 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 173 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 174 | raise TestFailed("Failed to catch expected NoSectionError from set()") |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 175 | expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar") |
| 176 | cf.add_section("foo") |
| 177 | expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar") |
| 178 | |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 179 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 180 | def weird_errors(): |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 181 | print "Testing miscellaneous error conditions..." |
| 182 | cf = ConfigParser.ConfigParser() |
| 183 | cf.add_section("Foo") |
| 184 | try: |
| 185 | cf.add_section("Foo") |
| 186 | except ConfigParser.DuplicateSectionError, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 187 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 188 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 189 | raise TestFailed("Failed to catch expected DuplicateSectionError") |
| 190 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 191 | |
| 192 | def expect_get_error(cf, exctype, section, option, raw=0): |
| 193 | try: |
| 194 | cf.get(section, option, raw=raw) |
| 195 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 196 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 197 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 198 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 199 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 200 | |
| 201 | def expect_parse_error(exctype, src): |
| 202 | cf = ConfigParser.ConfigParser() |
| 203 | sio = StringIO.StringIO(src) |
| 204 | try: |
| 205 | cf.readfp(sio) |
| 206 | except exctype, e: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 207 | pass |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 208 | else: |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 209 | raise TestFailed("Failed to catch expected " + exctype.__name__) |
| 210 | |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 211 | |
| 212 | basic(r""" |
| 213 | [Foo Bar] |
| 214 | foo=bar |
| 215 | [Spacey Bar] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 216 | foo = bar |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 217 | [Commented Bar] |
| 218 | foo: bar ; comment |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 219 | [Long Line] |
| 220 | foo: this line is much, much longer than my editor |
| 221 | likes it. |
Fred Drake | cc1f951 | 2001-02-14 15:30:31 +0000 | [diff] [blame] | 222 | [Section\with$weird%characters[""" '\t' r"""] |
Fred Drake | 95b96d3 | 2001-02-12 17:23:20 +0000 | [diff] [blame] | 223 | [Internationalized Stuff] |
| 224 | foo[bg]: Bulgarian |
| 225 | foo=Default |
| 226 | foo[en]=English |
| 227 | foo[de]=Deutsch |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 228 | """) |
Andrew M. Kuchling | 1bf7117 | 2002-03-08 18:10:12 +0000 | [diff] [blame] | 229 | write("""[Long Line] |
| 230 | foo: this line is much, much longer than my editor |
| 231 | likes it. |
| 232 | [DEFAULT] |
| 233 | foo: another very |
| 234 | long line""") |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 235 | case_sensitivity() |
Fred Drake | 168bead | 2001-10-08 17:13:12 +0000 | [diff] [blame] | 236 | boolean(r""" |
| 237 | [BOOLTEST] |
| 238 | T1=1 |
| 239 | T2=TRUE |
| 240 | T3=True |
| 241 | T4=oN |
| 242 | T5=yes |
| 243 | F1=0 |
| 244 | F2=FALSE |
| 245 | F3=False |
| 246 | F4=oFF |
| 247 | F5=nO |
| 248 | E1=2 |
| 249 | E2=foo |
| 250 | E3=-1 |
| 251 | E4=0.1 |
| 252 | E5=FALSE AND MORE |
| 253 | """) |
Fred Drake | 8ef6767 | 2000-09-27 22:45:25 +0000 | [diff] [blame] | 254 | interpolation(r""" |
| 255 | [Foo] |
| 256 | bar=something %(with1)s interpolation (1 step) |
| 257 | bar9=something %(with9)s lots of interpolation (9 steps) |
| 258 | bar10=something %(with10)s lots of interpolation (10 steps) |
| 259 | bar11=something %(with11)s lots of interpolation (11 steps) |
| 260 | with11=%(with10)s |
| 261 | with10=%(with9)s |
| 262 | with9=%(with8)s |
| 263 | with8=%(with7)s |
| 264 | with7=%(with6)s |
| 265 | with6=%(with5)s |
| 266 | with5=%(with4)s |
| 267 | with4=%(with3)s |
| 268 | with3=%(with2)s |
| 269 | with2=%(with1)s |
| 270 | with1=with |
| 271 | |
| 272 | [Mutual Recursion] |
| 273 | foo=%(bar)s |
| 274 | bar=%(foo)s |
| 275 | """) |
| 276 | parse_errors() |
| 277 | query_errors() |
| 278 | weird_errors() |