Debian Codegraph @

tools/test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # WxGeometrie
5 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
6 # Copyright (C) 2005-2013 Nicolas Pourcelot
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 """Utilitaires et tests à effectuer avant de publier une nouvelle version de WxGéométrie.
23
24 La suite de tests suivante est librement inspirée de Sympy.
25
26 python test.py audit -> will run pyflakes checker on source code
27 python test.py bench -> will run the complete benchmark suite
28 python test.py clean -> will clean all trash (*.pyc and stuff)
29 python test.py test -> will run the complete test suite
30 """
31
32 import sys, os
33
34 #from testlib import ROOTDIR, WXGEODIR
35
36 _module_path = os.path.split(os.path.realpath(sys._getframe().f_code.co_filename))[0]
37 ROOTDIR = os.path.abspath(_module_path + '/..') # /.../nom_du_projet/
38 WXGEODIR = ROOTDIR + '/wxgeometrie'
39 SKIP_DIRS = ['sympy/', 'sympy_OLD/', 'sympy_old/', 'pylib/decorator.py', # already tested by sympy team.
40 'modules/OLD', 'BAZAR/']
41
42 sys.path.insert(0, WXGEODIR)
43 sys.path.insert(0, ROOTDIR)
44
45 print(ROOTDIR)
46
47 try:
48 import sip
49 # PyQt new API (PyQt 4.6+)
50 sip.setapi('QDate', 2)
51 sip.setapi('QDateTime', 2)
52 sip.setapi('QString', 2)
53 sip.setapi('QTextStream', 2)
54 sip.setapi('QTime', 2)
55 sip.setapi('QUrl', 2)
56 sip.setapi('QVariant', 2)
57 except ValueError:
58 print("Warning: SIP API has already been set.")
59
60 from wxgeometrie import param
61
62
63 # Make sure I have the right Python version.
64 if sys.version_info[:2] < param.python_version_min:
65 print("Python %d.%d or newer is required. Python %d.%d detected." % \
66 (param.python_min + sys.version_info[:2]))
67 sys.exit(-1)
68
69
70 #TODO: use argparse once python 2.6 is deprecated for WxGéométrie.
71
72 actions = {'u': 'audit', 'c': 'clean', 't': 'test', 'd': 'doc', 'a': 'all', 'h': 'help'}
73
74 args = sys.argv[1:]
75
76
77 def audit():
78 """Audit WxGeometrie's source with PyFlakes.
79
80 Audit WxGeometrie source code for following issues:
81 - Names which are used but not defined or used before they are defined.
82 - Names which are redefined without having been used.
83 """
84 os.chdir(WXGEODIR)
85 try:
86 import pyflakes.scripts.pyflakes as flakes
87 except ImportError:
88 print("""In order to run the audit, you need to have PyFlakes installed.""")
89 sys.exit(-1)
90 print('\n == Auditing files... ==')
91 warns = 0
92 for dirpath, dirnames, filenames in os.walk('.'):
93 if not any((dirpath.startswith('./' + dir) or dir + '/' == dirpath) for dir in SKIP_DIRS):
94 print('\nAuditing ' + dirpath + '...')
95 print([('./' + dir, dirpath.startswith('./' + dir)) for dir in SKIP_DIRS])
96 for filename in filenames:
97 if filename.endswith('.py') and filename != '__init__.py':
98 warns += flakes.checkPath(os.path.join(dirpath, filename))
99 if warns > 0:
100 print("Audit finished with total %d warnings" % warns)
101 else:
102 print("Audit finished without any warning. :)")
103
104
105 def clean():
106 """Cleans *.pyc and debian trashs."""
107 os.chdir(ROOTDIR)
108 os.system("py.cleanup --remove=.py~,.bak,.pyc,.txt~")
109
110
111 def test(*args):
112 "Run all unit tests."
113 os.chdir(ROOTDIR)
114 param.debug = False
115 param.verbose = False
116 from runtests import test
117 test(*args, blacklist = SKIP_DIRS)
118
119
120 def doctest(*args):
121 "Run all doctests."
122 os.chdir(WXGEODIR)
123
124 param.debug = False
125 param.verbose = False
126 sys.argv = [sys.argv[0], '--defaut']
127 from runtests import doctest
128 doctest(*args, blacklist = SKIP_DIRS)
129
130 doc = doctest
131
132 def all(*args):
133 "Run all tests and doctests."
134 test(*args)
135 doctest(*args)
136
137
138 def help():
139 print("""\n === Usage ===\n
140 - Launch all unit tests:
141 $ ./tools/test.py
142 - Launch all doctests:
143 $ ./tools/test.py --doc
144 - Launch all unit tests and doctests:
145 $ ./tools/test.py --all
146 - Clear working wxgeometrie directories:
147 $ ./tools/test.py --clear
148 - Audit code using PyFlakes:
149 $ ./tools/test.py --audit
150
151 Note that you may test only a few modules.
152 For example:
153 - Launch all unit tests concerning geolib:
154 $ ./tools/test.py geolib
155 - Launch all unit tests contained in geolib/test_objets.py
156 $ ./tools/test.py objets
157 """)
158 sys.exit()
159
160
161
162 # Actions are launched
163 if args:
164 if args[0].startswith('--'):
165 action = args[0][2:]
166 args = args[1:]
167 elif args[0].startswith('-'):
168 act = args[0][1:]
169 args = args[1:]
170 if act in actions:
171 action = actions[act]
172 else:
173 help()
174 else:
175 action = 'test'
176 if action in actions.values():
177 locals()[action](*args)
178 else:
179 help()
180 else:
181 test()