@@ -457,31 +457,29 @@ def test_error(self):
457
457
class CommandLineTestsBase :
458
458
"""Test compileall's CLI."""
459
459
460
- @classmethod
461
- def setUpClass (cls ):
462
- for path in filter (os .path .isdir , sys .path ):
463
- directory_created = False
464
- directory = pathlib .Path (path ) / '__pycache__'
465
- path = directory / 'test.try'
466
- try :
467
- if not directory .is_dir ():
468
- directory .mkdir ()
469
- directory_created = True
470
- path .write_text ('# for test_compileall' , encoding = "utf-8" )
471
- except OSError :
472
- sys_path_writable = False
473
- break
474
- finally :
475
- os_helper .unlink (str (path ))
476
- if directory_created :
477
- directory .rmdir ()
478
- else :
479
- sys_path_writable = True
480
- cls ._sys_path_writable = sys_path_writable
481
-
482
- def _skip_if_sys_path_not_writable (self ):
483
- if not self ._sys_path_writable :
484
- raise unittest .SkipTest ('not all entries on sys.path are writable' )
460
+ def setUp (self ):
461
+ self .directory = tempfile .mkdtemp ()
462
+ self .addCleanup (os_helper .rmtree , self .directory )
463
+ self .pkgdir = os .path .join (self .directory , 'foo' )
464
+ os .mkdir (self .pkgdir )
465
+ self .pkgdir_cachedir = os .path .join (self .pkgdir , '__pycache__' )
466
+ # Create the __init__.py and a package module.
467
+ self .initfn = script_helper .make_script (self .pkgdir , '__init__' , '' )
468
+ self .barfn = script_helper .make_script (self .pkgdir , 'bar' , '' )
469
+
470
+ @contextlib .contextmanager
471
+ def temporary_pycache_prefix (self ):
472
+ """Adjust and restore sys.pycache_prefix."""
473
+ old_prefix = sys .pycache_prefix
474
+ new_prefix = os .path .join (self .directory , '__testcache__' )
475
+ try :
476
+ sys .pycache_prefix = new_prefix
477
+ yield {
478
+ 'PYTHONPATH' : self .directory ,
479
+ 'PYTHONPYCACHEPREFIX' : new_prefix ,
480
+ }
481
+ finally :
482
+ sys .pycache_prefix = old_prefix
485
483
486
484
def _get_run_args (self , args ):
487
485
return [* support .optim_args_from_interpreter_flags (),
@@ -509,49 +507,39 @@ def assertNotCompiled(self, fn):
509
507
path = importlib .util .cache_from_source (fn )
510
508
self .assertFalse (os .path .exists (path ))
511
509
512
- def setUp (self ):
513
- self .directory = tempfile .mkdtemp ()
514
- self .addCleanup (os_helper .rmtree , self .directory )
515
- self .pkgdir = os .path .join (self .directory , 'foo' )
516
- os .mkdir (self .pkgdir )
517
- self .pkgdir_cachedir = os .path .join (self .pkgdir , '__pycache__' )
518
- # Create the __init__.py and a package module.
519
- self .initfn = script_helper .make_script (self .pkgdir , '__init__' , '' )
520
- self .barfn = script_helper .make_script (self .pkgdir , 'bar' , '' )
521
-
522
510
def test_no_args_compiles_path (self ):
523
511
# Note that -l is implied for the no args case.
524
- self ._skip_if_sys_path_not_writable ()
525
512
bazfn = script_helper .make_script (self .directory , 'baz' , '' )
526
- self .assertRunOK (PYTHONPATH = self .directory )
527
- self .assertCompiled (bazfn )
528
- self .assertNotCompiled (self .initfn )
529
- self .assertNotCompiled (self .barfn )
513
+ with self .temporary_pycache_prefix () as env :
514
+ self .assertRunOK (** env )
515
+ self .assertCompiled (bazfn )
516
+ self .assertNotCompiled (self .initfn )
517
+ self .assertNotCompiled (self .barfn )
530
518
531
519
@without_source_date_epoch # timestamp invalidation test
532
520
def test_no_args_respects_force_flag (self ):
533
- self ._skip_if_sys_path_not_writable ()
534
521
bazfn = script_helper .make_script (self .directory , 'baz' , '' )
535
- self .assertRunOK (PYTHONPATH = self .directory )
536
- pycpath = importlib .util .cache_from_source (bazfn )
522
+ with self .temporary_pycache_prefix () as env :
523
+ self .assertRunOK (** env )
524
+ pycpath = importlib .util .cache_from_source (bazfn )
537
525
# Set atime/mtime backward to avoid file timestamp resolution issues
538
526
os .utime (pycpath , (time .time ()- 60 ,)* 2 )
539
527
mtime = os .stat (pycpath ).st_mtime
540
528
# Without force, no recompilation
541
- self .assertRunOK (PYTHONPATH = self . directory )
529
+ self .assertRunOK (** env )
542
530
mtime2 = os .stat (pycpath ).st_mtime
543
531
self .assertEqual (mtime , mtime2 )
544
532
# Now force it.
545
- self .assertRunOK ('-f' , PYTHONPATH = self . directory )
533
+ self .assertRunOK ('-f' , ** env )
546
534
mtime2 = os .stat (pycpath ).st_mtime
547
535
self .assertNotEqual (mtime , mtime2 )
548
536
549
537
def test_no_args_respects_quiet_flag (self ):
550
- self ._skip_if_sys_path_not_writable ()
551
538
script_helper .make_script (self .directory , 'baz' , '' )
552
- noisy = self .assertRunOK (PYTHONPATH = self .directory )
539
+ with self .temporary_pycache_prefix () as env :
540
+ noisy = self .assertRunOK (** env )
553
541
self .assertIn (b'Listing ' , noisy )
554
- quiet = self .assertRunOK ('-q' , PYTHONPATH = self . directory )
542
+ quiet = self .assertRunOK ('-q' , ** env )
555
543
self .assertNotIn (b'Listing ' , quiet )
556
544
557
545
# Ensure that the default behavior of compileall's CLI is to create
0 commit comments