| 1 |
import os |
| 2 |
from pickle import dump |
| 3 |
import sys |
| 4 |
from test.support import captured_stdout, requires_resource |
| 5 |
from test.support.os_helper import (TESTFN, rmtree, unlink) |
| 6 |
from test.support.script_helper import assert_python_ok, assert_python_failure |
| 7 |
import textwrap |
| 8 |
import unittest |
| 9 |
from types import FunctionType |
| 10 |
|
| 11 |
import trace |
| 12 |
from trace import Trace |
| 13 |
|
| 14 |
from test.tracedmodules import testmod |
| 15 |
|
| 16 |
## |
| 17 |
## See also test_sys_settrace.py, which contains tests that cover |
| 18 |
## tracing of many more code blocks. |
| 19 |
## |
| 20 |
|
| 21 |
#------------------------------- Utilities -----------------------------------# |
| 22 |
|
| 23 |
def fix_ext_py(filename): |
| 24 |
"""Given a .pyc filename converts it to the appropriate .py""" |
| 25 |
if filename.endswith('.pyc'): |
| 26 |
filename = filename[:-1] |
| 27 |
return filename |
| 28 |
|
| 29 |
def my_file_and_modname(): |
| 30 |
"""The .py file and module name of this file (__file__)""" |
| 31 |
modname = os.path.splitext(os.path.basename(__file__))[0] |
| 32 |
return fix_ext_py(__file__), modname |
| 33 |
|
| 34 |
def get_firstlineno(func): |
| 35 |
return func.__code__.co_firstlineno |
| 36 |
|
| 37 |
#-------------------- Target functions for tracing ---------------------------# |
| 38 |
# |
| 39 |
# The relative line numbers of lines in these functions matter for verifying |
| 40 |
# tracing. Please modify the appropriate tests if you change one of the |
| 41 |
# functions. Absolute line numbers don't matter. |
| 42 |
# |
| 43 |
|
| 44 |
def traced_func_linear(x, y): |
| 45 |
a = x |
| 46 |
b = y |
| 47 |
c = a + b |
| 48 |
return c |
| 49 |
|
| 50 |
def traced_func_loop(x, y): |
| 51 |
c = x |
| 52 |
for i in range(5): |
| 53 |
c += y |
| 54 |
return c |
| 55 |
|
| 56 |
def traced_func_importing(x, y): |
| 57 |
return x + y + testmod.func(1) |
| 58 |
|
| 59 |
def traced_func_simple_caller(x): |
| 60 |
c = traced_func_linear(x, x) |
| 61 |
return c + x |
| 62 |
|
| 63 |
def traced_func_importing_caller(x): |
| 64 |
k = traced_func_simple_caller(x) |
| 65 |
k += traced_func_importing(k, x) |
| 66 |
return k |
| 67 |
|
| 68 |
def traced_func_generator(num): |
| 69 |
c = 5 # executed once |
| 70 |
for i in range(num): |
| 71 |
yield i + c |
| 72 |
|
| 73 |
def traced_func_calling_generator(): |
| 74 |
k = 0 |
| 75 |
for i in traced_func_generator(10): |
| 76 |
k += i |
| 77 |
|
| 78 |
def traced_doubler(num): |
| 79 |
return num * 2 |
| 80 |
|
| 81 |
def traced_capturer(*args, **kwargs): |
| 82 |
return args, kwargs |
| 83 |
|
| 84 |
def traced_caller_list_comprehension(): |
| 85 |
k = 10 |
| 86 |
mylist = [traced_doubler(i) for i in range(k)] |
| 87 |
return mylist |
| 88 |
|
| 89 |
def traced_decorated_function(): |
| 90 |
def decorator1(f): |
| 91 |
return f |
| 92 |
def decorator_fabric(): |
| 93 |
def decorator2(f): |
| 94 |
return f |
| 95 |
return decorator2 |
| 96 |
@decorator1 |
| 97 |
@decorator_fabric() |
| 98 |
def func(): |
| 99 |
pass |
| 100 |
func() |
| 101 |
|
| 102 |
|
| 103 |
class TracedClass(object): |
| 104 |
def __init__(self, x): |
| 105 |
self.a = x |
| 106 |
|
| 107 |
def inst_method_linear(self, y): |
| 108 |
return self.a + y |
| 109 |
|
| 110 |
def inst_method_calling(self, x): |
| 111 |
c = self.inst_method_linear(x) |
| 112 |
return c + traced_func_linear(x, c) |
| 113 |
|
| 114 |
@classmethod |
| 115 |
def class_method_linear(cls, y): |
| 116 |
return y * 2 |
| 117 |
|
| 118 |
@staticmethod |
| 119 |
def static_method_linear(y): |
| 120 |
return y * 2 |
| 121 |
|
| 122 |
|
| 123 |
#------------------------------ Test cases -----------------------------------# |
| 124 |
|
| 125 |
|
| 126 |
class TestLineCounts(unittest.TestCase): |
| 127 |
"""White-box testing of line-counting, via runfunc""" |
| 128 |
def setUp(self): |
| 129 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 130 |
self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 131 |
self.my_py_filename = fix_ext_py(__file__) |
| 132 |
|
| 133 |
def test_traced_func_linear(self): |
| 134 |
result = self.tracer.runfunc(traced_func_linear, 2, 5) |
| 135 |
self.assertEqual(result, 7) |
| 136 |
|
| 137 |
# all lines are executed once |
| 138 |
expected = {} |
| 139 |
firstlineno = get_firstlineno(traced_func_linear) |
| 140 |
for i in range(1, 5): |
| 141 |
expected[(self.my_py_filename, firstlineno + i)] = 1 |
| 142 |
|
| 143 |
self.assertEqual(self.tracer.results().counts, expected) |
| 144 |
|
| 145 |
def test_traced_func_loop(self): |
| 146 |
self.tracer.runfunc(traced_func_loop, 2, 3) |
| 147 |
|
| 148 |
firstlineno = get_firstlineno(traced_func_loop) |
| 149 |
expected = { |
| 150 |
(self.my_py_filename, firstlineno + 1): 1, |
| 151 |
(self.my_py_filename, firstlineno + 2): 6, |
| 152 |
(self.my_py_filename, firstlineno + 3): 5, |
| 153 |
(self.my_py_filename, firstlineno + 4): 1, |
| 154 |
} |
| 155 |
self.assertEqual(self.tracer.results().counts, expected) |
| 156 |
|
| 157 |
def test_traced_func_importing(self): |
| 158 |
self.tracer.runfunc(traced_func_importing, 2, 5) |
| 159 |
|
| 160 |
firstlineno = get_firstlineno(traced_func_importing) |
| 161 |
expected = { |
| 162 |
(self.my_py_filename, firstlineno + 1): 1, |
| 163 |
(fix_ext_py(testmod.__file__), 2): 1, |
| 164 |
(fix_ext_py(testmod.__file__), 3): 1, |
| 165 |
} |
| 166 |
|
| 167 |
self.assertEqual(self.tracer.results().counts, expected) |
| 168 |
|
| 169 |
def test_trace_func_generator(self): |
| 170 |
self.tracer.runfunc(traced_func_calling_generator) |
| 171 |
|
| 172 |
firstlineno_calling = get_firstlineno(traced_func_calling_generator) |
| 173 |
firstlineno_gen = get_firstlineno(traced_func_generator) |
| 174 |
expected = { |
| 175 |
(self.my_py_filename, firstlineno_calling + 1): 1, |
| 176 |
(self.my_py_filename, firstlineno_calling + 2): 11, |
| 177 |
(self.my_py_filename, firstlineno_calling + 3): 10, |
| 178 |
(self.my_py_filename, firstlineno_gen + 1): 1, |
| 179 |
(self.my_py_filename, firstlineno_gen + 2): 11, |
| 180 |
(self.my_py_filename, firstlineno_gen + 3): 10, |
| 181 |
} |
| 182 |
self.assertEqual(self.tracer.results().counts, expected) |
| 183 |
|
| 184 |
def test_trace_list_comprehension(self): |
| 185 |
self.tracer.runfunc(traced_caller_list_comprehension) |
| 186 |
|
| 187 |
firstlineno_calling = get_firstlineno(traced_caller_list_comprehension) |
| 188 |
firstlineno_called = get_firstlineno(traced_doubler) |
| 189 |
expected = { |
| 190 |
(self.my_py_filename, firstlineno_calling + 1): 1, |
| 191 |
(self.my_py_filename, firstlineno_calling + 2): 11, |
| 192 |
(self.my_py_filename, firstlineno_calling + 3): 1, |
| 193 |
(self.my_py_filename, firstlineno_called + 1): 10, |
| 194 |
} |
| 195 |
self.assertEqual(self.tracer.results().counts, expected) |
| 196 |
|
| 197 |
def test_traced_decorated_function(self): |
| 198 |
self.tracer.runfunc(traced_decorated_function) |
| 199 |
|
| 200 |
firstlineno = get_firstlineno(traced_decorated_function) |
| 201 |
expected = { |
| 202 |
(self.my_py_filename, firstlineno + 1): 1, |
| 203 |
(self.my_py_filename, firstlineno + 2): 1, |
| 204 |
(self.my_py_filename, firstlineno + 3): 1, |
| 205 |
(self.my_py_filename, firstlineno + 4): 1, |
| 206 |
(self.my_py_filename, firstlineno + 5): 1, |
| 207 |
(self.my_py_filename, firstlineno + 6): 1, |
| 208 |
(self.my_py_filename, firstlineno + 7): 2, |
| 209 |
(self.my_py_filename, firstlineno + 8): 2, |
| 210 |
(self.my_py_filename, firstlineno + 9): 2, |
| 211 |
(self.my_py_filename, firstlineno + 10): 1, |
| 212 |
(self.my_py_filename, firstlineno + 11): 1, |
| 213 |
} |
| 214 |
self.assertEqual(self.tracer.results().counts, expected) |
| 215 |
|
| 216 |
def test_linear_methods(self): |
| 217 |
# XXX todo: later add 'static_method_linear' and 'class_method_linear' |
| 218 |
# here, once issue1764286 is resolved |
| 219 |
# |
| 220 |
for methname in ['inst_method_linear',]: |
| 221 |
tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 222 |
traced_obj = TracedClass(25) |
| 223 |
method = getattr(traced_obj, methname) |
| 224 |
tracer.runfunc(method, 20) |
| 225 |
|
| 226 |
firstlineno = get_firstlineno(method) |
| 227 |
expected = { |
| 228 |
(self.my_py_filename, firstlineno + 1): 1, |
| 229 |
} |
| 230 |
self.assertEqual(tracer.results().counts, expected) |
| 231 |
|
| 232 |
|
| 233 |
class TestRunExecCounts(unittest.TestCase): |
| 234 |
"""A simple sanity test of line-counting, via runctx (exec)""" |
| 235 |
def setUp(self): |
| 236 |
self.my_py_filename = fix_ext_py(__file__) |
| 237 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 238 |
|
| 239 |
def test_exec_counts(self): |
| 240 |
self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 241 |
code = r'''traced_func_loop(2, 5)''' |
| 242 |
code = compile(code, __file__, 'exec') |
| 243 |
self.tracer.runctx(code, globals(), vars()) |
| 244 |
|
| 245 |
firstlineno = get_firstlineno(traced_func_loop) |
| 246 |
expected = { |
| 247 |
(self.my_py_filename, firstlineno + 1): 1, |
| 248 |
(self.my_py_filename, firstlineno + 2): 6, |
| 249 |
(self.my_py_filename, firstlineno + 3): 5, |
| 250 |
(self.my_py_filename, firstlineno + 4): 1, |
| 251 |
} |
| 252 |
|
| 253 |
# When used through 'run', some other spurious counts are produced, like |
| 254 |
# the settrace of threading, which we ignore, just making sure that the |
| 255 |
# counts fo traced_func_loop were right. |
| 256 |
# |
| 257 |
for k in expected.keys(): |
| 258 |
self.assertEqual(self.tracer.results().counts[k], expected[k]) |
| 259 |
|
| 260 |
|
| 261 |
class TestFuncs(unittest.TestCase): |
| 262 |
"""White-box testing of funcs tracing""" |
| 263 |
def setUp(self): |
| 264 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 265 |
self.tracer = Trace(count=0, trace=0, countfuncs=1) |
| 266 |
self.filemod = my_file_and_modname() |
| 267 |
self._saved_tracefunc = sys.gettrace() |
| 268 |
|
| 269 |
def tearDown(self): |
| 270 |
if self._saved_tracefunc is not None: |
| 271 |
sys.settrace(self._saved_tracefunc) |
| 272 |
|
| 273 |
def test_simple_caller(self): |
| 274 |
self.tracer.runfunc(traced_func_simple_caller, 1) |
| 275 |
|
| 276 |
expected = { |
| 277 |
self.filemod + ('traced_func_simple_caller',): 1, |
| 278 |
self.filemod + ('traced_func_linear',): 1, |
| 279 |
} |
| 280 |
self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 281 |
|
| 282 |
def test_arg_errors(self): |
| 283 |
res = self.tracer.runfunc(traced_capturer, 1, 2, self=3, func=4) |
| 284 |
self.assertEqual(res, ((1, 2), {'self': 3, 'func': 4})) |
| 285 |
with self.assertRaises(TypeError): |
| 286 |
self.tracer.runfunc(func=traced_capturer, arg=1) |
| 287 |
with self.assertRaises(TypeError): |
| 288 |
self.tracer.runfunc() |
| 289 |
|
| 290 |
def test_loop_caller_importing(self): |
| 291 |
self.tracer.runfunc(traced_func_importing_caller, 1) |
| 292 |
|
| 293 |
expected = { |
| 294 |
self.filemod + ('traced_func_simple_caller',): 1, |
| 295 |
self.filemod + ('traced_func_linear',): 1, |
| 296 |
self.filemod + ('traced_func_importing_caller',): 1, |
| 297 |
self.filemod + ('traced_func_importing',): 1, |
| 298 |
(fix_ext_py(testmod.__file__), 'testmod', 'func'): 1, |
| 299 |
} |
| 300 |
self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 301 |
|
| 302 |
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 303 |
'pre-existing trace function throws off measurements') |
| 304 |
def test_inst_method_calling(self): |
| 305 |
obj = TracedClass(20) |
| 306 |
self.tracer.runfunc(obj.inst_method_calling, 1) |
| 307 |
|
| 308 |
expected = { |
| 309 |
self.filemod + ('TracedClass.inst_method_calling',): 1, |
| 310 |
self.filemod + ('TracedClass.inst_method_linear',): 1, |
| 311 |
self.filemod + ('traced_func_linear',): 1, |
| 312 |
} |
| 313 |
self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 314 |
|
| 315 |
def test_traced_decorated_function(self): |
| 316 |
self.tracer.runfunc(traced_decorated_function) |
| 317 |
|
| 318 |
expected = { |
| 319 |
self.filemod + ('traced_decorated_function',): 1, |
| 320 |
self.filemod + ('decorator_fabric',): 1, |
| 321 |
self.filemod + ('decorator2',): 1, |
| 322 |
self.filemod + ('decorator1',): 1, |
| 323 |
self.filemod + ('func',): 1, |
| 324 |
} |
| 325 |
self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 326 |
|
| 327 |
|
| 328 |
class TestCallers(unittest.TestCase): |
| 329 |
"""White-box testing of callers tracing""" |
| 330 |
def setUp(self): |
| 331 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 332 |
self.tracer = Trace(count=0, trace=0, countcallers=1) |
| 333 |
self.filemod = my_file_and_modname() |
| 334 |
|
| 335 |
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 336 |
'pre-existing trace function throws off measurements') |
| 337 |
def test_loop_caller_importing(self): |
| 338 |
self.tracer.runfunc(traced_func_importing_caller, 1) |
| 339 |
|
| 340 |
expected = { |
| 341 |
((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'), |
| 342 |
(self.filemod + ('traced_func_importing_caller',))): 1, |
| 343 |
((self.filemod + ('traced_func_simple_caller',)), |
| 344 |
(self.filemod + ('traced_func_linear',))): 1, |
| 345 |
((self.filemod + ('traced_func_importing_caller',)), |
| 346 |
(self.filemod + ('traced_func_simple_caller',))): 1, |
| 347 |
((self.filemod + ('traced_func_importing_caller',)), |
| 348 |
(self.filemod + ('traced_func_importing',))): 1, |
| 349 |
((self.filemod + ('traced_func_importing',)), |
| 350 |
(fix_ext_py(testmod.__file__), 'testmod', 'func')): 1, |
| 351 |
} |
| 352 |
self.assertEqual(self.tracer.results().callers, expected) |
| 353 |
|
| 354 |
|
| 355 |
# Created separately for issue #3821 |
| 356 |
class TestCoverage(unittest.TestCase): |
| 357 |
def setUp(self): |
| 358 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 359 |
|
| 360 |
def tearDown(self): |
| 361 |
rmtree(TESTFN) |
| 362 |
unlink(TESTFN) |
| 363 |
|
| 364 |
DEFAULT_SCRIPT = '''if True: |
| 365 |
import unittest |
| 366 |
from test.test_pprint import QueryTestCase |
| 367 |
loader = unittest.TestLoader() |
| 368 |
tests = loader.loadTestsFromTestCase(QueryTestCase) |
| 369 |
tests(unittest.TestResult()) |
| 370 |
''' |
| 371 |
def _coverage(self, tracer, cmd=DEFAULT_SCRIPT): |
| 372 |
tracer.run(cmd) |
| 373 |
r = tracer.results() |
| 374 |
r.write_results(show_missing=True, summary=True, coverdir=TESTFN) |
| 375 |
|
| 376 |
@requires_resource('cpu') |
| 377 |
def test_coverage(self): |
| 378 |
tracer = trace.Trace(trace=0, count=1) |
| 379 |
with captured_stdout() as stdout: |
| 380 |
self._coverage(tracer) |
| 381 |
stdout = stdout.getvalue() |
| 382 |
self.assertIn("pprint.py", stdout) |
| 383 |
self.assertIn("case.py", stdout) # from unittest |
| 384 |
files = os.listdir(TESTFN) |
| 385 |
self.assertIn("pprint.cover", files) |
| 386 |
self.assertIn("unittest.case.cover", files) |
| 387 |
|
| 388 |
def test_coverage_ignore(self): |
| 389 |
# Ignore all files, nothing should be traced nor printed |
| 390 |
libpath = os.path.normpath(os.path.dirname(os.path.dirname(__file__))) |
| 391 |
# sys.prefix does not work when running from a checkout |
| 392 |
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix, |
| 393 |
libpath] + sys.path, trace=0, count=1) |
| 394 |
with captured_stdout() as stdout: |
| 395 |
self._coverage(tracer) |
| 396 |
if os.path.exists(TESTFN): |
| 397 |
files = os.listdir(TESTFN) |
| 398 |
self.assertEqual(files, ['_importlib.cover']) # Ignore __import__ |
| 399 |
|
| 400 |
def test_issue9936(self): |
| 401 |
tracer = trace.Trace(trace=0, count=1) |
| 402 |
modname = 'test.tracedmodules.testmod' |
| 403 |
# Ensure that the module is executed in import |
| 404 |
if modname in sys.modules: |
| 405 |
del sys.modules[modname] |
| 406 |
cmd = ("import test.tracedmodules.testmod as t;" |
| 407 |
"t.func(0); t.func2();") |
| 408 |
with captured_stdout() as stdout: |
| 409 |
self._coverage(tracer, cmd) |
| 410 |
stdout.seek(0) |
| 411 |
stdout.readline() |
| 412 |
coverage = {} |
| 413 |
for line in stdout: |
| 414 |
lines, cov, module = line.split()[:3] |
| 415 |
coverage[module] = (float(lines), float(cov[:-1])) |
| 416 |
# XXX This is needed to run regrtest.py as a script |
| 417 |
modname = trace._fullmodname(sys.modules[modname].__file__) |
| 418 |
self.assertIn(modname, coverage) |
| 419 |
self.assertEqual(coverage[modname], (5, 100)) |
| 420 |
|
| 421 |
def test_coverageresults_update(self): |
| 422 |
# Update empty CoverageResults with a non-empty infile. |
| 423 |
infile = TESTFN + '-infile' |
| 424 |
with open(infile, 'wb') as f: |
| 425 |
dump(({}, {}, {'caller': 1}), f, protocol=1) |
| 426 |
self.addCleanup(unlink, infile) |
| 427 |
results = trace.CoverageResults({}, {}, infile, {}) |
| 428 |
self.assertEqual(results.callers, {'caller': 1}) |
| 429 |
|
| 430 |
### Tests that don't mess with sys.settrace and can be traced |
| 431 |
### themselves TODO: Skip tests that do mess with sys.settrace when |
| 432 |
### regrtest is invoked with -T option. |
| 433 |
class Test_Ignore(unittest.TestCase): |
| 434 |
def test_ignored(self): |
| 435 |
jn = os.path.join |
| 436 |
ignore = trace._Ignore(['x', 'y.z'], [jn('foo', 'bar')]) |
| 437 |
self.assertTrue(ignore.names('x.py', 'x')) |
| 438 |
self.assertFalse(ignore.names('xy.py', 'xy')) |
| 439 |
self.assertFalse(ignore.names('y.py', 'y')) |
| 440 |
self.assertTrue(ignore.names(jn('foo', 'bar', 'baz.py'), 'baz')) |
| 441 |
self.assertFalse(ignore.names(jn('bar', 'z.py'), 'z')) |
| 442 |
# Matched before. |
| 443 |
self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz')) |
| 444 |
|
| 445 |
# Created for Issue 31908 -- CLI utility not writing cover files |
| 446 |
class TestCoverageCommandLineOutput(unittest.TestCase): |
| 447 |
|
| 448 |
codefile = 'tmp.py' |
| 449 |
coverfile = 'tmp.cover' |
| 450 |
|
| 451 |
def setUp(self): |
| 452 |
with open(self.codefile, 'w', encoding='iso-8859-15') as f: |
| 453 |
f.write(textwrap.dedent('''\ |
| 454 |
# coding: iso-8859-15 |
| 455 |
x = 'spœm' |
| 456 |
if []: |
| 457 |
print('unreachable') |
| 458 |
''')) |
| 459 |
|
| 460 |
def tearDown(self): |
| 461 |
unlink(self.codefile) |
| 462 |
unlink(self.coverfile) |
| 463 |
|
| 464 |
def test_cover_files_written_no_highlight(self): |
| 465 |
# Test also that the cover file for the trace module is not created |
| 466 |
# (issue #34171). |
| 467 |
tracedir = os.path.dirname(os.path.abspath(trace.__file__)) |
| 468 |
tracecoverpath = os.path.join(tracedir, 'trace.cover') |
| 469 |
unlink(tracecoverpath) |
| 470 |
|
| 471 |
argv = '-m trace --count'.split() + [self.codefile] |
| 472 |
status, stdout, stderr = assert_python_ok(*argv) |
| 473 |
self.assertEqual(stderr, b'') |
| 474 |
self.assertFalse(os.path.exists(tracecoverpath)) |
| 475 |
self.assertTrue(os.path.exists(self.coverfile)) |
| 476 |
with open(self.coverfile, encoding='iso-8859-15') as f: |
| 477 |
self.assertEqual(f.read(), |
| 478 |
" # coding: iso-8859-15\n" |
| 479 |
" 1: x = 'spœm'\n" |
| 480 |
" 1: if []:\n" |
| 481 |
" print('unreachable')\n" |
| 482 |
) |
| 483 |
|
| 484 |
def test_cover_files_written_with_highlight(self): |
| 485 |
argv = '-m trace --count --missing'.split() + [self.codefile] |
| 486 |
status, stdout, stderr = assert_python_ok(*argv) |
| 487 |
self.assertTrue(os.path.exists(self.coverfile)) |
| 488 |
with open(self.coverfile, encoding='iso-8859-15') as f: |
| 489 |
self.assertEqual(f.read(), textwrap.dedent('''\ |
| 490 |
# coding: iso-8859-15 |
| 491 |
1: x = 'spœm' |
| 492 |
1: if []: |
| 493 |
>>>>>> print('unreachable') |
| 494 |
''')) |
| 495 |
|
| 496 |
class TestCommandLine(unittest.TestCase): |
| 497 |
|
| 498 |
def test_failures(self): |
| 499 |
_errors = ( |
| 500 |
(b'progname is missing: required with the main options', '-l', '-T'), |
| 501 |
(b'cannot specify both --listfuncs and (--trace or --count)', '-lc'), |
| 502 |
(b'argument -R/--no-report: not allowed with argument -r/--report', '-rR'), |
| 503 |
(b'must specify one of --trace, --count, --report, --listfuncs, or --trackcalls', '-g'), |
| 504 |
(b'-r/--report requires -f/--file', '-r'), |
| 505 |
(b'--summary can only be used with --count or --report', '-sT'), |
| 506 |
(b'unrecognized arguments: -y', '-y')) |
| 507 |
for message, *args in _errors: |
| 508 |
*_, stderr = assert_python_failure('-m', 'trace', *args) |
| 509 |
self.assertIn(message, stderr) |
| 510 |
|
| 511 |
def test_listfuncs_flag_success(self): |
| 512 |
filename = TESTFN + '.py' |
| 513 |
modulename = os.path.basename(TESTFN) |
| 514 |
with open(filename, 'w', encoding='utf-8') as fd: |
| 515 |
self.addCleanup(unlink, filename) |
| 516 |
fd.write("a = 1\n") |
| 517 |
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', filename, |
| 518 |
PYTHONIOENCODING='utf-8') |
| 519 |
self.assertIn(b'functions called:', stdout) |
| 520 |
expected = f'filename: {filename}, modulename: {modulename}, funcname: <module>' |
| 521 |
self.assertIn(expected.encode(), stdout) |
| 522 |
|
| 523 |
def test_sys_argv_list(self): |
| 524 |
with open(TESTFN, 'w', encoding='utf-8') as fd: |
| 525 |
self.addCleanup(unlink, TESTFN) |
| 526 |
fd.write("import sys\n") |
| 527 |
fd.write("print(type(sys.argv))\n") |
| 528 |
|
| 529 |
status, direct_stdout, stderr = assert_python_ok(TESTFN) |
| 530 |
status, trace_stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN, |
| 531 |
PYTHONIOENCODING='utf-8') |
| 532 |
self.assertIn(direct_stdout.strip(), trace_stdout) |
| 533 |
|
| 534 |
def test_count_and_summary(self): |
| 535 |
filename = f'{TESTFN}.py' |
| 536 |
coverfilename = f'{TESTFN}.cover' |
| 537 |
modulename = os.path.basename(TESTFN) |
| 538 |
with open(filename, 'w', encoding='utf-8') as fd: |
| 539 |
self.addCleanup(unlink, filename) |
| 540 |
self.addCleanup(unlink, coverfilename) |
| 541 |
fd.write(textwrap.dedent("""\ |
| 542 |
x = 1 |
| 543 |
y = 2 |
| 544 |
|
| 545 |
def f(): |
| 546 |
return x + y |
| 547 |
|
| 548 |
for i in range(10): |
| 549 |
f() |
| 550 |
""")) |
| 551 |
status, stdout, _ = assert_python_ok('-m', 'trace', '-cs', filename, |
| 552 |
PYTHONIOENCODING='utf-8') |
| 553 |
stdout = stdout.decode() |
| 554 |
self.assertEqual(status, 0) |
| 555 |
self.assertIn('lines cov% module (path)', stdout) |
| 556 |
self.assertIn(f'6 100.0% {modulename} ({filename})', stdout) |
| 557 |
|
| 558 |
def test_run_as_module(self): |
| 559 |
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1') |
| 560 |
assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz') |
| 561 |
|
| 562 |
|
| 563 |
class TestTrace(unittest.TestCase): |
| 564 |
def setUp(self): |
| 565 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 566 |
self.tracer = Trace(count=0, trace=1) |
| 567 |
self.filemod = my_file_and_modname() |
| 568 |
|
| 569 |
def test_no_source_file(self): |
| 570 |
filename = "<unknown>" |
| 571 |
co = traced_func_linear.__code__ |
| 572 |
co = co.replace(co_filename=filename) |
| 573 |
f = FunctionType(co, globals()) |
| 574 |
|
| 575 |
with captured_stdout() as out: |
| 576 |
self.tracer.runfunc(f, 2, 3) |
| 577 |
|
| 578 |
out = out.getvalue().splitlines() |
| 579 |
firstlineno = get_firstlineno(f) |
| 580 |
self.assertIn(f" --- modulename: {self.filemod[1]}, funcname: {f.__code__.co_name}", out[0]) |
| 581 |
self.assertIn(f"{filename}({firstlineno + 1})", out[1]) |
| 582 |
self.assertIn(f"{filename}({firstlineno + 2})", out[2]) |
| 583 |
self.assertIn(f"{filename}({firstlineno + 3})", out[3]) |
| 584 |
self.assertIn(f"{filename}({firstlineno + 4})", out[4]) |
| 585 |
|
| 586 |
|
| 587 |
if __name__ == '__main__': |
| 588 |
unittest.main() |