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