Skip to content

Commit c6ae38e

Browse files
committed
improve test
Signed-off-by: Bernat Gabor <[email protected]>
1 parent 05dc1ad commit c6ae38e

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

src/virtualenv/create/via_global_ref/builtin/python2/python2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def sources(cls, interpreter):
5757
yield src
5858
# install files needed to run site.py
5959
for req in cls.modules():
60-
stdlib_path = interpreter.stdlib_path("{}.py".format(req))
61-
comp = Path(stdlib_path.stem + ".pyc")
60+
comp = interpreter.stdlib_path("{}.pyc".format(req))
6261
comp_exists = comp.exists()
63-
if stdlib_path.exists() or not comp_exists:
64-
yield PathRefToDest(stdlib_path, dest=cls.to_stdlib)
6562
if comp_exists:
6663
yield PathRefToDest(comp, dest=cls.to_stdlib)
64+
stdlib_path = interpreter.stdlib_path("{}.py".format(req))
65+
if stdlib_path.exists() or not comp_exists:
66+
yield PathRefToDest(stdlib_path, dest=cls.to_stdlib)
6767

6868
def to_stdlib(self, src):
6969
return self.stdlib / src.name

tests/unit/create/test_creator.py

+29-27
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from virtualenv.create.via_global_ref.builtin.cpython.cpython2 import CPython2
2323
from virtualenv.discovery.builtin import get_interpreter
2424
from virtualenv.discovery.py_info import PythonInfo
25-
from virtualenv.info import IS_PYPY, IS_WIN, PY3, fs_is_case_sensitive, fs_supports_symlink
25+
from virtualenv.info import IS_PYPY, IS_WIN, PY2, PY3, fs_is_case_sensitive, fs_supports_symlink
2626
from virtualenv.pyenv_cfg import PyEnvCfg
2727
from virtualenv.run import cli_run, session_via_cli
2828
from virtualenv.util.path import Path
@@ -451,29 +451,31 @@ def _get_sys_path(flag=None):
451451
assert base == extra_all
452452

453453

454-
@pytest.fixture
455-
def creator_with_pyc_only_cp2_modules(tmp_path, monkeypatch, current_creators):
456-
"""
457-
Get a current_creator in a context where CPython2.modules only
458-
references modules that have .pyc files (and no corresponding .py file).
459-
"""
460-
monkeypatch.chdir(tmp_path)
461-
creator = current_creators[2]
462-
module_name = os.path.relpath("CPython2_patch_modules_module", CURRENT.system_stdlib)
463-
module_path = Path(CURRENT.system_stdlib) / "{}.pyc".format(module_name)
464-
with module_path.open(mode="w"):
465-
pass # just a touch
466-
467-
@classmethod
468-
def modules(cls):
469-
return [module_name]
470-
471-
monkeypatch.setattr(CPython2, "modules", modules)
472-
yield creator
473-
module_path.unlink()
474-
475-
476-
@pytest.mark.skipif(PY3, reason=".py files are only checked for in py2.")
477-
def test_pyc_only(creator_with_pyc_only_cp2_modules):
478-
"""Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)."""
479-
assert creator_with_pyc_only_cp2_modules.can_create(CURRENT) is not None
454+
@pytest.mark.skipif(
455+
not PY2 or not ("builtin" in CURRENT.creators().key_to_class), reason="stdlib python files only needed for Python 2"
456+
)
457+
def test_pyc_only(tmp_path, mocker, session_app_data):
458+
"""Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)"""
459+
interpreter = PythonInfo.from_exe(sys.executable, session_app_data)
460+
host_pyc = interpreter.stdlib_path("os.pyc")
461+
if not host_pyc.exists():
462+
pytest.skip("missing system os.pyc at {}".format(host_pyc))
463+
previous = interpreter.stdlib_path
464+
465+
def stdlib_path(name):
466+
path = previous(name)
467+
if name.endswith(".py"):
468+
469+
class _Path(type(path)):
470+
@staticmethod
471+
def exists():
472+
return False
473+
474+
return _Path(path)
475+
return path
476+
477+
mocker.patch.object(interpreter, "stdlib_path", side_effect=stdlib_path)
478+
result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", ""])
479+
assert not (result.creator.stdlib / "os.py").exists()
480+
assert (result.creator.stdlib / "os.pyc").exists()
481+
assert 'os.pyc' in result.creator.debug['os']

0 commit comments

Comments
 (0)