11import os
22import sys
3- from test .support import TESTFN , rmtree , unlink , captured_stdout
3+ from test .support import TESTFN , TESTFN_UNICODE , FS_NONASCII , rmtree , unlink , captured_stdout
44from test .support .script_helper import assert_python_ok , assert_python_failure
55import textwrap
66import unittest
@@ -429,9 +429,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
429429 coverfile = 'tmp.cover'
430430
431431 def setUp (self ):
432- with open (self .codefile , 'w' ) as f :
432+ with open (self .codefile , 'w' , encoding = 'iso-8859-15' ) as f :
433433 f .write (textwrap .dedent ('''\
434- x = 42
434+ # coding: iso-8859-15
435+ x = 'spœm'
435436 if []:
436437 print('unreachable')
437438 ''' ))
@@ -452,9 +453,10 @@ def test_cover_files_written_no_highlight(self):
452453 self .assertEqual (stderr , b'' )
453454 self .assertFalse (os .path .exists (tracecoverpath ))
454455 self .assertTrue (os .path .exists (self .coverfile ))
455- with open (self .coverfile ) as f :
456+ with open (self .coverfile , encoding = 'iso-8859-15' ) as f :
456457 self .assertEqual (f .read (),
457- " 1: x = 42\n "
458+ " # coding: iso-8859-15\n "
459+ " 1: x = 'spœm'\n "
458460 " 1: if []:\n "
459461 " print('unreachable')\n "
460462 )
@@ -463,9 +465,10 @@ def test_cover_files_written_with_highlight(self):
463465 argv = '-m trace --count --missing' .split () + [self .codefile ]
464466 status , stdout , stderr = assert_python_ok (* argv )
465467 self .assertTrue (os .path .exists (self .coverfile ))
466- with open (self .coverfile ) as f :
468+ with open (self .coverfile , encoding = 'iso-8859-15' ) as f :
467469 self .assertEqual (f .read (), textwrap .dedent ('''\
468- 1: x = 42
470+ # coding: iso-8859-15
471+ 1: x = 'spœm'
469472 1: if []:
470473 >>>>>> print('unreachable')
471474 ''' ))
@@ -486,14 +489,19 @@ def test_failures(self):
486489 self .assertIn (message , stderr )
487490
488491 def test_listfuncs_flag_success (self ):
489- with open (TESTFN , 'w' ) as fd :
490- self .addCleanup (unlink , TESTFN )
492+ filename = TESTFN + '.py'
493+ modulename = os .path .basename (TESTFN )
494+ with open (filename , 'w' , encoding = 'utf-8' ) as fd :
495+ self .addCleanup (unlink , filename )
491496 fd .write ("a = 1\n " )
492- status , stdout , stderr = assert_python_ok ('-m' , 'trace' , '-l' , TESTFN )
497+ status , stdout , stderr = assert_python_ok ('-m' , 'trace' , '-l' , filename ,
498+ PYTHONIOENCODING = 'utf-8' )
493499 self .assertIn (b'functions called:' , stdout )
500+ expected = f'filename: { filename } , modulename: { modulename } , funcname: <module>'
501+ self .assertIn (expected .encode (), stdout )
494502
495503 def test_sys_argv_list (self ):
496- with open (TESTFN , 'w' ) as fd :
504+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as fd :
497505 self .addCleanup (unlink , TESTFN )
498506 fd .write ("import sys\n " )
499507 fd .write ("print(type(sys.argv))\n " )
@@ -505,7 +513,8 @@ def test_sys_argv_list(self):
505513 def test_count_and_summary (self ):
506514 filename = f'{ TESTFN } .py'
507515 coverfilename = f'{ TESTFN } .cover'
508- with open (filename , 'w' ) as fd :
516+ modulename = os .path .basename (TESTFN )
517+ with open (filename , 'w' , encoding = 'utf-8' ) as fd :
509518 self .addCleanup (unlink , filename )
510519 self .addCleanup (unlink , coverfilename )
511520 fd .write (textwrap .dedent ("""\
@@ -522,7 +531,7 @@ def f():
522531 stdout = stdout .decode ()
523532 self .assertEqual (status , 0 )
524533 self .assertIn ('lines cov% module (path)' , stdout )
525- self .assertIn (f'6 100% { TESTFN } ({ filename } )' , stdout )
534+ self .assertIn (f'6 100% { modulename } ({ filename } )' , stdout )
526535
527536 def test_run_as_module (self ):
528537 assert_python_ok ('-m' , 'trace' , '-l' , '--module' , 'timeit' , '-n' , '1' )
0 commit comments