root/mail-onna-stick/trunk/lib/mostest.py

Revision 327, 5.2 kB (checked in by t, 10 years ago)

updated svn:ignores to ignore .pyc files;
fixed a variety of tests by tweaking configuration settings.

  • Property svn:executable set to *
Line 
1 #! /usr/bin/env python
2 """
3 Test collector for setup.py that wraps nose.collector with a code coverage
4 analyzer.
5
6 In particular, make sure that the stats are fresh with each run of the
7 unit tests, so that outdated statistics don't get reported.
8
9 If run from the command line, reports unit test coverage & annotates
10 core MOS code with coverage information.
11 """
12
13 DEBUG_AVOID_SLOW=True               # avoid doing really slow things.
14
15 import os, sys
16 from cStringIO import StringIO
17
18 import coverage_lib
19 import nose
20
21 __all__ = ['collector', 'report', 'annotate', 'stop_coverage']
22
23 thisdir = os.path.dirname(__file__)
24 DEFAULT_COVERAGE_FILE='.coverage-unit'
25
26 ###
27 #
28
29 # this is called on import by mos.__init__; it checks to see if coverage
30 # recording is on from before mos import, i.e. before anything is executed.
31 #
32 # coverage_on_start is set to either True or False; it should never be
33 # None except during initial import.
34
35 coverage_on_start = None
36 def check_if_coverage():
37     global coverage_on_start
38    
39     if coverage_on_start is None:
40         coverage_lib.initialize()
41         if coverage_lib.the_coverage.nesting > 0:
42             coverage_on_start = True
43         else:
44             coverage_on_start = False
45
46 check_if_coverage()
47
48 def collector(where_to_look=None, record_coverage=False, avoid_slow=None):
49     """
50     Wrap the nose.collector function with (optional) code coverage.
51
52     Note that avoid_slow can be True, False, or None.  If None, it's left
53     at the default value; if True or False, it's set for the unit tests.
54     """
55     global DEBUG_AVOID_SLOW
56     if avoid_slow is not None:
57         DEBUG_AVOID_SLOW=avoid_slow
58    
59     if record_coverage:
60         coverage_lib.initialize()
61         coverage_lib.the_coverage.start()
62         coverage_lib.the_coverage.erase()
63
64     ###
65
66     if DEBUG_AVOID_SLOW:
67         sys.stderr.write('\n')
68         sys.stderr.write('*** WARNING: DEBUG_AVOID_SLOW is set. ***\n')
69         sys.stderr.write('***     Not all tests will be run.    ***\n')
70         sys.stderr.write('*** WARNING: DEBUG_AVOID_SLOW is set. ***\n')
71         sys.stderr.write('\n')
72         sys.stderr.write("Use 'run-timed-unit-tests' to run all tests.\n")
73         sys.stderr.write('\n')
74
75     ###
76
77     cwd = os.getcwd()
78     sys.path.insert(0, cwd)
79    
80     if where_to_look is None:
81         where_to_look = cwd
82     else:
83         where_to_look = os.path.abspath(where_to_look)
84
85     conf = nose.core.configure(env=os.environ)
86     conf.where = [where_to_look]
87    
88     return nose.TestCollector(conf)
89
90 def stop_coverage():
91     """
92     Stop coverage recording.
93     """
94     coverage_lib.the_coverage.stop()
95
96     filename = _make_coverage_filename()
97     fp = open(filename, 'wb')
98     try:
99         coverage_lib.the_coverage.save(fp)
100     finally:
101         fp.close()
102
103 def report():
104     """
105     Report on the code coverage; return a formatted string.
106     """
107    
108     files_list = _get_package_files()
109    
110     # initialize coverage module
111     coverage_lib.initialize()
112     coverage_lib.the_coverage.get_ready()
113
114     # report!
115     fp = StringIO()
116     coverage_lib.the_coverage.report(files_list, file=fp, show_missing=False)
117     return fp.getvalue()
118
119 def annotate():
120     """
121     Annotate the core code files.
122     """
123     coverage_lib.the_coverage.get_ready()
124
125     files_list = _get_package_files()
126     coverage_lib.the_coverage.annotate(files_list)
127
128 ###
129
130 def _make_coverage_filename(basefile=None):
131     if basefile is None:
132         basefile = DEFAULT_COVERAGE_FILE
133     filename = os.path.join(thisdir, '..')
134     filename = os.path.join(filename, basefile)
135    
136     return filename
137
138 def _get_package_files():
139     """
140     Collect all python files under the given packages.
141     """
142
143     packages_to_report = ['mailsearch', 'mos', 'mosweb', 'misc']
144     thisdir = os.path.dirname(__file__)
145
146     def correct_filename(filename):     # CTB a bit of a hack.
147         filename = 'lib' + filename[len(thisdir):]
148         return filename
149
150     files_list = []
151     for subdir in packages_to_report:
152         path = os.path.join(thisdir, subdir)
153
154         for (dirpath, dirnames, filenames) in os.walk(path):
155             # get rid of subversion files.
156             if '/.svn' in dirpath:
157                 continue
158
159             for filename in filenames:
160                 # only take Python source files.
161                 if filename.endswith('.py'):
162                     filename = os.path.join(dirpath, filename)
163                     filename = correct_filename(filename)
164                     files_list.append(filename)
165
166     return files_list
167
168 ###
169
170 def filter_files():
171     files_list = _get_package_files()
172     coverage_lib.the_coverage.filter_on_filenames(files_list)
173
174 # report coverage, if run from the command line.
175 if __name__ == '__main__':
176     if len(sys.argv) == 2:
177         coverage_file = _make_coverage_filename(sys.argv[1])
178     else:
179         coverage_file = _make_coverage_filename()
180
181     coverage_lib.initialize()
182
183     try:
184         fp = open(coverage_file, 'rb')
185     except IOError:
186         print 'NO COVERAGE FILE!',
187         print '(cannot open %s)' % (coverage_file,)
188     else:
189         coverage_lib.the_coverage.restore(fp)
190
191         print report()
192
193         print '(...annotating source files...)'
194         annotate()
195
196 #    filter_files()
197 #    fp = open(coverage_file, 'wb')
198 #    coverage_lib.the_coverage.save(fp)
199 #    fp.close()
Note: See TracBrowser for help on using the browser.