55"""
66
77import os
8+ import sys
9+ import imp
810import unittest
911import sysconfig
12+ import tempfile
1013from test import support
1114from test .script_helper import assert_python_ok
1215
1720
1821srcdir = sysconfig .get_config_var ('projectbase' )
1922basepath = os .path .join (os .getcwd (), srcdir , 'Tools' )
23+ scriptsdir = os .path .join (basepath , 'scripts' )
2024
2125
2226class ReindentTests (unittest .TestCase ):
23- script = os .path .join (basepath , 'scripts' , 'reindent.py' )
27+ script = os .path .join (scriptsdir , 'reindent.py' )
2428
2529 def test_noargs (self ):
2630 assert_python_ok (self .script )
@@ -31,8 +35,73 @@ def test_help(self):
3135 self .assertGreater (err , b'' )
3236
3337
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+
68+ @unittest .skipIf (not support .threading , "test requires _thread module" )
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+
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+
34102def test_main ():
35- support .run_unittest (ReindentTests )
103+ support .run_unittest (* [obj for obj in globals ().values ()
104+ if isinstance (obj , type )])
36105
37106
38107if __name__ == '__main__' :
0 commit comments