Add a stub compat/_scon_dbm.py module and copy whichdb.whichdb() to
[scons.git] / src / test_pychecker.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24 from __future__ import generators  ### KEEP FOR COMPATIBILITY FIXERS
25
26 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27
28 """
29 Use pychecker to catch various Python coding errors.
30 """
31
32 import os
33 import os.path
34 import sys
35
36 import TestSCons
37
38 test = TestSCons.TestSCons()
39
40 test.skip_test('Not ready for clean pychecker output; skipping test.\n')
41
42 try:
43     import pychecker
44 except ImportError:
45     pychecker = test.where_is('pychecker')
46     if not pychecker:
47         test.skip_test("Could not find 'pychecker'; skipping test(s).\n")
48     program = pychecker
49     default_arguments = []
50 else:
51     pychecker = os.path.join(os.path.split(pychecker.__file__)[0], 'checker.py')
52     program = sys.executable
53     default_arguments = [pychecker]
54
55 try:
56     cwd = os.environ['SCONS_CWD']
57 except KeyError:
58     src_engine = os.environ['SCONS_LIB_DIR']
59 else:
60     src_engine = os.path.join(cwd, 'build', 'scons-src', 'src', 'engine')
61     if not os.path.exists(src_engine):
62         src_engine = os.path.join(cwd, 'src', 'engine')
63
64 src_engine_ = os.path.join(src_engine, '')
65
66 MANIFEST = os.path.join(src_engine, 'MANIFEST.in')
67 files = open(MANIFEST).read().split()
68
69 files = [f for f in files if f[-3:] == '.py']
70
71 ignore = [
72     'SCons/compat/__init__.py',
73     'SCons/compat/_scons_UserString.py',
74     'SCons/compat/_scons_hashlib.py',
75     'SCons/compat/_scons_itertools.py',
76     'SCons/compat/_scons_optparse.py',
77     'SCons/compat/_scons_sets.py',
78     'SCons/compat/_scons_shlex.py',
79     'SCons/compat/_scons_subprocess.py',
80     'SCons/compat/_scons_textwrap.py',
81     'SCons/compat/builtins.py',
82 ]
83
84 u = {}
85 for file in files:
86     u[file] = 1
87 for file in ignore:
88     try:
89         del u[file]
90     except KeyError:
91         pass
92 files = sorted(u.keys())
93
94 mismatches = []
95
96 default_arguments.extend([
97     '--quiet',
98     '--limit=1000',
99     # Suppress warnings about unused arguments to functions and methods.
100     # We have too many wrapper functions that intentionally only use some
101     # of their arguments.
102     '--no-argsused',
103 ])
104
105 if sys.platform == 'win32':
106     default_arguments.extend([
107         '--blacklist', '"pywintypes,pywintypes.error"',
108     ])
109
110 per_file_arguments = {
111     #'SCons/__init__.py' : [
112     #    '--varlist',
113     #    '"__revision__,__version__,__build__,__buildsys__,__date__,__developer__"',
114     #],
115 }
116
117 pywintypes_warning = "warning: couldn't find real module for class pywintypes.error (module name: pywintypes)\n"
118
119 os.environ['PYTHONPATH'] = src_engine
120
121 for file in files:
122
123     args = (default_arguments + 
124             per_file_arguments.get(file, []) +
125             [os.path.join(src_engine, file)])
126
127     test.run(program=program, arguments=args, status=None, stderr=None)
128
129     stdout = test.stdout()
130     stdout = stdout.replace(src_engine_, '')
131
132     stderr = test.stderr()
133     stderr = stderr.replace(src_engine_, '')
134     stderr = stderr.replace(pywintypes_warning, '')
135
136     if test.status or stdout or stderr:
137         mismatches.append('\n')
138         mismatches.append(' '.join([program] + args) + '\n')
139
140         mismatches.append('STDOUT =====================================\n')
141         mismatches.append(stdout)
142
143         if stderr:
144             mismatches.append('STDERR =====================================\n')
145             mismatches.append(stderr)
146
147 if mismatches:
148     print ''.join(mismatches[1:])
149     test.fail_test()
150
151 test.pass_test()
152
153 # Local Variables:
154 # tab-width:4
155 # indent-tabs-mode:nil
156 # End:
157 # vim: set expandtab tabstop=4 shiftwidth=4: