blob: 7a25069f96070cbb67219c9f17a4c05c47cd5ee3 [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
Brett Cannonc0499522012-05-11 14:48:41 -04009import importlib.machinery
Éric Araujob4656242012-02-25 16:57:04 +010010import unittest
R David Murrayea169802012-04-11 15:17:37 -040011from unittest import mock
Éric Araujob4656242012-02-25 16:57:04 +010012import sysconfig
R David Murrayd3af6342012-04-05 22:59:13 -040013import tempfile
Éric Araujob4656242012-02-25 16:57:04 +010014from test import support
15from test.script_helper import assert_python_ok
16
17if 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 Pitrou8afc2432012-06-28 01:20:26 +020022basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
23 'Tools')
R David Murray54ac8322012-04-04 21:28:14 -040024scriptsdir = os.path.join(basepath, 'scripts')
Éric Araujob4656242012-02-25 16:57:04 +010025
26
27class ReindentTests(unittest.TestCase):
R David Murray54ac8322012-04-04 21:28:14 -040028 script = os.path.join(scriptsdir, 'reindent.py')
Éric Araujob4656242012-02-25 16:57:04 +010029
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 Murray54ac8322012-04-04 21:28:14 -040039class 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 Murrayea169802012-04-11 15:17:37 -040044 whitelist = ['reindent.py', 'pdeps.py', 'gprof2html']
R David Murray54ac8322012-04-04 21:28:14 -040045 # 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 Murrayca60b362012-04-04 22:37:50 -040069 @unittest.skipIf(not support.threading, "test requires _thread module")
R David Murray54ac8322012-04-04 21:28:14 -040070 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 Murrayd3af6342012-04-05 22:59:13 -040078class PdepsTests(unittest.TestCase):
79
80 @classmethod
81 def setUpClass(self):
82 path = os.path.join(scriptsdir, 'pdeps.py')
Brett Cannonc0499522012-05-11 14:48:41 -040083 loader = importlib.machinery.SourceFileLoader('pdeps', path)
84 self.pdeps = loader.load_module()
R David Murrayd3af6342012-04-05 22:59:13 -040085
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 Murrayea169802012-04-11 15:17:37 -0400104class Gprof2htmlTests(unittest.TestCase):
105
106 def setUp(self):
107 path = os.path.join(scriptsdir, 'gprof2html.py')
Brett Cannonc0499522012-05-11 14:48:41 -0400108 loader = importlib.machinery.SourceFileLoader('gprof2html', path)
109 self.gprof = loader.load_module()
R David Murrayea169802012-04-11 15:17:37 -0400110 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 Dickinson44ceea92012-05-07 10:27:23 +0100127# Run the tests in Tools/parser/test_unparse.py
128with support.DirsOnSysPath(os.path.join(basepath, 'parser')):
Mark Dickinson79575b22012-05-07 22:36:43 +0100129 from test_unparse import UnparseTestCase
Mark Dickinsonbe4fb692012-06-23 09:27:47 +0100130 from test_unparse import DirectoryTestCase
Mark Dickinson44ceea92012-05-07 10:27:23 +0100131
132
Éric Araujob4656242012-02-25 16:57:04 +0100133def test_main():
R David Murray54ac8322012-04-04 21:28:14 -0400134 support.run_unittest(*[obj for obj in globals().values()
135 if isinstance(obj, type)])
Éric Araujob4656242012-02-25 16:57:04 +0100136
137
138if __name__ == '__main__':
139 unittest.main()