blob: 75478557e787196366b55e7e627ee87a5c2a1ecf [file] [log] [blame]
Alexander Belopolsky4d770172010-09-13 18:14:34 +00001import os
2import sys
Hai Shia7f5d932020-08-04 00:41:24 +08003from test.support import captured_stdout
4from test.support.os_helper import (TESTFN, rmtree, unlink)
Senthil Kumaran436831d2016-01-13 07:46:54 -08005from test.support.script_helper import assert_python_ok, assert_python_failure
Michael Selik47ab1542018-04-30 20:46:52 -07006import textwrap
Georg Brandl283b1252010-08-02 12:48:46 +00007import unittest
Georg Brandl283b1252010-08-02 12:48:46 +00008
Alexander Belopolsky4d770172010-09-13 18:14:34 +00009import trace
Senthil Kumaran96b531a2016-01-11 07:09:42 -080010from trace import Trace
Alexander Belopolsky4d770172010-09-13 18:14:34 +000011
12from test.tracedmodules import testmod
13
Alexander Belopolsky4d770172010-09-13 18:14:34 +000014#------------------------------- Utilities -----------------------------------#
15
16def fix_ext_py(filename):
Brett Cannonf299abd2015-04-13 14:21:02 -040017 """Given a .pyc filename converts it to the appropriate .py"""
18 if filename.endswith('.pyc'):
Alexander Belopolsky4d770172010-09-13 18:14:34 +000019 filename = filename[:-1]
20 return filename
21
22def my_file_and_modname():
23 """The .py file and module name of this file (__file__)"""
24 modname = os.path.splitext(os.path.basename(__file__))[0]
25 return fix_ext_py(__file__), modname
26
27def get_firstlineno(func):
28 return func.__code__.co_firstlineno
29
30#-------------------- Target functions for tracing ---------------------------#
31#
32# The relative line numbers of lines in these functions matter for verifying
33# tracing. Please modify the appropriate tests if you change one of the
34# functions. Absolute line numbers don't matter.
35#
36
37def traced_func_linear(x, y):
38 a = x
39 b = y
40 c = a + b
41 return c
42
43def traced_func_loop(x, y):
44 c = x
45 for i in range(5):
46 c += y
47 return c
48
49def traced_func_importing(x, y):
50 return x + y + testmod.func(1)
51
52def traced_func_simple_caller(x):
53 c = traced_func_linear(x, x)
54 return c + x
55
56def traced_func_importing_caller(x):
57 k = traced_func_simple_caller(x)
58 k += traced_func_importing(k, x)
59 return k
60
61def traced_func_generator(num):
62 c = 5 # executed once
63 for i in range(num):
64 yield i + c
65
66def traced_func_calling_generator():
67 k = 0
68 for i in traced_func_generator(10):
69 k += i
70
71def traced_doubler(num):
72 return num * 2
73
Serhiy Storchaka42a139e2019-04-01 09:16:35 +030074def traced_capturer(*args, **kwargs):
75 return args, kwargs
76
Alexander Belopolsky4d770172010-09-13 18:14:34 +000077def traced_caller_list_comprehension():
78 k = 10
79 mylist = [traced_doubler(i) for i in range(k)]
80 return mylist
81
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +020082def traced_decorated_function():
83 def decorator1(f):
84 return f
85 def decorator_fabric():
86 def decorator2(f):
87 return f
88 return decorator2
89 @decorator1
90 @decorator_fabric()
91 def func():
92 pass
93 func()
94
Alexander Belopolsky4d770172010-09-13 18:14:34 +000095
96class TracedClass(object):
97 def __init__(self, x):
98 self.a = x
99
100 def inst_method_linear(self, y):
101 return self.a + y
102
103 def inst_method_calling(self, x):
104 c = self.inst_method_linear(x)
105 return c + traced_func_linear(x, c)
106
107 @classmethod
108 def class_method_linear(cls, y):
109 return y * 2
110
111 @staticmethod
112 def static_method_linear(y):
113 return y * 2
114
115
116#------------------------------ Test cases -----------------------------------#
117
118
119class TestLineCounts(unittest.TestCase):
120 """White-box testing of line-counting, via runfunc"""
121 def setUp(self):
Brett Cannon31f59292011-02-21 19:29:56 +0000122 self.addCleanup(sys.settrace, sys.gettrace())
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000123 self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
124 self.my_py_filename = fix_ext_py(__file__)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000125
126 def test_traced_func_linear(self):
127 result = self.tracer.runfunc(traced_func_linear, 2, 5)
128 self.assertEqual(result, 7)
129
130 # all lines are executed once
131 expected = {}
132 firstlineno = get_firstlineno(traced_func_linear)
133 for i in range(1, 5):
134 expected[(self.my_py_filename, firstlineno + i)] = 1
135
136 self.assertEqual(self.tracer.results().counts, expected)
137
138 def test_traced_func_loop(self):
139 self.tracer.runfunc(traced_func_loop, 2, 3)
140
141 firstlineno = get_firstlineno(traced_func_loop)
142 expected = {
143 (self.my_py_filename, firstlineno + 1): 1,
144 (self.my_py_filename, firstlineno + 2): 6,
145 (self.my_py_filename, firstlineno + 3): 5,
146 (self.my_py_filename, firstlineno + 4): 1,
147 }
148 self.assertEqual(self.tracer.results().counts, expected)
149
150 def test_traced_func_importing(self):
151 self.tracer.runfunc(traced_func_importing, 2, 5)
152
153 firstlineno = get_firstlineno(traced_func_importing)
154 expected = {
155 (self.my_py_filename, firstlineno + 1): 1,
156 (fix_ext_py(testmod.__file__), 2): 1,
157 (fix_ext_py(testmod.__file__), 3): 1,
158 }
159
160 self.assertEqual(self.tracer.results().counts, expected)
161
162 def test_trace_func_generator(self):
163 self.tracer.runfunc(traced_func_calling_generator)
164
165 firstlineno_calling = get_firstlineno(traced_func_calling_generator)
166 firstlineno_gen = get_firstlineno(traced_func_generator)
167 expected = {
168 (self.my_py_filename, firstlineno_calling + 1): 1,
169 (self.my_py_filename, firstlineno_calling + 2): 11,
170 (self.my_py_filename, firstlineno_calling + 3): 10,
171 (self.my_py_filename, firstlineno_gen + 1): 1,
172 (self.my_py_filename, firstlineno_gen + 2): 11,
173 (self.my_py_filename, firstlineno_gen + 3): 10,
174 }
175 self.assertEqual(self.tracer.results().counts, expected)
176
177 def test_trace_list_comprehension(self):
178 self.tracer.runfunc(traced_caller_list_comprehension)
179
180 firstlineno_calling = get_firstlineno(traced_caller_list_comprehension)
181 firstlineno_called = get_firstlineno(traced_doubler)
182 expected = {
183 (self.my_py_filename, firstlineno_calling + 1): 1,
Min ho Kimc4cacc82019-07-31 08:16:13 +1000184 # List comprehensions work differently in 3.x, so the count
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000185 # below changed compared to 2.x.
186 (self.my_py_filename, firstlineno_calling + 2): 12,
187 (self.my_py_filename, firstlineno_calling + 3): 1,
188 (self.my_py_filename, firstlineno_called + 1): 10,
189 }
190 self.assertEqual(self.tracer.results().counts, expected)
191
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200192 def test_traced_decorated_function(self):
193 self.tracer.runfunc(traced_decorated_function)
194
195 firstlineno = get_firstlineno(traced_decorated_function)
196 expected = {
197 (self.my_py_filename, firstlineno + 1): 1,
198 (self.my_py_filename, firstlineno + 2): 1,
199 (self.my_py_filename, firstlineno + 3): 1,
200 (self.my_py_filename, firstlineno + 4): 1,
201 (self.my_py_filename, firstlineno + 5): 1,
202 (self.my_py_filename, firstlineno + 6): 1,
203 (self.my_py_filename, firstlineno + 7): 1,
204 (self.my_py_filename, firstlineno + 8): 1,
205 (self.my_py_filename, firstlineno + 9): 1,
206 (self.my_py_filename, firstlineno + 10): 1,
207 (self.my_py_filename, firstlineno + 11): 1,
208 }
209 self.assertEqual(self.tracer.results().counts, expected)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000210
211 def test_linear_methods(self):
212 # XXX todo: later add 'static_method_linear' and 'class_method_linear'
213 # here, once issue1764286 is resolved
214 #
215 for methname in ['inst_method_linear',]:
216 tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
217 traced_obj = TracedClass(25)
218 method = getattr(traced_obj, methname)
219 tracer.runfunc(method, 20)
220
221 firstlineno = get_firstlineno(method)
222 expected = {
223 (self.my_py_filename, firstlineno + 1): 1,
224 }
225 self.assertEqual(tracer.results().counts, expected)
226
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +0200227
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000228class TestRunExecCounts(unittest.TestCase):
229 """A simple sanity test of line-counting, via runctx (exec)"""
230 def setUp(self):
231 self.my_py_filename = fix_ext_py(__file__)
Brett Cannon31f59292011-02-21 19:29:56 +0000232 self.addCleanup(sys.settrace, sys.gettrace())
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000233
234 def test_exec_counts(self):
235 self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
236 code = r'''traced_func_loop(2, 5)'''
237 code = compile(code, __file__, 'exec')
238 self.tracer.runctx(code, globals(), vars())
239
240 firstlineno = get_firstlineno(traced_func_loop)
241 expected = {
242 (self.my_py_filename, firstlineno + 1): 1,
243 (self.my_py_filename, firstlineno + 2): 6,
244 (self.my_py_filename, firstlineno + 3): 5,
245 (self.my_py_filename, firstlineno + 4): 1,
246 }
247
Ezio Melotti13925002011-03-16 11:05:33 +0200248 # When used through 'run', some other spurious counts are produced, like
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000249 # the settrace of threading, which we ignore, just making sure that the
250 # counts fo traced_func_loop were right.
251 #
252 for k in expected.keys():
253 self.assertEqual(self.tracer.results().counts[k], expected[k])
254
255
256class TestFuncs(unittest.TestCase):
257 """White-box testing of funcs tracing"""
258 def setUp(self):
Brett Cannon31f59292011-02-21 19:29:56 +0000259 self.addCleanup(sys.settrace, sys.gettrace())
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000260 self.tracer = Trace(count=0, trace=0, countfuncs=1)
261 self.filemod = my_file_and_modname()
Alexander Belopolskyf026dae2014-06-29 17:44:05 -0400262 self._saved_tracefunc = sys.gettrace()
263
264 def tearDown(self):
265 if self._saved_tracefunc is not None:
266 sys.settrace(self._saved_tracefunc)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000267
268 def test_simple_caller(self):
269 self.tracer.runfunc(traced_func_simple_caller, 1)
270
271 expected = {
272 self.filemod + ('traced_func_simple_caller',): 1,
273 self.filemod + ('traced_func_linear',): 1,
274 }
275 self.assertEqual(self.tracer.results().calledfuncs, expected)
276
Serhiy Storchaka42a139e2019-04-01 09:16:35 +0300277 def test_arg_errors(self):
278 res = self.tracer.runfunc(traced_capturer, 1, 2, self=3, func=4)
279 self.assertEqual(res, ((1, 2), {'self': 3, 'func': 4}))
Serhiy Storchaka142566c2019-06-05 18:22:31 +0300280 with self.assertRaises(TypeError):
281 self.tracer.runfunc(func=traced_capturer, arg=1)
Serhiy Storchaka42a139e2019-04-01 09:16:35 +0300282 with self.assertRaises(TypeError):
283 self.tracer.runfunc()
284
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000285 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 Cannon7a540732011-02-22 03:04:06 +0000297 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
298 'pre-existing trace function throws off measurements')
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000299 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 Storchaka95b6acf2018-10-30 13:16:02 +0200310 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 Belopolsky4d770172010-09-13 18:14:34 +0000322
323class TestCallers(unittest.TestCase):
324 """White-box testing of callers tracing"""
325 def setUp(self):
Brett Cannon31f59292011-02-21 19:29:56 +0000326 self.addCleanup(sys.settrace, sys.gettrace())
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000327 self.tracer = Trace(count=0, trace=0, countcallers=1)
328 self.filemod = my_file_and_modname()
329
Brett Cannon7a540732011-02-22 03:04:06 +0000330 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
331 'pre-existing trace function throws off measurements')
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000332 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 Brandl283b1252010-08-02 12:48:46 +0000351class TestCoverage(unittest.TestCase):
Brett Cannon31f59292011-02-21 19:29:56 +0000352 def setUp(self):
353 self.addCleanup(sys.settrace, sys.gettrace())
354
Georg Brandl283b1252010-08-02 12:48:46 +0000355 def tearDown(self):
356 rmtree(TESTFN)
357 unlink(TESTFN)
358
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000359 def _coverage(self, tracer,
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300360 cmd='import test.support, test.test_pprint;'
361 'test.support.run_unittest(test.test_pprint.QueryTestCase)'):
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000362 tracer.run(cmd)
Georg Brandl283b1252010-08-02 12:48:46 +0000363 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 Storchaka62e32d62016-11-11 12:05:01 +0200371 self.assertIn("pprint.py", stdout)
372 self.assertIn("case.py", stdout) # from unittest
Georg Brandl283b1252010-08-02 12:48:46 +0000373 files = os.listdir(TESTFN)
Serhiy Storchaka62e32d62016-11-11 12:05:01 +0200374 self.assertIn("pprint.cover", files)
375 self.assertIn("unittest.case.cover", files)
Georg Brandl283b1252010-08-02 12:48:46 +0000376
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 Sajip7ded1f02012-05-26 03:45:29 +0100381 tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,
382 libpath], trace=0, count=1)
Georg Brandl283b1252010-08-02 12:48:46 +0000383 with captured_stdout() as stdout:
384 self._coverage(tracer)
Georg Brandl283b1252010-08-02 12:48:46 +0000385 if os.path.exists(TESTFN):
386 files = os.listdir(TESTFN)
Brett Cannonfd074152012-04-14 14:10:13 -0400387 self.assertEqual(files, ['_importlib.cover']) # Ignore __import__
Georg Brandl283b1252010-08-02 12:48:46 +0000388
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000389 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 Belopolskya847c812010-09-24 22:04:22 +0000405 # XXX This is needed to run regrtest.py as a script
Alexander Belopolsky1f75f5d2010-11-26 18:51:39 +0000406 modname = trace._fullmodname(sys.modules[modname].__file__)
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000407 self.assertIn(modname, coverage)
408 self.assertEqual(coverage[modname], (5, 100))
409
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000410### 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.
413class Test_Ignore(unittest.TestCase):
414 def test_ignored(self):
Alexander Belopolsky18c33732010-11-08 23:10:20 +0000415 jn = os.path.join
Alexander Belopolsky1f75f5d2010-11-26 18:51:39 +0000416 ignore = trace._Ignore(['x', 'y.z'], [jn('foo', 'bar')])
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000417 self.assertTrue(ignore.names('x.py', 'x'))
418 self.assertFalse(ignore.names('xy.py', 'xy'))
419 self.assertFalse(ignore.names('y.py', 'y'))
Alexander Belopolsky18c33732010-11-08 23:10:20 +0000420 self.assertTrue(ignore.names(jn('foo', 'bar', 'baz.py'), 'baz'))
421 self.assertFalse(ignore.names(jn('bar', 'z.py'), 'z'))
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000422 # Matched before.
Alexander Belopolsky18c33732010-11-08 23:10:20 +0000423 self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz'))
Alexander Belopolsky6672ea92010-11-08 18:32:40 +0000424
Michael Selik47ab1542018-04-30 20:46:52 -0700425# Created for Issue 31908 -- CLI utility not writing cover files
426class TestCoverageCommandLineOutput(unittest.TestCase):
427
428 codefile = 'tmp.py'
429 coverfile = 'tmp.cover'
430
431 def setUp(self):
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300432 with open(self.codefile, 'w', encoding='iso-8859-15') as f:
Michael Selik47ab1542018-04-30 20:46:52 -0700433 f.write(textwrap.dedent('''\
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300434 # coding: iso-8859-15
435 x = 'spœm'
Michael Selik47ab1542018-04-30 20:46:52 -0700436 if []:
437 print('unreachable')
438 '''))
439
440 def tearDown(self):
441 unlink(self.codefile)
442 unlink(self.coverfile)
443
444 def test_cover_files_written_no_highlight(self):
Serhiy Storchakab44a1d42018-08-27 13:10:36 +0300445 # Test also that the cover file for the trace module is not created
446 # (issue #34171).
447 tracedir = os.path.dirname(os.path.abspath(trace.__file__))
448 tracecoverpath = os.path.join(tracedir, 'trace.cover')
449 unlink(tracecoverpath)
450
Michael Selik47ab1542018-04-30 20:46:52 -0700451 argv = '-m trace --count'.split() + [self.codefile]
452 status, stdout, stderr = assert_python_ok(*argv)
Serhiy Storchakac406d5c2018-08-25 10:27:55 +0300453 self.assertEqual(stderr, b'')
Serhiy Storchakac406d5c2018-08-25 10:27:55 +0300454 self.assertFalse(os.path.exists(tracecoverpath))
Michael Selik47ab1542018-04-30 20:46:52 -0700455 self.assertTrue(os.path.exists(self.coverfile))
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300456 with open(self.coverfile, encoding='iso-8859-15') as f:
Michael Selik47ab1542018-04-30 20:46:52 -0700457 self.assertEqual(f.read(),
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300458 " # coding: iso-8859-15\n"
459 " 1: x = 'spœm'\n"
Michael Selik47ab1542018-04-30 20:46:52 -0700460 " 1: if []:\n"
461 " print('unreachable')\n"
462 )
463
464 def test_cover_files_written_with_highlight(self):
465 argv = '-m trace --count --missing'.split() + [self.codefile]
466 status, stdout, stderr = assert_python_ok(*argv)
467 self.assertTrue(os.path.exists(self.coverfile))
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300468 with open(self.coverfile, encoding='iso-8859-15') as f:
Michael Selik47ab1542018-04-30 20:46:52 -0700469 self.assertEqual(f.read(), textwrap.dedent('''\
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300470 # coding: iso-8859-15
471 1: x = 'spœm'
Michael Selik47ab1542018-04-30 20:46:52 -0700472 1: if []:
473 >>>>>> print('unreachable')
474 '''))
475
Senthil Kumaran436831d2016-01-13 07:46:54 -0800476class TestCommandLine(unittest.TestCase):
477
478 def test_failures(self):
479 _errors = (
Mario Corchero354227a2019-06-01 05:49:10 +0100480 (b'progname is missing: required with the main options', '-l', '-T'),
Senthil Kumaran436831d2016-01-13 07:46:54 -0800481 (b'cannot specify both --listfuncs and (--trace or --count)', '-lc'),
482 (b'argument -R/--no-report: not allowed with argument -r/--report', '-rR'),
483 (b'must specify one of --trace, --count, --report, --listfuncs, or --trackcalls', '-g'),
484 (b'-r/--report requires -f/--file', '-r'),
485 (b'--summary can only be used with --count or --report', '-sT'),
486 (b'unrecognized arguments: -y', '-y'))
487 for message, *args in _errors:
488 *_, stderr = assert_python_failure('-m', 'trace', *args)
489 self.assertIn(message, stderr)
490
491 def test_listfuncs_flag_success(self):
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300492 filename = TESTFN + '.py'
493 modulename = os.path.basename(TESTFN)
494 with open(filename, 'w', encoding='utf-8') as fd:
495 self.addCleanup(unlink, filename)
Senthil Kumaran436831d2016-01-13 07:46:54 -0800496 fd.write("a = 1\n")
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300497 status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', filename,
Serhiy Storchaka700cfa82020-06-25 17:56:31 +0300498 PYTHONIOENCODING='utf-8')
Senthil Kumaran436831d2016-01-13 07:46:54 -0800499 self.assertIn(b'functions called:', stdout)
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300500 expected = f'filename: {filename}, modulename: {modulename}, funcname: <module>'
501 self.assertIn(expected.encode(), stdout)
Georg Brandl283b1252010-08-02 12:48:46 +0000502
Kyle Altendorf9f422322018-02-16 22:32:37 -0800503 def test_sys_argv_list(self):
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300504 with open(TESTFN, 'w', encoding='utf-8') as fd:
Kyle Altendorf9f422322018-02-16 22:32:37 -0800505 self.addCleanup(unlink, TESTFN)
506 fd.write("import sys\n")
507 fd.write("print(type(sys.argv))\n")
508
509 status, direct_stdout, stderr = assert_python_ok(TESTFN)
Serhiy Storchaka700cfa82020-06-25 17:56:31 +0300510 status, trace_stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN,
511 PYTHONIOENCODING='utf-8')
Kyle Altendorf9f422322018-02-16 22:32:37 -0800512 self.assertIn(direct_stdout.strip(), trace_stdout)
513
Berker Peksagc8b0dbc2018-08-11 09:15:43 +0300514 def test_count_and_summary(self):
515 filename = f'{TESTFN}.py'
516 coverfilename = f'{TESTFN}.cover'
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300517 modulename = os.path.basename(TESTFN)
518 with open(filename, 'w', encoding='utf-8') as fd:
Berker Peksagc8b0dbc2018-08-11 09:15:43 +0300519 self.addCleanup(unlink, filename)
520 self.addCleanup(unlink, coverfilename)
521 fd.write(textwrap.dedent("""\
522 x = 1
523 y = 2
524
525 def f():
526 return x + y
527
528 for i in range(10):
529 f()
530 """))
Serhiy Storchaka700cfa82020-06-25 17:56:31 +0300531 status, stdout, _ = assert_python_ok('-m', 'trace', '-cs', filename,
532 PYTHONIOENCODING='utf-8')
Berker Peksagc8b0dbc2018-08-11 09:15:43 +0300533 stdout = stdout.decode()
534 self.assertEqual(status, 0)
535 self.assertIn('lines cov% module (path)', stdout)
Serhiy Storchaka04cdeb72020-06-28 13:34:22 +0300536 self.assertIn(f'6 100% {modulename} ({filename})', stdout)
Berker Peksagc8b0dbc2018-08-11 09:15:43 +0300537
Mario Corchero354227a2019-06-01 05:49:10 +0100538 def test_run_as_module(self):
539 assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')
540 assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz')
541
542
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000543if __name__ == '__main__':
Serhiy Storchakacbfe07e2015-05-20 19:37:10 +0300544 unittest.main()