http://scons.tigris.org/issues/show_bug.cgi?id=2329
[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_sets15.py',
79     'SCons/compat/_scons_shlex.py',
80     'SCons/compat/_scons_subprocess.py',
81     'SCons/compat/_scons_textwrap.py',
82     'SCons/compat/builtins.py',
83 ]
84
85 u = {}
86 for file in files:
87     u[file] = 1
88 for file in ignore:
89     try:
90         del u[file]
91     except KeyError:
92         pass
93 files = sorted(u.keys())
94
95 mismatches = []
96
97 default_arguments.extend([
98     '--quiet',
99     '--limit=1000',
100     # Suppress warnings about unused arguments to functions and methods.
101     # We have too many wrapper functions that intentionally only use some
102     # of their arguments.
103     '--no-argsused',
104 ])
105
106 if sys.platform == 'win32':
107     default_arguments.extend([
108         '--blacklist', '"pywintypes,pywintypes.error"',
109     ])
110
111 per_file_arguments = {
112     #'SCons/__init__.py' : [
113     #    '--varlist',
114     #    '"__revision__,__version__,__build__,__buildsys__,__date__,__developer__"',
115     #],
116 }
117
118 pywintypes_warning = "warning: couldn't find real module for class pywintypes.error (module name: pywintypes)\n"
119
120 os.environ['PYTHONPATH'] = src_engine
121
122 for file in files:
123
124     args = (default_arguments + 
125             per_file_arguments.get(file, []) +
126             [os.path.join(src_engine, file)])
127
128     test.run(program=program, arguments=args, status=None, stderr=None)
129
130     stdout = test.stdout()
131     stdout = stdout.replace(src_engine_, '')
132
133     stderr = test.stderr()
134     stderr = stderr.replace(src_engine_, '')
135     stderr = stderr.replace(pywintypes_warning, '')
136
137     if test.status or stdout or stderr:
138         mismatches.append('\n')
139         mismatches.append(' '.join([program] + args) + '\n')
140
141         mismatches.append('STDOUT =====================================\n')
142         mismatches.append(stdout)
143
144         if stderr:
145             mismatches.append('STDERR =====================================\n')
146             mismatches.append(stderr)
147
148 if mismatches:
149     print ''.join(mismatches[1:])
150     test.fail_test()
151
152 test.pass_test()
153
154 # Local Variables:
155 # tab-width:4
156 # indent-tabs-mode:nil
157 # End:
158 # vim: set expandtab tabstop=4 shiftwidth=4: