1
1
import os
2
2
import 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
4
4
from test .support .script_helper import assert_python_ok , assert_python_failure
5
5
import textwrap
6
6
import unittest
@@ -429,9 +429,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
429
429
coverfile = 'tmp.cover'
430
430
431
431
def setUp (self ):
432
- with open (self .codefile , 'w' ) as f :
432
+ with open (self .codefile , 'w' , encoding = 'iso-8859-15' ) as f :
433
433
f .write (textwrap .dedent ('''\
434
- x = 42
434
+ # coding: iso-8859-15
435
+ x = 'spœm'
435
436
if []:
436
437
print('unreachable')
437
438
''' ))
@@ -452,9 +453,10 @@ def test_cover_files_written_no_highlight(self):
452
453
self .assertEqual (stderr , b'' )
453
454
self .assertFalse (os .path .exists (tracecoverpath ))
454
455
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 :
456
457
self .assertEqual (f .read (),
457
- " 1: x = 42\n "
458
+ " # coding: iso-8859-15\n "
459
+ " 1: x = 'spœm'\n "
458
460
" 1: if []:\n "
459
461
" print('unreachable')\n "
460
462
)
@@ -463,9 +465,10 @@ def test_cover_files_written_with_highlight(self):
463
465
argv = '-m trace --count --missing' .split () + [self .codefile ]
464
466
status , stdout , stderr = assert_python_ok (* argv )
465
467
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 :
467
469
self .assertEqual (f .read (), textwrap .dedent ('''\
468
- 1: x = 42
470
+ # coding: iso-8859-15
471
+ 1: x = 'spœm'
469
472
1: if []:
470
473
>>>>>> print('unreachable')
471
474
''' ))
@@ -486,14 +489,19 @@ def test_failures(self):
486
489
self .assertIn (message , stderr )
487
490
488
491
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 )
491
496
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' )
493
499
self .assertIn (b'functions called:' , stdout )
500
+ expected = f'filename: { filename } , modulename: { modulename } , funcname: <module>'
501
+ self .assertIn (expected .encode (), stdout )
494
502
495
503
def test_sys_argv_list (self ):
496
- with open (TESTFN , 'w' ) as fd :
504
+ with open (TESTFN , 'w' , encoding = 'utf-8' ) as fd :
497
505
self .addCleanup (unlink , TESTFN )
498
506
fd .write ("import sys\n " )
499
507
fd .write ("print(type(sys.argv))\n " )
@@ -505,7 +513,8 @@ def test_sys_argv_list(self):
505
513
def test_count_and_summary (self ):
506
514
filename = f'{ TESTFN } .py'
507
515
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 :
509
518
self .addCleanup (unlink , filename )
510
519
self .addCleanup (unlink , coverfilename )
511
520
fd .write (textwrap .dedent ("""\
@@ -522,7 +531,7 @@ def f():
522
531
stdout = stdout .decode ()
523
532
self .assertEqual (status , 0 )
524
533
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 )
526
535
527
536
def test_run_as_module (self ):
528
537
assert_python_ok ('-m' , 'trace' , '-l' , '--module' , 'timeit' , '-n' , '1' )
0 commit comments