Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 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 | """ |
| 6 | |
| 7 | import os |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 8 | import sys |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 9 | import importlib.machinery |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 10 | import unittest |
R David Murray | ea16980 | 2012-04-11 15:17:37 -0400 | [diff] [blame] | 11 | from unittest import mock |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 12 | import sysconfig |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame] | 13 | import tempfile |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 14 | from test import support |
| 15 | from test.script_helper import assert_python_ok |
| 16 | |
| 17 | if not sysconfig.is_python_build(): |
| 18 | # XXX some installers do contain the tools, should we detect that |
| 19 | # and run the tests in that case too? |
| 20 | raise unittest.SkipTest('test irrelevant for an installed Python') |
| 21 | |
Antoine Pitrou | 8afc243 | 2012-06-28 01:20:26 +0200 | [diff] [blame] | 22 | basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), |
| 23 | 'Tools') |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 24 | scriptsdir = os.path.join(basepath, 'scripts') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class ReindentTests(unittest.TestCase): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 28 | script = os.path.join(scriptsdir, 'reindent.py') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 29 | |
| 30 | def test_noargs(self): |
| 31 | assert_python_ok(self.script) |
| 32 | |
| 33 | def test_help(self): |
| 34 | rc, out, err = assert_python_ok(self.script, '-h') |
| 35 | self.assertEqual(out, b'') |
| 36 | self.assertGreater(err, b'') |
| 37 | |
| 38 | |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 39 | class TestSundryScripts(unittest.TestCase): |
| 40 | # At least make sure the rest don't have syntax errors. When tests are |
| 41 | # added for a script it should be added to the whitelist below. |
| 42 | |
| 43 | # scripts that have independent tests. |
R David Murray | ea16980 | 2012-04-11 15:17:37 -0400 | [diff] [blame] | 44 | whitelist = ['reindent.py', 'pdeps.py', 'gprof2html'] |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 45 | # scripts that can't be imported without running |
| 46 | blacklist = ['make_ctype.py'] |
| 47 | # scripts that use windows-only modules |
| 48 | windows_only = ['win_add2path.py'] |
| 49 | # blacklisted for other reasons |
| 50 | other = ['analyze_dxp.py'] |
| 51 | |
| 52 | skiplist = blacklist + whitelist + windows_only + other |
| 53 | |
| 54 | def setUp(self): |
| 55 | cm = support.DirsOnSysPath(scriptsdir) |
| 56 | cm.__enter__() |
| 57 | self.addCleanup(cm.__exit__) |
| 58 | |
| 59 | def test_sundry(self): |
| 60 | for fn in os.listdir(scriptsdir): |
| 61 | if fn.endswith('.py') and fn not in self.skiplist: |
| 62 | __import__(fn[:-3]) |
| 63 | |
| 64 | @unittest.skipIf(sys.platform != "win32", "Windows-only test") |
| 65 | def test_sundry_windows(self): |
| 66 | for fn in self.windows_only: |
| 67 | __import__(fn[:-3]) |
| 68 | |
R David Murray | ca60b36 | 2012-04-04 22:37:50 -0400 | [diff] [blame] | 69 | @unittest.skipIf(not support.threading, "test requires _thread module") |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 70 | def test_analyze_dxp_import(self): |
| 71 | if hasattr(sys, 'getdxp'): |
| 72 | import analyze_dxp |
| 73 | else: |
| 74 | with self.assertRaises(RuntimeError): |
| 75 | import analyze_dxp |
| 76 | |
| 77 | |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame] | 78 | class PdepsTests(unittest.TestCase): |
| 79 | |
| 80 | @classmethod |
| 81 | def setUpClass(self): |
| 82 | path = os.path.join(scriptsdir, 'pdeps.py') |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 83 | loader = importlib.machinery.SourceFileLoader('pdeps', path) |
| 84 | self.pdeps = loader.load_module() |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame] | 85 | |
| 86 | @classmethod |
| 87 | def tearDownClass(self): |
| 88 | if 'pdeps' in sys.modules: |
| 89 | del sys.modules['pdeps'] |
| 90 | |
| 91 | def test_process_errors(self): |
| 92 | # Issue #14492: m_import.match(line) can be None. |
| 93 | with tempfile.TemporaryDirectory() as tmpdir: |
| 94 | fn = os.path.join(tmpdir, 'foo') |
| 95 | with open(fn, 'w') as stream: |
| 96 | stream.write("#!/this/will/fail") |
| 97 | self.pdeps.process(fn, {}) |
| 98 | |
| 99 | def test_inverse_attribute_error(self): |
| 100 | # Issue #14492: this used to fail with an AttributeError. |
| 101 | self.pdeps.inverse({'a': []}) |
| 102 | |
| 103 | |
R David Murray | ea16980 | 2012-04-11 15:17:37 -0400 | [diff] [blame] | 104 | class Gprof2htmlTests(unittest.TestCase): |
| 105 | |
| 106 | def setUp(self): |
| 107 | path = os.path.join(scriptsdir, 'gprof2html.py') |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 108 | loader = importlib.machinery.SourceFileLoader('gprof2html', path) |
| 109 | self.gprof = loader.load_module() |
R David Murray | ea16980 | 2012-04-11 15:17:37 -0400 | [diff] [blame] | 110 | oldargv = sys.argv |
| 111 | def fixup(): |
| 112 | sys.argv = oldargv |
| 113 | self.addCleanup(fixup) |
| 114 | sys.argv = [] |
| 115 | |
| 116 | def test_gprof(self): |
| 117 | # Issue #14508: this used to fail with an NameError. |
| 118 | with mock.patch.object(self.gprof, 'webbrowser') as wmock, \ |
| 119 | tempfile.TemporaryDirectory() as tmpdir: |
| 120 | fn = os.path.join(tmpdir, 'abc') |
| 121 | open(fn, 'w').close() |
| 122 | sys.argv = ['gprof2html', fn] |
| 123 | self.gprof.main() |
| 124 | self.assertTrue(wmock.open.called) |
| 125 | |
| 126 | |
Mark Dickinson | 44ceea9 | 2012-05-07 10:27:23 +0100 | [diff] [blame] | 127 | # Run the tests in Tools/parser/test_unparse.py |
| 128 | with support.DirsOnSysPath(os.path.join(basepath, 'parser')): |
Mark Dickinson | 79575b2 | 2012-05-07 22:36:43 +0100 | [diff] [blame] | 129 | from test_unparse import UnparseTestCase |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 130 | from test_unparse import DirectoryTestCase |
Mark Dickinson | 44ceea9 | 2012-05-07 10:27:23 +0100 | [diff] [blame] | 131 | |
| 132 | |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 133 | def test_main(): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 134 | support.run_unittest(*[obj for obj in globals().values() |
| 135 | if isinstance(obj, type)]) |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 136 | |
| 137 | |
| 138 | if __name__ == '__main__': |
| 139 | unittest.main() |