blob: cfe13acc2df9bc008c36cf8eae6badea2bbe065a [file] [log] [blame]
Éric Araujob4656242012-02-25 16:57:04 +01001"""Tests for scripts in the Tools directory.
2
3This file contains regression tests for some of the scripts found in the
4Tools directory of a Python checkout or tarball, such as reindent.py.
5"""
6
7import os
R David Murray54ac8322012-04-04 21:28:14 -04008import sys
R David Murrayd3af6342012-04-05 22:59:13 -04009import imp
Éric Araujob4656242012-02-25 16:57:04 +010010import unittest
11import sysconfig
R David Murrayd3af6342012-04-05 22:59:13 -040012import tempfile
Éric Araujob4656242012-02-25 16:57:04 +010013from test import support
14from test.script_helper import assert_python_ok
15
16if 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
21srcdir = sysconfig.get_config_var('projectbase')
22basepath = os.path.join(os.getcwd(), srcdir, 'Tools')
R David Murray54ac8322012-04-04 21:28:14 -040023scriptsdir = os.path.join(basepath, 'scripts')
Éric Araujob4656242012-02-25 16:57:04 +010024
25
26class ReindentTests(unittest.TestCase):
R David Murray54ac8322012-04-04 21:28:14 -040027 script = os.path.join(scriptsdir, 'reindent.py')
Éric Araujob4656242012-02-25 16:57:04 +010028
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 Murray54ac8322012-04-04 21:28:14 -040038class 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 Murrayca60b362012-04-04 22:37:50 -040068 @unittest.skipIf(not support.threading, "test requires _thread module")
R David Murray54ac8322012-04-04 21:28:14 -040069 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 Murrayd3af6342012-04-05 22:59:13 -040077class 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 Araujob4656242012-02-25 16:57:04 +0100102def test_main():
R David Murray54ac8322012-04-04 21:28:14 -0400103 support.run_unittest(*[obj for obj in globals().values()
104 if isinstance(obj, type)])
Éric Araujob4656242012-02-25 16:57:04 +0100105
106
107if __name__ == '__main__':
108 unittest.main()