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