Skip to content

Commit 25dee8f

Browse files
committed
testing: fix test_assertrewrite with PYTHONPYCACHEPREFIX
Make the tests work when running with PYTHONPYCACHEPREFIX (possible when running in a dirty environment, not under tox).
1 parent d9ac2ef commit 25dee8f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

testing/test_assertrewrite.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ def test_rewritten():
786786
sub.chmod(old_mode)
787787

788788
def test_dont_write_bytecode(self, testdir, monkeypatch):
789+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
790+
789791
testdir.makepyfile(
790792
"""
791793
import os
@@ -797,7 +799,10 @@ def test_no_bytecode():
797799
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
798800
assert testdir.runpytest_subprocess().ret == 0
799801

800-
def test_orphaned_pyc_file(self, testdir):
802+
def test_orphaned_pyc_file(self, testdir, monkeypatch):
803+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
804+
monkeypatch.setattr(sys, "pycache_prefix", None, raising=False)
805+
801806
testdir.makepyfile(
802807
"""
803808
import orphan
@@ -826,6 +831,7 @@ def test_it():
826831
def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
827832
"""Avoid stale caches (#1671)"""
828833
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
834+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
829835
testdir.makepyfile(
830836
test_foo="""
831837
def test_foo():
@@ -852,11 +858,13 @@ def test_optimized():
852858
tmp = "--basetemp=%s" % p
853859
monkeypatch.setenv("PYTHONOPTIMIZE", "2")
854860
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
861+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
855862
assert testdir.runpytest_subprocess(tmp).ret == 0
856863
tagged = "test_pyc_vs_pyo." + PYTEST_TAG
857864
assert tagged + ".pyo" in os.listdir("__pycache__")
858865
monkeypatch.undo()
859866
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
867+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
860868
assert testdir.runpytest_subprocess(tmp).ret == 1
861869
assert tagged + ".pyc" in os.listdir("__pycache__")
862870

@@ -1592,10 +1600,11 @@ class TestPyCacheDir:
15921600
],
15931601
)
15941602
def test_get_cache_dir(self, monkeypatch, prefix, source, expected):
1595-
if prefix:
1596-
if sys.version_info < (3, 8):
1597-
pytest.skip("pycache_prefix not available in py<38")
1598-
monkeypatch.setattr(sys, "pycache_prefix", prefix)
1603+
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
1604+
1605+
if prefix is not None and sys.version_info < (3, 8):
1606+
pytest.skip("pycache_prefix not available in py<38")
1607+
monkeypatch.setattr(sys, "pycache_prefix", prefix, raising=False)
15991608

16001609
assert get_cache_dir(Path(source)) == Path(expected)
16011610

0 commit comments

Comments
 (0)