É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 |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 9 | import unittest |
| 10 | import sysconfig |
| 11 | from test import support |
| 12 | from test.script_helper import assert_python_ok |
| 13 | |
| 14 | if not sysconfig.is_python_build(): |
| 15 | # XXX some installers do contain the tools, should we detect that |
| 16 | # and run the tests in that case too? |
| 17 | raise unittest.SkipTest('test irrelevant for an installed Python') |
| 18 | |
| 19 | srcdir = sysconfig.get_config_var('projectbase') |
| 20 | basepath = os.path.join(os.getcwd(), srcdir, 'Tools') |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame^] | 21 | scriptsdir = os.path.join(basepath, 'scripts') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class ReindentTests(unittest.TestCase): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame^] | 25 | script = os.path.join(scriptsdir, 'reindent.py') |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 26 | |
| 27 | def test_noargs(self): |
| 28 | assert_python_ok(self.script) |
| 29 | |
| 30 | def test_help(self): |
| 31 | rc, out, err = assert_python_ok(self.script, '-h') |
| 32 | self.assertEqual(out, b'') |
| 33 | self.assertGreater(err, b'') |
| 34 | |
| 35 | |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame^] | 36 | class TestSundryScripts(unittest.TestCase): |
| 37 | # At least make sure the rest don't have syntax errors. When tests are |
| 38 | # added for a script it should be added to the whitelist below. |
| 39 | |
| 40 | # scripts that have independent tests. |
| 41 | whitelist = ['reindent.py'] |
| 42 | # scripts that can't be imported without running |
| 43 | blacklist = ['make_ctype.py'] |
| 44 | # scripts that use windows-only modules |
| 45 | windows_only = ['win_add2path.py'] |
| 46 | # blacklisted for other reasons |
| 47 | other = ['analyze_dxp.py'] |
| 48 | |
| 49 | skiplist = blacklist + whitelist + windows_only + other |
| 50 | |
| 51 | def setUp(self): |
| 52 | cm = support.DirsOnSysPath(scriptsdir) |
| 53 | cm.__enter__() |
| 54 | self.addCleanup(cm.__exit__) |
| 55 | |
| 56 | def test_sundry(self): |
| 57 | for fn in os.listdir(scriptsdir): |
| 58 | if fn.endswith('.py') and fn not in self.skiplist: |
| 59 | __import__(fn[:-3]) |
| 60 | |
| 61 | @unittest.skipIf(sys.platform != "win32", "Windows-only test") |
| 62 | def test_sundry_windows(self): |
| 63 | for fn in self.windows_only: |
| 64 | __import__(fn[:-3]) |
| 65 | |
| 66 | def test_analyze_dxp_import(self): |
| 67 | if hasattr(sys, 'getdxp'): |
| 68 | import analyze_dxp |
| 69 | else: |
| 70 | with self.assertRaises(RuntimeError): |
| 71 | import analyze_dxp |
| 72 | |
| 73 | |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 74 | def test_main(): |
R David Murray | 54ac832 | 2012-04-04 21:28:14 -0400 | [diff] [blame^] | 75 | support.run_unittest(*[obj for obj in globals().values() |
| 76 | if isinstance(obj, type)]) |
Éric Araujo | b465624 | 2012-02-25 16:57:04 +0100 | [diff] [blame] | 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | unittest.main() |