Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(78771)

Delta Between Two Patch Sets: Lib/test/test_configparser.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_concurrent_futures.py ('k') | Lib/test/test_copyreg.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import collections 1 import collections
2 import configparser 2 import configparser
3 import io 3 import io
4 import os 4 import os
5 import sys 5 import sys
6 import textwrap 6 import textwrap
7 import unittest 7 import unittest
8 import warnings 8 import warnings
9 9
10 from test import support 10 from test import support
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 with self.assertRaises(configparser.DuplicateSectionError) as cm: 619 with self.assertRaises(configparser.DuplicateSectionError) as cm:
620 cf.read_string(textwrap.dedent("""\ 620 cf.read_string(textwrap.dedent("""\
621 [Foo] 621 [Foo]
622 will this be added{equals}True 622 will this be added{equals}True
623 [Bar] 623 [Bar]
624 what about this{equals}True 624 what about this{equals}True
625 [Foo] 625 [Foo]
626 oops{equals}this won't 626 oops{equals}this won't
627 """.format(equals=self.delimiters[0])), source='<foo-bar>') 627 """.format(equals=self.delimiters[0])), source='<foo-bar>')
628 e = cm.exception 628 e = cm.exception
629 self.assertEqual(str(e), "While reading from <foo-bar> [line 5]: " 629 self.assertEqual(str(e), "While reading from '<foo-bar>' "
630 "section 'Foo' already exists") 630 "[line 5]: section 'Foo' already exists")
631 self.assertEqual(e.args, ("Foo", '<foo-bar>', 5)) 631 self.assertEqual(e.args, ("Foo", '<foo-bar>', 5))
632 632
633 with self.assertRaises(configparser.DuplicateOptionError) as cm: 633 with self.assertRaises(configparser.DuplicateOptionError) as cm:
634 cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}}) 634 cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}})
635 e = cm.exception 635 e = cm.exception
636 self.assertEqual(str(e), "While reading from <dict>: option 'opt' " 636 self.assertEqual(str(e), "While reading from '<dict>': option "
637 "in section 'Bar' already exists") 637 "'opt' in section 'Bar' already exists")
638 self.assertEqual(e.args, ("Bar", "opt", "<dict>", None)) 638 self.assertEqual(e.args, ("Bar", "opt", "<dict>", None))
639 639
640 def test_write(self): 640 def test_write(self):
641 config_string = ( 641 config_string = (
642 "[Long Line]\n" 642 "[Long Line]\n"
643 "foo{0[0]} this line is much, much longer than my editor\n" 643 "foo{0[0]} this line is much, much longer than my editor\n"
644 " likes it.\n" 644 " likes it.\n"
645 "[{default_section}]\n" 645 "[{default_section}]\n"
646 "foo{0[1]} another very\n" 646 "foo{0[1]} another very\n"
647 " long line\n" 647 " long line\n"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 pass 700 pass
701 cf.set("sect", "option1", "splat") 701 cf.set("sect", "option1", "splat")
702 cf.set("sect", "option1", mystr("splat")) 702 cf.set("sect", "option1", mystr("splat"))
703 cf.set("sect", "option2", "splat") 703 cf.set("sect", "option2", "splat")
704 cf.set("sect", "option2", mystr("splat")) 704 cf.set("sect", "option2", mystr("splat"))
705 cf.set("sect", "option1", "splat") 705 cf.set("sect", "option1", "splat")
706 cf.set("sect", "option2", "splat") 706 cf.set("sect", "option2", "splat")
707 707
708 def test_read_returns_file_list(self): 708 def test_read_returns_file_list(self):
709 if self.delimiters[0] != '=': 709 if self.delimiters[0] != '=':
710 # skip reading the file if we're using an incompatible format 710 self.skipTest('incompatible format')
711 return
712 file1 = support.findfile("cfgparser.1") 711 file1 = support.findfile("cfgparser.1")
713 # check when we pass a mix of readable and non-readable files: 712 # check when we pass a mix of readable and non-readable files:
714 cf = self.newconfig() 713 cf = self.newconfig()
715 parsed_files = cf.read([file1, "nonexistent-file"]) 714 parsed_files = cf.read([file1, "nonexistent-file"])
716 self.assertEqual(parsed_files, [file1]) 715 self.assertEqual(parsed_files, [file1])
717 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") 716 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
718 # check when we pass only a filename: 717 # check when we pass only a filename:
719 cf = self.newconfig() 718 cf = self.newconfig()
720 parsed_files = cf.read(file1) 719 parsed_files = cf.read(file1)
721 self.assertEqual(parsed_files, [file1]) 720 self.assertEqual(parsed_files, [file1])
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 def readline_generator(f): 1411 def readline_generator(f):
1413 """As advised in Doc/library/configparser.rst.""" 1412 """As advised in Doc/library/configparser.rst."""
1414 line = f.readline() 1413 line = f.readline()
1415 while line: 1414 while line:
1416 yield line 1415 yield line
1417 line = f.readline() 1416 line = f.readline()
1418 1417
1419 1418
1420 class ReadFileTestCase(unittest.TestCase): 1419 class ReadFileTestCase(unittest.TestCase):
1421 def test_file(self): 1420 def test_file(self):
1422 file_path = support.findfile("cfgparser.1") 1421 file_paths = [support.findfile("cfgparser.1")]
1423 parser = configparser.ConfigParser() 1422 try:
1424 with open(file_path) as f: 1423 file_paths.append(file_paths[0].encode('utf8'))
1425 parser.read_file(f) 1424 except UnicodeEncodeError:
1426 self.assertIn("Foo Bar", parser) 1425 pass # unfortunately we can't test bytes on this path
1427 self.assertIn("foo", parser["Foo Bar"]) 1426 for file_path in file_paths:
1428 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") 1427 parser = configparser.ConfigParser()
1428 with open(file_path) as f:
1429 parser.read_file(f)
1430 self.assertIn("Foo Bar", parser)
1431 self.assertIn("foo", parser["Foo Bar"])
1432 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1429 1433
1430 def test_iterable(self): 1434 def test_iterable(self):
1431 lines = textwrap.dedent(""" 1435 lines = textwrap.dedent("""
1432 [Foo Bar] 1436 [Foo Bar]
1433 foo=newbar""").strip().split('\n') 1437 foo=newbar""").strip().split('\n')
1434 parser = configparser.ConfigParser() 1438 parser = configparser.ConfigParser()
1435 parser.read_file(lines) 1439 parser.read_file(lines)
1436 self.assertIn("Foo Bar", parser) 1440 self.assertIn("Foo Bar", parser)
1437 self.assertIn("foo", parser["Foo Bar"]) 1441 self.assertIn("foo", parser["Foo Bar"])
1438 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") 1442 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1439 1443
1440 def test_readline_generator(self): 1444 def test_readline_generator(self):
1441 """Issue #11670.""" 1445 """Issue #11670."""
1442 parser = configparser.ConfigParser() 1446 parser = configparser.ConfigParser()
1443 with self.assertRaises(TypeError): 1447 with self.assertRaises(TypeError):
1444 parser.read_file(FakeFile()) 1448 parser.read_file(FakeFile())
1445 parser.read_file(readline_generator(FakeFile())) 1449 parser.read_file(readline_generator(FakeFile()))
1446 self.assertIn("Foo Bar", parser) 1450 self.assertIn("Foo Bar", parser)
1447 self.assertIn("foo", parser["Foo Bar"]) 1451 self.assertIn("foo", parser["Foo Bar"])
1448 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") 1452 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1453
1454 def test_source_as_bytes(self):
1455 """Issue #18260."""
1456 lines = textwrap.dedent("""
1457 [badbad]
1458 [badbad]""").strip().split('\n')
1459 parser = configparser.ConfigParser()
1460 with self.assertRaises(configparser.DuplicateSectionError) as dse:
1461 parser.read_file(lines, source=b"badbad")
1462 self.assertEqual(
1463 str(dse.exception),
1464 "While reading from b'badbad' [line 2]: section 'badbad' "
1465 "already exists"
1466 )
1467 lines = textwrap.dedent("""
1468 [badbad]
1469 bad = bad
1470 bad = bad""").strip().split('\n')
1471 parser = configparser.ConfigParser()
1472 with self.assertRaises(configparser.DuplicateOptionError) as dse:
1473 parser.read_file(lines, source=b"badbad")
1474 self.assertEqual(
1475 str(dse.exception),
1476 "While reading from b'badbad' [line 3]: option 'bad' in section "
1477 "'badbad' already exists"
1478 )
1479 lines = textwrap.dedent("""
1480 [badbad]
1481 = bad""").strip().split('\n')
1482 parser = configparser.ConfigParser()
1483 with self.assertRaises(configparser.ParsingError) as dse:
1484 parser.read_file(lines, source=b"badbad")
1485 self.assertEqual(
1486 str(dse.exception),
1487 "Source contains parsing errors: b'badbad'\n\t[line 2]: '= bad'"
1488 )
1489 lines = textwrap.dedent("""
1490 [badbad
1491 bad = bad""").strip().split('\n')
1492 parser = configparser.ConfigParser()
1493 with self.assertRaises(configparser.MissingSectionHeaderError) as dse:
1494 parser.read_file(lines, source=b"badbad")
1495 self.assertEqual(
1496 str(dse.exception),
1497 "File contains no section headers.\nfile: b'badbad', line: 1\n"
1498 "'[badbad'"
1499 )
1449 1500
1450 1501
1451 class CoverageOneHundredTestCase(unittest.TestCase): 1502 class CoverageOneHundredTestCase(unittest.TestCase):
1452 """Covers edge cases in the codebase.""" 1503 """Covers edge cases in the codebase."""
1453 1504
1454 def test_duplicate_option_error(self): 1505 def test_duplicate_option_error(self):
1455 error = configparser.DuplicateOptionError('section', 'option') 1506 error = configparser.DuplicateOptionError('section', 'option')
1456 self.assertEqual(error.section, 'section') 1507 self.assertEqual(error.section, 'section')
1457 self.assertEqual(error.option, 'option') 1508 self.assertEqual(error.option, 'option')
1458 self.assertEqual(error.source, None) 1509 self.assertEqual(error.source, None)
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1708 self.assertEqual(s['k6'], 'v6;still v6; and still v6') 1759 self.assertEqual(s['k6'], 'v6;still v6; and still v6')
1709 self.assertEqual(s['k7'], 'v7;still v7; and still v7') 1760 self.assertEqual(s['k7'], 'v7;still v7; and still v7')
1710 s = cfg['multiprefix'] 1761 s = cfg['multiprefix']
1711 self.assertEqual(s['k1'], 'v1;still v1') 1762 self.assertEqual(s['k1'], 'v1;still v1')
1712 self.assertEqual(s['k2'], 'v2') 1763 self.assertEqual(s['k2'], 'v2')
1713 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3') 1764 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
1714 1765
1715 1766
1716 if __name__ == '__main__': 1767 if __name__ == '__main__':
1717 unittest.main() 1768 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+