Skip to content

Commit

Permalink
improve test
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Mar 13, 2020
1 parent 05dc1ad commit eab7bb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def sources(cls, interpreter):
yield src
# install files needed to run site.py
for req in cls.modules():
stdlib_path = interpreter.stdlib_path("{}.py".format(req))
comp = Path(stdlib_path.stem + ".pyc")
comp = interpreter.stdlib_path("{}.pyc".format(req))
comp_exists = comp.exists()
if stdlib_path.exists() or not comp_exists:
yield PathRefToDest(stdlib_path, dest=cls.to_stdlib)
if comp_exists:
yield PathRefToDest(comp, dest=cls.to_stdlib)
stdlib_path = interpreter.stdlib_path("{}.py".format(req))
if stdlib_path.exists() or not comp_exists:
yield PathRefToDest(stdlib_path, dest=cls.to_stdlib)

def to_stdlib(self, src):
return self.stdlib / src.name
Expand Down
50 changes: 27 additions & 23 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from virtualenv.create.via_global_ref.builtin.cpython.cpython2 import CPython2
from virtualenv.discovery.builtin import get_interpreter
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import IS_PYPY, IS_WIN, PY3, fs_is_case_sensitive, fs_supports_symlink
from virtualenv.info import IS_PYPY, IS_WIN, PY2, PY3, fs_is_case_sensitive, fs_supports_symlink
from virtualenv.pyenv_cfg import PyEnvCfg
from virtualenv.run import cli_run, session_via_cli
from virtualenv.util.path import Path
Expand Down Expand Up @@ -451,29 +451,33 @@ def _get_sys_path(flag=None):
assert base == extra_all


@pytest.fixture
def creator_with_pyc_only_cp2_modules(tmp_path, monkeypatch, current_creators):
"""
Get a current_creator in a context where CPython2.modules only
references modules that have .pyc files (and no corresponding .py file).
"""
monkeypatch.chdir(tmp_path)
creator = current_creators[2]
module_name = os.path.relpath("CPython2_patch_modules_module", CURRENT.system_stdlib)
module_path = Path(CURRENT.system_stdlib) / "{}.pyc".format(module_name)
with module_path.open(mode="w"):
pass # just a touch
@pytest.mark.skipif(
not PY2 or not ("builtin" in CURRENT.creators().key_to_class), reason="stdlib python files only needed for Python 2"
)
def test_pyc_only(tmp_path, mocker, session_app_data):
"""Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)"""
interpreter = PythonInfo.from_exe(sys.executable, session_app_data)
host_pyc = interpreter.stdlib_path("os.pyc")
if not host_pyc.exists():
pytest.skip("missing system os.pyc at {}".format(host_pyc))
previous = interpreter.stdlib_path

def stdlib_path(name):
path = previous(name)
if name.endswith(".py"):

@classmethod
def modules(cls):
return [module_name]
class _Path(type(path)):
@staticmethod
def exists():
return False

monkeypatch.setattr(CPython2, "modules", modules)
yield creator
module_path.unlink()
return _Path(path)
return path

mocker.patch.object(interpreter, "stdlib_path", side_effect=stdlib_path)

result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", ""])

@pytest.mark.skipif(PY3, reason=".py files are only checked for in py2.")
def test_pyc_only(creator_with_pyc_only_cp2_modules):
"""Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)."""
assert creator_with_pyc_only_cp2_modules.can_create(CURRENT) is not None
assert not (result.creator.stdlib / "os.py").exists()
assert (result.creator.stdlib / "os.pyc").exists()
assert "os.pyc" in result.creator.debug["os"]

0 comments on commit eab7bb1

Please sign in to comment.