|
16 | 16 | import tempfile
|
17 | 17 | import textwrap
|
18 | 18 | from test import support
|
19 |
| -from test.script_helper import assert_python_ok, temp_dir |
| 19 | +from test.script_helper import assert_python_ok, assert_python_failure |
20 | 20 |
|
21 | 21 | if not sysconfig.is_python_build():
|
22 | 22 | # XXX some installers do contain the tools, should we detect that
|
@@ -61,7 +61,7 @@ def lstriplines(self, data):
|
61 | 61 |
|
62 | 62 | def test_selftest(self):
|
63 | 63 | self.maxDiff = None
|
64 |
| - with temp_dir() as directory: |
| 64 | + with support.temp_dir() as directory: |
65 | 65 | data_path = os.path.join(directory, '_test.py')
|
66 | 66 | with open(self.script) as f:
|
67 | 67 | closed = f.read()
|
@@ -367,7 +367,7 @@ class TestSundryScripts(unittest.TestCase):
|
367 | 367 | # added for a script it should be added to the whitelist below.
|
368 | 368 |
|
369 | 369 | # scripts that have independent tests.
|
370 |
| - whitelist = ['reindent.py', 'pdeps.py', 'gprof2html'] |
| 370 | + whitelist = ['reindent.py', 'pdeps.py', 'gprof2html', 'md5sum.py'] |
371 | 371 | # scripts that can't be imported without running
|
372 | 372 | blacklist = ['make_ctype.py']
|
373 | 373 | # scripts that use windows-only modules
|
@@ -450,16 +450,74 @@ def test_gprof(self):
|
450 | 450 | self.assertTrue(wmock.open.called)
|
451 | 451 |
|
452 | 452 |
|
| 453 | +class MD5SumTests(unittest.TestCase): |
| 454 | + |
| 455 | + @classmethod |
| 456 | + def setUpClass(cls): |
| 457 | + cls.script = os.path.join(scriptsdir, 'md5sum.py') |
| 458 | + os.mkdir(support.TESTFN) |
| 459 | + cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder') |
| 460 | + with open(cls.fodder, 'wb') as f: |
| 461 | + f.write(b'md5sum\r\ntest file\r\n') |
| 462 | + cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d' |
| 463 | + cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5' |
| 464 | + |
| 465 | + @classmethod |
| 466 | + def tearDownClass(cls): |
| 467 | + support.rmtree(support.TESTFN) |
| 468 | + |
| 469 | + def test_noargs(self): |
| 470 | + rc, out, err = assert_python_ok(self.script) |
| 471 | + self.assertEqual(rc, 0) |
| 472 | + self.assertTrue( |
| 473 | + out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>')) |
| 474 | + self.assertFalse(err) |
| 475 | + |
| 476 | + def test_checksum_fodder(self): |
| 477 | + rc, out, err = assert_python_ok(self.script, self.fodder) |
| 478 | + self.assertEqual(rc, 0) |
| 479 | + self.assertTrue(out.startswith(self.fodder_md5)) |
| 480 | + for part in self.fodder.split(os.path.sep): |
| 481 | + self.assertIn(part.encode(), out) |
| 482 | + self.assertFalse(err) |
| 483 | + |
| 484 | + def test_dash_l(self): |
| 485 | + rc, out, err = assert_python_ok(self.script, '-l', self.fodder) |
| 486 | + self.assertEqual(rc, 0) |
| 487 | + self.assertIn(self.fodder_md5, out) |
| 488 | + parts = self.fodder.split(os.path.sep) |
| 489 | + self.assertIn(parts[-1].encode(), out) |
| 490 | + self.assertNotIn(parts[-2].encode(), out) |
| 491 | + |
| 492 | + def test_dash_t(self): |
| 493 | + rc, out, err = assert_python_ok(self.script, '-t', self.fodder) |
| 494 | + self.assertEqual(rc, 0) |
| 495 | + self.assertTrue(out.startswith(self.fodder_textmode_md5)) |
| 496 | + self.assertNotIn(self.fodder_md5, out) |
| 497 | + |
| 498 | + def test_dash_s(self): |
| 499 | + rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder) |
| 500 | + self.assertEqual(rc, 0) |
| 501 | + self.assertIn(self.fodder_md5, out) |
| 502 | + |
| 503 | + def test_multiple_files(self): |
| 504 | + rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder) |
| 505 | + self.assertEqual(rc, 0) |
| 506 | + lines = out.splitlines() |
| 507 | + self.assertEqual(len(lines), 2) |
| 508 | + self.assertEqual(*lines) |
| 509 | + |
| 510 | + def test_usage(self): |
| 511 | + rc, out, err = assert_python_failure(self.script, '-h') |
| 512 | + self.assertEqual(rc, 2) |
| 513 | + self.assertEqual(out, b'') |
| 514 | + self.assertGreater(err, b'') |
| 515 | + |
| 516 | + |
453 | 517 | # Run the tests in Tools/parser/test_unparse.py
|
454 | 518 | with support.DirsOnSysPath(os.path.join(basepath, 'parser')):
|
455 | 519 | from test_unparse import UnparseTestCase
|
456 | 520 | from test_unparse import DirectoryTestCase
|
457 | 521 |
|
458 |
| - |
459 |
| -def test_main(): |
460 |
| - support.run_unittest(*[obj for obj in globals().values() |
461 |
| - if isinstance(obj, type)]) |
462 |
| - |
463 |
| - |
464 | 522 | if __name__ == '__main__':
|
465 | 523 | unittest.main()
|
0 commit comments