|
1 | | -"""Tests for scripts in the Tools directory. |
2 | | -
|
3 | | -This file contains regression tests for some of the scripts found in the |
4 | | -Tools directory of a Python checkout or tarball, such as reindent.py. |
5 | | -""" |
| 1 | +"""Tests for the pindent script in the Tools directory.""" |
6 | 2 |
|
7 | 3 | import os |
8 | 4 | import sys |
9 | | -import importlib._bootstrap |
10 | | -import importlib.machinery |
11 | 5 | import unittest |
12 | | -from unittest import mock |
13 | | -import shutil |
14 | 6 | import subprocess |
15 | | -import sysconfig |
16 | | -import tempfile |
17 | 7 | import textwrap |
18 | 8 | from test import support |
19 | | -from test.script_helper import assert_python_ok, assert_python_failure |
20 | | - |
21 | | -if not sysconfig.is_python_build(): |
22 | | - # XXX some installers do contain the tools, should we detect that |
23 | | - # and run the tests in that case too? |
24 | | - raise unittest.SkipTest('test irrelevant for an installed Python') |
25 | | - |
26 | | -basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), |
27 | | - 'Tools') |
28 | | -scriptsdir = os.path.join(basepath, 'scripts') |
| 9 | +from test.script_helper import assert_python_ok |
29 | 10 |
|
| 11 | +from test.test_tools import scriptsdir, skip_if_missing |
30 | 12 |
|
31 | | -class ReindentTests(unittest.TestCase): |
32 | | - script = os.path.join(scriptsdir, 'reindent.py') |
33 | | - |
34 | | - def test_noargs(self): |
35 | | - assert_python_ok(self.script) |
36 | | - |
37 | | - def test_help(self): |
38 | | - rc, out, err = assert_python_ok(self.script, '-h') |
39 | | - self.assertEqual(out, b'') |
40 | | - self.assertGreater(err, b'') |
| 13 | +skip_if_missing() |
41 | 14 |
|
42 | 15 |
|
43 | 16 | class PindentTests(unittest.TestCase): |
@@ -362,162 +335,5 @@ def test_oneline(self): |
362 | 335 | self.pindent_test(clean, closed) |
363 | 336 |
|
364 | 337 |
|
365 | | -class TestSundryScripts(unittest.TestCase): |
366 | | - # At least make sure the rest don't have syntax errors. When tests are |
367 | | - # added for a script it should be added to the whitelist below. |
368 | | - |
369 | | - # scripts that have independent tests. |
370 | | - whitelist = ['reindent.py', 'pdeps.py', 'gprof2html', 'md5sum.py'] |
371 | | - # scripts that can't be imported without running |
372 | | - blacklist = ['make_ctype.py'] |
373 | | - # scripts that use windows-only modules |
374 | | - windows_only = ['win_add2path.py'] |
375 | | - # blacklisted for other reasons |
376 | | - other = ['analyze_dxp.py'] |
377 | | - |
378 | | - skiplist = blacklist + whitelist + windows_only + other |
379 | | - |
380 | | - def setUp(self): |
381 | | - cm = support.DirsOnSysPath(scriptsdir) |
382 | | - cm.__enter__() |
383 | | - self.addCleanup(cm.__exit__) |
384 | | - |
385 | | - def test_sundry(self): |
386 | | - for fn in os.listdir(scriptsdir): |
387 | | - if fn.endswith('.py') and fn not in self.skiplist: |
388 | | - __import__(fn[:-3]) |
389 | | - |
390 | | - @unittest.skipIf(sys.platform != "win32", "Windows-only test") |
391 | | - def test_sundry_windows(self): |
392 | | - for fn in self.windows_only: |
393 | | - __import__(fn[:-3]) |
394 | | - |
395 | | - @unittest.skipIf(not support.threading, "test requires _thread module") |
396 | | - def test_analyze_dxp_import(self): |
397 | | - if hasattr(sys, 'getdxp'): |
398 | | - import analyze_dxp |
399 | | - else: |
400 | | - with self.assertRaises(RuntimeError): |
401 | | - import analyze_dxp |
402 | | - |
403 | | - |
404 | | -class PdepsTests(unittest.TestCase): |
405 | | - |
406 | | - @classmethod |
407 | | - def setUpClass(self): |
408 | | - path = os.path.join(scriptsdir, 'pdeps.py') |
409 | | - spec = importlib.util.spec_from_file_location('pdeps', path) |
410 | | - self.pdeps = importlib._bootstrap._load(spec) |
411 | | - |
412 | | - @classmethod |
413 | | - def tearDownClass(self): |
414 | | - if 'pdeps' in sys.modules: |
415 | | - del sys.modules['pdeps'] |
416 | | - |
417 | | - def test_process_errors(self): |
418 | | - # Issue #14492: m_import.match(line) can be None. |
419 | | - with tempfile.TemporaryDirectory() as tmpdir: |
420 | | - fn = os.path.join(tmpdir, 'foo') |
421 | | - with open(fn, 'w') as stream: |
422 | | - stream.write("#!/this/will/fail") |
423 | | - self.pdeps.process(fn, {}) |
424 | | - |
425 | | - def test_inverse_attribute_error(self): |
426 | | - # Issue #14492: this used to fail with an AttributeError. |
427 | | - self.pdeps.inverse({'a': []}) |
428 | | - |
429 | | - |
430 | | -class Gprof2htmlTests(unittest.TestCase): |
431 | | - |
432 | | - def setUp(self): |
433 | | - path = os.path.join(scriptsdir, 'gprof2html.py') |
434 | | - spec = importlib.util.spec_from_file_location('gprof2html', path) |
435 | | - self.gprof = importlib._bootstrap._load(spec) |
436 | | - oldargv = sys.argv |
437 | | - def fixup(): |
438 | | - sys.argv = oldargv |
439 | | - self.addCleanup(fixup) |
440 | | - sys.argv = [] |
441 | | - |
442 | | - def test_gprof(self): |
443 | | - # Issue #14508: this used to fail with an NameError. |
444 | | - with mock.patch.object(self.gprof, 'webbrowser') as wmock, \ |
445 | | - tempfile.TemporaryDirectory() as tmpdir: |
446 | | - fn = os.path.join(tmpdir, 'abc') |
447 | | - open(fn, 'w').close() |
448 | | - sys.argv = ['gprof2html', fn] |
449 | | - self.gprof.main() |
450 | | - self.assertTrue(wmock.open.called) |
451 | | - |
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 | | - |
517 | | -# Run the tests in Tools/parser/test_unparse.py |
518 | | -with support.DirsOnSysPath(os.path.join(basepath, 'parser')): |
519 | | - from test_unparse import UnparseTestCase |
520 | | - from test_unparse import DirectoryTestCase |
521 | | - |
522 | 338 | if __name__ == '__main__': |
523 | 339 | unittest.main() |
0 commit comments