1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2008 Matt Good <matt@matt-good.net> |
---|
5 | # Copyright (C) 2008-2010 Edgewall Software |
---|
6 | # All rights reserved. |
---|
7 | # |
---|
8 | # This software is licensed as described in the file COPYING, which |
---|
9 | # you should have received as part of this distribution. The terms |
---|
10 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
11 | |
---|
12 | import os |
---|
13 | import cPickle as pickle |
---|
14 | import shutil |
---|
15 | import tempfile |
---|
16 | import unittest |
---|
17 | |
---|
18 | from bitten.build import pythontools |
---|
19 | from bitten.build import FileSet |
---|
20 | from bitten.recipe import Context, Recipe |
---|
21 | |
---|
22 | |
---|
23 | class CoverageTestCase(unittest.TestCase): |
---|
24 | |
---|
25 | def setUp(self): |
---|
26 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
27 | self.ctxt = Context(self.basedir) |
---|
28 | self.summary = open(os.path.join(self.basedir, 'test-coverage.txt'), |
---|
29 | 'w') |
---|
30 | self.coverdir = os.path.join(self.basedir, 'coverage') |
---|
31 | os.mkdir(self.coverdir) |
---|
32 | |
---|
33 | def tearDown(self): |
---|
34 | shutil.rmtree(self.basedir) |
---|
35 | |
---|
36 | def _create_file(self, *path): |
---|
37 | filename = os.path.join(self.basedir, *path) |
---|
38 | dirname = os.path.dirname(filename) |
---|
39 | os.makedirs(dirname) |
---|
40 | fd = file(filename, 'w') |
---|
41 | fd.close() |
---|
42 | return filename[len(self.basedir) + 1:] |
---|
43 | |
---|
44 | def test_missing_param_summary(self): |
---|
45 | self.summary.close() |
---|
46 | self.assertRaises(AssertionError, pythontools.coverage, self.ctxt, |
---|
47 | coverdir=self.coverdir) |
---|
48 | |
---|
49 | def test_empty_summary(self): |
---|
50 | self.summary.write(""" |
---|
51 | Name Stmts Exec Cover Missing |
---|
52 | ------------------------------------------- |
---|
53 | """) |
---|
54 | self.summary.close() |
---|
55 | pythontools.coverage(self.ctxt, summary=self.summary.name, include='*.py') |
---|
56 | type, category, generator, xml = self.ctxt.output.pop() |
---|
57 | self.assertEqual(Recipe.REPORT, type) |
---|
58 | self.assertEqual('coverage', category) |
---|
59 | self.assertEqual(0, len(xml.children)) |
---|
60 | |
---|
61 | def test_summary_with_absolute_path(self): |
---|
62 | self.summary.write(""" |
---|
63 | Name Stmts Exec Cover Missing |
---|
64 | ------------------------------------------- |
---|
65 | test.module 60 60 100%% %s/test/module.py |
---|
66 | """ % self.ctxt.basedir) |
---|
67 | self.summary.close() |
---|
68 | self._create_file('test', 'module.py') |
---|
69 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
70 | include='test/*') |
---|
71 | type, category, generator, xml = self.ctxt.output.pop() |
---|
72 | self.assertEqual(Recipe.REPORT, type) |
---|
73 | self.assertEqual('coverage', category) |
---|
74 | self.assertEqual(1, len(xml.children)) |
---|
75 | child = xml.children[0] |
---|
76 | self.assertEqual('coverage', child.name) |
---|
77 | self.assertEqual('test.module', child.attr['name']) |
---|
78 | self.assertEqual('test/module.py', child.attr['file']) |
---|
79 | self.assertEqual(100, child.attr['percentage']) |
---|
80 | self.assertEqual(60, child.attr['lines']) |
---|
81 | |
---|
82 | def test_summary_with_relative_path(self): |
---|
83 | self.summary.write(""" |
---|
84 | Name Stmts Exec Cover Missing |
---|
85 | ------------------------------------------- |
---|
86 | test.module 60 60 100% ./test/module.py |
---|
87 | """) |
---|
88 | self.summary.close() |
---|
89 | self._create_file('test', 'module.py') |
---|
90 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
91 | include='test/*') |
---|
92 | type, category, generator, xml = self.ctxt.output.pop() |
---|
93 | self.assertEqual(Recipe.REPORT, type) |
---|
94 | self.assertEqual('coverage', category) |
---|
95 | self.assertEqual(1, len(xml.children)) |
---|
96 | child = xml.children[0] |
---|
97 | self.assertEqual('coverage', child.name) |
---|
98 | self.assertEqual('test.module', child.attr['name']) |
---|
99 | self.assertEqual('test/module.py', child.attr['file']) |
---|
100 | self.assertEqual(100, child.attr['percentage']) |
---|
101 | self.assertEqual(60, child.attr['lines']) |
---|
102 | |
---|
103 | def test_summary_with_missing_lines(self): |
---|
104 | self.summary.write(""" |
---|
105 | Name Stmts Exec Cover Missing |
---|
106 | ------------------------------------------- |
---|
107 | test.module 28 26 92% 13-14 ./test/module.py |
---|
108 | """) |
---|
109 | self.summary.close() |
---|
110 | self._create_file('test', 'module.py') |
---|
111 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
112 | include='test/*') |
---|
113 | type, category, generator, xml = self.ctxt.output.pop() |
---|
114 | self.assertEqual(Recipe.REPORT, type) |
---|
115 | self.assertEqual('coverage', category) |
---|
116 | self.assertEqual(1, len(xml.children)) |
---|
117 | child = xml.children[0] |
---|
118 | self.assertEqual('coverage', child.name) |
---|
119 | self.assertEqual('test.module', child.attr['name']) |
---|
120 | self.assertEqual('test/module.py', child.attr['file']) |
---|
121 | self.assertEqual(92, child.attr['percentage']) |
---|
122 | self.assertEqual(28, child.attr['lines']) |
---|
123 | |
---|
124 | def test_line_hits_with_absolute_path(self): |
---|
125 | self.summary.write(""" |
---|
126 | Name Stmts Exec Cover Missing |
---|
127 | ------------------------------------------- |
---|
128 | test.module 60 60 100%% %s/test/module.py |
---|
129 | """ % self.ctxt.basedir) |
---|
130 | self.summary.close() |
---|
131 | coverfile = open(os.path.join(self.coverdir, |
---|
132 | 'test_module.py,cover'), 'w') |
---|
133 | coverfile.write("""> import sys |
---|
134 | |
---|
135 | > guido = True |
---|
136 | > if guido: |
---|
137 | > print "Hello world!" |
---|
138 | ! else: |
---|
139 | ! print "Goodbye world!" |
---|
140 | |
---|
141 | > sys.exit() |
---|
142 | ! print "What?" |
---|
143 | |
---|
144 | """) |
---|
145 | coverfile.close() |
---|
146 | self._create_file('test', 'module.py') |
---|
147 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
148 | include='test/*', coverdir='coverage') |
---|
149 | type, category, generator, xml = self.ctxt.output.pop() |
---|
150 | self.assertEqual(Recipe.REPORT, type) |
---|
151 | self.assertEqual('coverage', category) |
---|
152 | self.assertEqual(1, len(xml.children)) |
---|
153 | child = xml.children[0].children[0] |
---|
154 | self.assertEqual('line_hits', child.name) |
---|
155 | self.assertEqual('1 - 1 1 1 0 0 - 1 0 -', child.children[0]) |
---|
156 | |
---|
157 | def test_line_hits_with_relative_path(self): |
---|
158 | self.summary.write(""" |
---|
159 | Name Stmts Exec Cover Missing |
---|
160 | ------------------------------------------- |
---|
161 | test.module 60 60 100% ./test/module.py |
---|
162 | """) |
---|
163 | self.summary.close() |
---|
164 | coverfile = open(os.path.join(self.coverdir, |
---|
165 | 'test_module.py,cover'), 'w') |
---|
166 | coverfile.write("""> import sys |
---|
167 | |
---|
168 | > guido = True |
---|
169 | > if guido: |
---|
170 | > print "Hello world!" |
---|
171 | ! else: |
---|
172 | ! print "Goodbye world!" |
---|
173 | |
---|
174 | > sys.exit() |
---|
175 | ! print "What?" |
---|
176 | |
---|
177 | """) |
---|
178 | coverfile.close() |
---|
179 | self._create_file('test', 'module.py') |
---|
180 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
181 | include='test/*', coverdir='coverage') |
---|
182 | type, category, generator, xml = self.ctxt.output.pop() |
---|
183 | self.assertEqual(Recipe.REPORT, type) |
---|
184 | self.assertEqual('coverage', category) |
---|
185 | self.assertEqual(1, len(xml.children)) |
---|
186 | child = xml.children[0].children[0] |
---|
187 | self.assertEqual('line_hits', child.name) |
---|
188 | self.assertEqual('1 - 1 1 1 0 0 - 1 0 -', child.children[0]) |
---|
189 | |
---|
190 | def test_line_hits_with_missing_file(self): |
---|
191 | self.summary.write(""" |
---|
192 | Name Stmts Exec Cover Missing |
---|
193 | ------------------------------------------- |
---|
194 | test.module2 60 60 100%% %s/test/module2.py |
---|
195 | """ % self.ctxt.basedir) |
---|
196 | self.summary.close() |
---|
197 | self._create_file('test', 'module2.py') |
---|
198 | |
---|
199 | pythontools.coverage(self.ctxt, summary=self.summary.name, |
---|
200 | include='test/*', coverdir='coverage') |
---|
201 | type, category, generator, xml = self.ctxt.output.pop() |
---|
202 | self.assertEqual(Recipe.REPORT, type) |
---|
203 | self.assertEqual('coverage', category) |
---|
204 | self.assertEqual(1, len(xml.children)) |
---|
205 | self.assertEqual(0, len(xml.children[0].children)) |
---|
206 | |
---|
207 | class TraceTestCase(unittest.TestCase): |
---|
208 | |
---|
209 | def setUp(self): |
---|
210 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
211 | self.ctxt = Context(self.basedir) |
---|
212 | self.summary = open(os.path.join(self.basedir, 'test-coverage.txt'), |
---|
213 | 'w') |
---|
214 | self.coverdir = os.path.join(self.basedir, 'coverage') |
---|
215 | os.mkdir(self.coverdir) |
---|
216 | |
---|
217 | def tearDown(self): |
---|
218 | shutil.rmtree(self.basedir) |
---|
219 | |
---|
220 | def _create_file(self, *path): |
---|
221 | filename = os.path.join(self.basedir, *path) |
---|
222 | dirname = os.path.dirname(filename) |
---|
223 | os.makedirs(dirname) |
---|
224 | fd = file(filename, 'w') |
---|
225 | fd.close() |
---|
226 | return filename[len(self.basedir) + 1:] |
---|
227 | |
---|
228 | def test_missing_param_summary(self): |
---|
229 | self.summary.close() |
---|
230 | self.assertRaises(AssertionError, pythontools.trace, self.ctxt, |
---|
231 | coverdir='coverage') |
---|
232 | |
---|
233 | def test_missing_param_coverdir(self): |
---|
234 | self.summary.close() |
---|
235 | self.assertRaises(AssertionError, pythontools.trace, self.ctxt, |
---|
236 | summary='test-coverage.txt') |
---|
237 | |
---|
238 | def test_empty_summary(self): |
---|
239 | self.summary.write('line cov% module (path)') |
---|
240 | self.summary.close() |
---|
241 | pythontools.trace(self.ctxt, summary=self.summary.name, include='*.py', |
---|
242 | coverdir=self.coverdir) |
---|
243 | type, category, generator, xml = self.ctxt.output.pop() |
---|
244 | self.assertEqual(Recipe.REPORT, type) |
---|
245 | self.assertEqual('coverage', category) |
---|
246 | self.assertEqual(0, len(xml.children)) |
---|
247 | |
---|
248 | def test_summary_with_absolute_path(self): |
---|
249 | self.summary.write(""" |
---|
250 | lines cov%% module (path) |
---|
251 | 60 100%% test.module (%s/test/module.py) |
---|
252 | """ % self.ctxt.basedir) |
---|
253 | self.summary.close() |
---|
254 | self._create_file('test', 'module.py') |
---|
255 | pythontools.trace(self.ctxt, summary=self.summary.name, |
---|
256 | include='test/*', coverdir=self.coverdir) |
---|
257 | type, category, generator, xml = self.ctxt.output.pop() |
---|
258 | self.assertEqual(Recipe.REPORT, type) |
---|
259 | self.assertEqual('coverage', category) |
---|
260 | self.assertEqual(1, len(xml.children)) |
---|
261 | child = xml.children[0] |
---|
262 | self.assertEqual('coverage', child.name) |
---|
263 | self.assertEqual('test.module', child.attr['name']) |
---|
264 | self.assertEqual('test/module.py', child.attr['file']) |
---|
265 | |
---|
266 | def test_summary_with_relative_path(self): |
---|
267 | self.summary.write(""" |
---|
268 | lines cov% module (path) |
---|
269 | 60 100% test.module (./test/module.py) |
---|
270 | """) |
---|
271 | self.summary.close() |
---|
272 | self._create_file('test', 'module.py') |
---|
273 | pythontools.trace(self.ctxt, summary=self.summary.name, |
---|
274 | include='test/*', coverdir=self.coverdir) |
---|
275 | type, category, generator, xml = self.ctxt.output.pop() |
---|
276 | self.assertEqual(Recipe.REPORT, type) |
---|
277 | self.assertEqual('coverage', category) |
---|
278 | self.assertEqual(1, len(xml.children)) |
---|
279 | child = xml.children[0] |
---|
280 | self.assertEqual('coverage', child.name) |
---|
281 | self.assertEqual('test.module', child.attr['name']) |
---|
282 | self.assertEqual('test/module.py', child.attr['file']) |
---|
283 | |
---|
284 | |
---|
285 | class PyLintTestCase(unittest.TestCase): |
---|
286 | |
---|
287 | def setUp(self): |
---|
288 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
289 | self.ctxt = Context(self.basedir) |
---|
290 | self.summary = open(os.path.join(self.basedir, '.lint'), 'w') |
---|
291 | |
---|
292 | def tearDown(self): |
---|
293 | shutil.rmtree(self.basedir) |
---|
294 | |
---|
295 | def test_summary_format(self): |
---|
296 | # thoroughly check on report line |
---|
297 | self.summary.write(""" |
---|
298 | %s/module/file1.py:42: [C] Missing docstring |
---|
299 | """ % (self.ctxt.basedir,)) |
---|
300 | self.summary.close() |
---|
301 | pythontools.pylint(self.ctxt, file_=self.summary.name) |
---|
302 | type, category, generator, xml = self.ctxt.output.pop() |
---|
303 | self.assertEqual(Recipe.REPORT, type) |
---|
304 | self.assertEqual('lint', category) |
---|
305 | self.assertEqual(1, len(xml.children)) |
---|
306 | child = xml.children[0] |
---|
307 | self.assertEqual('problem', child.name) |
---|
308 | self.assertEqual('module/file1.py', child.attr['file']) |
---|
309 | self.assertEqual(1, len(child.children)) |
---|
310 | msg = child.children[0] |
---|
311 | self.assertEqual('msg', msg.name) |
---|
312 | self.assertEqual(1, len(msg.children)) |
---|
313 | self.assertEqual('Missing docstring', msg.children[0]) |
---|
314 | |
---|
315 | def test_summary_with_absolute_path(self): |
---|
316 | # One posix + one windows path to normalize |
---|
317 | self.summary.write(""" |
---|
318 | %s/module/file1.py:42: [C] Missing docstring |
---|
319 | %s\\module\\file2.py:42: [C] Missing docstring |
---|
320 | """ % (self.ctxt.basedir, self.ctxt.basedir)) |
---|
321 | self.summary.close() |
---|
322 | pythontools.pylint(self.ctxt, file_=self.summary.name) |
---|
323 | type, category, generator, xml = self.ctxt.output.pop() |
---|
324 | self.assertEqual(Recipe.REPORT, type) |
---|
325 | self.assertEqual('lint', category) |
---|
326 | self.assertEqual(2, len(xml.children)) |
---|
327 | child = xml.children[0] |
---|
328 | self.assertEqual('problem', child.name) |
---|
329 | self.assertEqual('module/file1.py', child.attr['file']) |
---|
330 | child = xml.children[1] |
---|
331 | self.assertEqual('problem', child.name) |
---|
332 | self.assertEqual('module/file2.py', child.attr['file']) |
---|
333 | |
---|
334 | def test_summary_with_relative_path(self): |
---|
335 | # One posix + one windows path to normalize |
---|
336 | self.summary.write(""" |
---|
337 | module/file1.py:42: [C] Missing docstring |
---|
338 | module\\file2.py:42: [C] Missing docstring |
---|
339 | """) |
---|
340 | self.summary.close() |
---|
341 | pythontools.pylint(self.ctxt, file_=self.summary.name) |
---|
342 | type, category, generator, xml = self.ctxt.output.pop() |
---|
343 | self.assertEqual(Recipe.REPORT, type) |
---|
344 | self.assertEqual('lint', category) |
---|
345 | self.assertEqual(2, len(xml.children)) |
---|
346 | child = xml.children[0] |
---|
347 | self.assertEqual('problem', child.name) |
---|
348 | self.assertEqual('module/file1.py', child.attr['file']) |
---|
349 | child = xml.children[1] |
---|
350 | self.assertEqual('problem', child.name) |
---|
351 | self.assertEqual('module/file2.py', child.attr['file']) |
---|
352 | |
---|
353 | |
---|
354 | class FigleafTestCase(unittest.TestCase): |
---|
355 | |
---|
356 | def setUp(self): |
---|
357 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
358 | self.ctxt = Context(self.basedir) |
---|
359 | self.summary = open(os.path.join(self.basedir, '.figleaf'), 'w') |
---|
360 | |
---|
361 | def tearDown(self): |
---|
362 | shutil.rmtree(self.basedir) |
---|
363 | |
---|
364 | def _create_file(self, *path): |
---|
365 | filename = os.path.join(self.basedir, *path) |
---|
366 | dirname = os.path.dirname(filename) |
---|
367 | os.makedirs(dirname) |
---|
368 | fd = file(filename, 'w') |
---|
369 | fd.close() |
---|
370 | return filename[len(self.basedir) + 1:] |
---|
371 | |
---|
372 | def test_missing_param_summary(self): |
---|
373 | self.summary.close() |
---|
374 | self.assertRaises(AssertionError, pythontools.coverage, self.ctxt) |
---|
375 | |
---|
376 | def test_empty_summary(self): |
---|
377 | pickle.dump({}, self.summary) |
---|
378 | self.summary.close() |
---|
379 | pythontools.figleaf(self.ctxt, summary=self.summary.name, include='*.py') |
---|
380 | type, category, generator, xml = self.ctxt.output.pop() |
---|
381 | self.assertEqual(Recipe.REPORT, type) |
---|
382 | self.assertEqual('coverage', category) |
---|
383 | self.assertEqual(0, len(xml.children)) |
---|
384 | |
---|
385 | def test_missing_coverage_file(self): |
---|
386 | self.summary.close() |
---|
387 | pythontools.figleaf(self.ctxt, summary='non-existant-file', include='*.py') |
---|
388 | self.assertEqual([], self.ctxt.output) |
---|
389 | |
---|
390 | def test_summary_with_absolute_path(self): |
---|
391 | filename = os.sep.join([self.ctxt.basedir, 'test', 'module.py']) |
---|
392 | pickle.dump({ |
---|
393 | filename: set([1, 4, 5]), |
---|
394 | }, self.summary) |
---|
395 | self.summary.close() |
---|
396 | sourcefile = self.ctxt.resolve(self._create_file('test', 'module.py')) |
---|
397 | open(sourcefile, 'w').write( |
---|
398 | "if foo: # line 1\n" |
---|
399 | " print 'uncovered' # line 2\n" |
---|
400 | "else: # line 3 (uninteresting)\n" |
---|
401 | " print 'covered' # line 4\n" |
---|
402 | "print 'covered' # line 6\n" |
---|
403 | ) |
---|
404 | pythontools.figleaf(self.ctxt, summary=self.summary.name, |
---|
405 | include='test/*') |
---|
406 | type, category, generator, xml = self.ctxt.output.pop() |
---|
407 | self.assertEqual(Recipe.REPORT, type) |
---|
408 | self.assertEqual('coverage', category) |
---|
409 | self.assertEqual(1, len(xml.children)) |
---|
410 | child = xml.children[0] |
---|
411 | self.assertEqual('coverage', child.name) |
---|
412 | self.assertEqual('test.module', child.attr['name']) |
---|
413 | self.assertEqual('test/module.py', child.attr['file']) |
---|
414 | self.assertEqual(75, child.attr['percentage']) |
---|
415 | self.assertEqual(4, child.attr['lines']) |
---|
416 | self.assertEqual('1 0 - 1 1', child.attr['line_hits']) |
---|
417 | |
---|
418 | def test_summary_with_non_covered_file(self): |
---|
419 | pickle.dump({}, self.summary) |
---|
420 | self.summary.close() |
---|
421 | sourcefile = self.ctxt.resolve(self._create_file('test', 'module.py')) |
---|
422 | open(sourcefile, 'w').write( |
---|
423 | "print 'line 1'\n" |
---|
424 | "print 'line 2'\n" |
---|
425 | "print 'line 3'\n" |
---|
426 | "print 'line 4'\n" |
---|
427 | "print 'line 5'\n" |
---|
428 | ) |
---|
429 | pythontools.figleaf(self.ctxt, summary=self.summary.name, |
---|
430 | include='test/*') |
---|
431 | type, category, generator, xml = self.ctxt.output.pop() |
---|
432 | self.assertEqual(Recipe.REPORT, type) |
---|
433 | self.assertEqual('coverage', category) |
---|
434 | self.assertEqual(1, len(xml.children)) |
---|
435 | child = xml.children[0] |
---|
436 | self.assertEqual('coverage', child.name) |
---|
437 | self.assertEqual('test.module', child.attr['name']) |
---|
438 | self.assertEqual('test/module.py', child.attr['file']) |
---|
439 | self.assertEqual(0, child.attr['percentage']) |
---|
440 | self.assertEqual(5, child.attr['lines']) |
---|
441 | |
---|
442 | def test_summary_with_non_python_files(self): |
---|
443 | "Figleaf coverage reports should not include files that do not end in .py" |
---|
444 | pickle.dump({}, self.summary) |
---|
445 | self.summary.close() |
---|
446 | sourcefile = self.ctxt.resolve(self._create_file('test', 'document.txt')) |
---|
447 | open(sourcefile, 'w').write("\n") |
---|
448 | pythontools.figleaf(self.ctxt, summary=self.summary.name, |
---|
449 | include='test/*') |
---|
450 | type, category, generator, xml = self.ctxt.output.pop() |
---|
451 | self.assertEqual(Recipe.REPORT, type) |
---|
452 | self.assertEqual('coverage', category) |
---|
453 | self.assertEqual(0, len(xml.children)) |
---|
454 | |
---|
455 | |
---|
456 | class FilenameNormalizationTestCase(unittest.TestCase): |
---|
457 | |
---|
458 | def setUp(self): |
---|
459 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
460 | self.ctxt = Context(self.basedir) |
---|
461 | |
---|
462 | def tearDown(self): |
---|
463 | shutil.rmtree(self.basedir) |
---|
464 | |
---|
465 | def _create_file(self, *path): |
---|
466 | filename = os.path.join(self.basedir, *path) |
---|
467 | dirname = os.path.dirname(filename) |
---|
468 | os.makedirs(dirname) |
---|
469 | fd = file(filename, 'w') |
---|
470 | fd.close() |
---|
471 | return filename[len(self.basedir) + 1:] |
---|
472 | |
---|
473 | def test_absolute_path(self): |
---|
474 | filename = os.sep.join([self.ctxt.basedir, 'test', 'module.py']) |
---|
475 | self._create_file('test', 'module.py') |
---|
476 | filenames = pythontools._normalize_filenames( |
---|
477 | self.ctxt, [filename], |
---|
478 | FileSet(self.ctxt.basedir, '**/*.py', None)) |
---|
479 | self.assertEqual(['test/module.py'], list(filenames)) |
---|
480 | |
---|
481 | |
---|
482 | class UnittestTestCase(unittest.TestCase): |
---|
483 | |
---|
484 | def setUp(self): |
---|
485 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
486 | self.ctxt = Context(self.basedir) |
---|
487 | self.results_xml = open(os.path.join(self.basedir, 'test-results.xml'), |
---|
488 | 'w') |
---|
489 | |
---|
490 | def tearDown(self): |
---|
491 | shutil.rmtree(self.basedir) |
---|
492 | |
---|
493 | def test_missing_file_param(self): |
---|
494 | self.results_xml.close() |
---|
495 | self.assertRaises(AssertionError, pythontools.unittest, self.ctxt) |
---|
496 | |
---|
497 | def test_empty_results(self): |
---|
498 | self.results_xml.write('<?xml version="1.0"?>' |
---|
499 | '<unittest-results>' |
---|
500 | '</unittest-results>') |
---|
501 | self.results_xml.close() |
---|
502 | pythontools.unittest(self.ctxt, self.results_xml.name) |
---|
503 | type, category, generator, xml = self.ctxt.output.pop() |
---|
504 | self.assertEqual(Recipe.REPORT, type) |
---|
505 | self.assertEqual('test', category) |
---|
506 | self.assertEqual(0, len(xml.children)) |
---|
507 | |
---|
508 | def test_successful_test(self): |
---|
509 | self.results_xml.write('<?xml version="1.0"?>' |
---|
510 | '<unittest-results>' |
---|
511 | '<test duration="0.12" status="success"' |
---|
512 | ' file="%s"' |
---|
513 | ' name="test_foo (pkg.BarTestCase)"/>' |
---|
514 | '</unittest-results>' |
---|
515 | % os.path.join(self.ctxt.basedir, 'bar_test.py')) |
---|
516 | self.results_xml.close() |
---|
517 | pythontools.unittest(self.ctxt, self.results_xml.name) |
---|
518 | type, category, generator, xml = self.ctxt.output.pop() |
---|
519 | self.assertEqual(1, len(xml.children)) |
---|
520 | test_elem = xml.children[0] |
---|
521 | self.assertEqual('test', test_elem.name) |
---|
522 | self.assertEqual('0.12', test_elem.attr['duration']) |
---|
523 | self.assertEqual('success', test_elem.attr['status']) |
---|
524 | self.assertEqual('bar_test.py', test_elem.attr['file']) |
---|
525 | self.assertEqual('test_foo (pkg.BarTestCase)', test_elem.attr['name']) |
---|
526 | |
---|
527 | def test_file_path_normalization(self): |
---|
528 | self.results_xml.write('<?xml version="1.0"?>' |
---|
529 | '<unittest-results>' |
---|
530 | '<test duration="0.12" status="success"' |
---|
531 | ' file="%s"' |
---|
532 | ' name="test_foo (pkg.BarTestCase)"/>' |
---|
533 | '</unittest-results>' |
---|
534 | % os.path.join(self.ctxt.basedir, 'bar_test.py')) |
---|
535 | self.results_xml.close() |
---|
536 | pythontools.unittest(self.ctxt, self.results_xml.name) |
---|
537 | type, category, generator, xml = self.ctxt.output.pop() |
---|
538 | self.assertEqual(1, len(xml.children)) |
---|
539 | self.assertEqual('bar_test.py', xml.children[0].attr['file']) |
---|
540 | |
---|
541 | def test_missing_file_attribute(self): |
---|
542 | self.results_xml.write('<?xml version="1.0"?>' |
---|
543 | '<unittest-results>' |
---|
544 | '<test duration="0.12" status="success"' |
---|
545 | ' name="test_foo (pkg.BarTestCase)"/>' |
---|
546 | '</unittest-results>') |
---|
547 | self.results_xml.close() |
---|
548 | pythontools.unittest(self.ctxt, self.results_xml.name) |
---|
549 | type, category, generator, xml = self.ctxt.output.pop() |
---|
550 | self.assertEqual(1, len(xml.children)) |
---|
551 | self.assertEqual(None, xml.children[0].attr.get('file')) |
---|
552 | |
---|
553 | |
---|
554 | def suite(): |
---|
555 | suite = unittest.TestSuite() |
---|
556 | suite.addTest(unittest.makeSuite(CoverageTestCase, 'test')) |
---|
557 | suite.addTest(unittest.makeSuite(TraceTestCase, 'test')) |
---|
558 | suite.addTest(unittest.makeSuite(PyLintTestCase, 'test')) |
---|
559 | suite.addTest(unittest.makeSuite(FigleafTestCase, 'test')) |
---|
560 | suite.addTest(unittest.makeSuite(FilenameNormalizationTestCase, 'test')) |
---|
561 | suite.addTest(unittest.makeSuite(UnittestTestCase, 'test')) |
---|
562 | return suite |
---|
563 | |
---|
564 | if __name__ == '__main__': |
---|
565 | unittest.main(defaultTest='suite') |
---|