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