blob: 8ccabfbcfd8f5757c90070b7b9dd2e151191c722 [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
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
22srcdir = sysconfig.get_config_var('projectbase')
23basepath = os.path.join(os.getcwd(), srcdir, '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')
83 self.pdeps = imp.load_source('pdeps', path)
84
85 @classmethod
86 def tearDownClass(self):
87 if 'pdeps' in sys.modules:
88 del sys.modules['pdeps']
89
90 def test_process_errors(self):
91 # Issue #14492: m_import.match(line) can be None.
92 with tempfile.TemporaryDirectory() as tmpdir:
93 fn = os.path.join(tmpdir, 'foo')
94 with open(fn, 'w') as stream:
95 stream.write("#!/this/will/fail")
96 self.pdeps.process(fn, {})
97
98 def test_inverse_attribute_error(self):
99 # Issue #14492: this used to fail with an AttributeError.
100 self.pdeps.inverse({'a': []})
101
102
R David Murrayea169802012-04-11 15:17:37 -0400103class Gprof2htmlTests(unittest.TestCase):
104
105 def setUp(self):
106 path = os.path.join(scriptsdir, 'gprof2html.py')
107 self.gprof = imp.load_source('gprof2html', path)
108 oldargv = sys.argv
109 def fixup():
110 sys.argv = oldargv
111 self.addCleanup(fixup)
112 sys.argv = []
113
114 def test_gprof(self):
115 # Issue #14508: this used to fail with an NameError.
116 with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
117 tempfile.TemporaryDirectory() as tmpdir:
118 fn = os.path.join(tmpdir, 'abc')
119 open(fn, 'w').close()
120 sys.argv = ['gprof2html', fn]
121 self.gprof.main()
122 self.assertTrue(wmock.open.called)
123
124
Éric Araujob4656242012-02-25 16:57:04 +0100125def test_main():
R David Murray54ac8322012-04-04 21:28:14 -0400126 support.run_unittest(*[obj for obj in globals().values()
127 if isinstance(obj, type)])
Éric Araujob4656242012-02-25 16:57:04 +0100128
129
130if __name__ == '__main__':
131 unittest.main()