É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 |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame^] | 9 | import imp |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 10 | import unittest |
| 11 | import sysconfig |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame^] | 12 | import tempfile |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 13 | from test import support |
| 14 | from test.script_helper import assert_python_ok |
| 15 | |
| 16 | if not sysconfig.is_python_build(): |
| 17 | # XXX some installers do contain the tools, should we detect that |
| 18 | # and run the tests in that case too? |
| 19 | raise unittest.SkipTest('test irrelevant for an installed Python') |
| 20 | |
| 21 | srcdir = sysconfig.get_config_var('projectbase') |
| 22 | basepath = os.path.join(os.getcwd(), srcdir, 'Tools') |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 23 | scriptsdir = os.path.join(basepath, 'scripts') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class ReindentTests(unittest.TestCase): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 27 | script = os.path.join(scriptsdir, 'reindent.py') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 28 | |
| 29 | def test_noargs(self): |
| 30 | assert_python_ok(self.script) |
| 31 | |
| 32 | def test_help(self): |
| 33 | rc, out, err = assert_python_ok(self.script, '-h') |
| 34 | self.assertEqual(out, b'') |
| 35 | self.assertGreater(err, b'') |
| 36 | |
| 37 | |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 38 | class TestSundryScripts(unittest.TestCase): |
| 39 | # At least make sure the rest don't have syntax errors. When tests are |
| 40 | # added for a script it should be added to the whitelist below. |
| 41 | |
| 42 | # scripts that have independent tests. |
| 43 | whitelist = ['reindent.py'] |
| 44 | # scripts that can't be imported without running |
| 45 | blacklist = ['make_ctype.py'] |
| 46 | # scripts that use windows-only modules |
| 47 | windows_only = ['win_add2path.py'] |
| 48 | # blacklisted for other reasons |
| 49 | other = ['analyze_dxp.py'] |
| 50 | |
| 51 | skiplist = blacklist + whitelist + windows_only + other |
| 52 | |
| 53 | def setUp(self): |
| 54 | cm = support.DirsOnSysPath(scriptsdir) |
| 55 | cm.__enter__() |
| 56 | self.addCleanup(cm.__exit__) |
| 57 | |
| 58 | def test_sundry(self): |
| 59 | for fn in os.listdir(scriptsdir): |
| 60 | if fn.endswith('.py') and fn not in self.skiplist: |
| 61 | __import__(fn[:-3]) |
| 62 | |
| 63 | @unittest.skipIf(sys.platform != "win32", "Windows-only test") |
| 64 | def test_sundry_windows(self): |
| 65 | for fn in self.windows_only: |
| 66 | __import__(fn[:-3]) |
| 67 | |
R David Murray | ca60b36 | 2012-04-04 22:37:50 -0400 | [diff] [blame] | 68 | @unittest.skipIf(not support.threading, "test requires _thread module") |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 69 | def test_analyze_dxp_import(self): |
| 70 | if hasattr(sys, 'getdxp'): |
| 71 | import analyze_dxp |
| 72 | else: |
| 73 | with self.assertRaises(RuntimeError): |
| 74 | import analyze_dxp |
| 75 | |
| 76 | |
R David Murray | d3af634 | 2012-04-05 22:59:13 -0400 | [diff] [blame^] | 77 | class PdepsTests(unittest.TestCase): |
| 78 | |
| 79 | @classmethod |
| 80 | def setUpClass(self): |
| 81 | path = os.path.join(scriptsdir, 'pdeps.py') |
| 82 | self.pdeps = imp.load_source('pdeps', path) |
| 83 | |
| 84 | @classmethod |
| 85 | def tearDownClass(self): |
| 86 | if 'pdeps' in sys.modules: |
| 87 | del sys.modules['pdeps'] |
| 88 | |
| 89 | def test_process_errors(self): |
| 90 | # Issue #14492: m_import.match(line) can be None. |
| 91 | with tempfile.TemporaryDirectory() as tmpdir: |
| 92 | fn = os.path.join(tmpdir, 'foo') |
| 93 | with open(fn, 'w') as stream: |
| 94 | stream.write("#!/this/will/fail") |
| 95 | self.pdeps.process(fn, {}) |
| 96 | |
| 97 | def test_inverse_attribute_error(self): |
| 98 | # Issue #14492: this used to fail with an AttributeError. |
| 99 | self.pdeps.inverse({'a': []}) |
| 100 | |
| 101 | |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 102 | def test_main(): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame] | 103 | support.run_unittest(*[obj for obj in globals().values() |
| 104 | if isinstance(obj, type)]) |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 105 | |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | unittest.main() |