Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso committed Oct 12, 2023
1 parent bc65e7b commit 7c899b0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tests/test_rez.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
import sys
import stat
import typing
import pathlib
import platform
import unittest.mock

import pytest
Expand Down Expand Up @@ -275,3 +279,79 @@ def test_getPythonExecutables(
rez_pip.rez.getPythonExecutables(range_, "python")
== expectedExecutables
)


def test_getPythonExecutables_isolation(
monkeypatch: pytest.MonkeyPatch,
tmp_path: pathlib.Path,
) -> None:
"""
Test that getPythonExecutables is properly isolated. In other words, that
system wide PATH does not affect ResolvedContext.which calls.
If ResolvedContext.append_sys_path is not False, this test should fail.
"""
packagePath = tmp_path / "package"
fakePath = tmp_path / "fake"

# Create a fake python-1.0.0 package
repoData: typing.Dict[str, typing.Dict[str, typing.Dict[str, str]]] = {
"python": {
"1.0.0": {
"version": "1.0.0",
"commands": f"env.PATH.prepend('{os.fspath(packagePath)}')",
},
}
}

repo = typing.cast(
rez.package_repository.PackageRepository,
rez.package_repository.create_memory_package_repository(repoData),
)

with monkeypatch.context() as context:
context.setitem(
rez.package_repository.package_repository_manager.repositories,
f"memory@{repo.location}",
repo,
)

context.setattr(rez.config.config, "packages_path", [f"memory@{repo.location}"])

context.setenv("PATH", os.fspath(fakePath) + os.pathsep + os.environ["PATH"])

ext = ".bat" if platform.system() == "Windows" else ""
packagePath.mkdir()

# Create the python executable in the python package.
# Note that we use python1 here and we will use python1.0
# in the fake python install so that the fake would have hiher priority
# based on the algorithm in getPythonExecutables.
with open(packagePath / f"python1{ext}", "w") as f:
f.write("#FAKE EXECUTABLE")

if platform.system() != "Windows":
os.chmod(packagePath / f"python1{ext}", stat.S_IRUSR | stat.S_IXUSR)

fakePath.mkdir()

# Now create the fake executable.
with open(fakePath / f"python1.0{ext}", "w") as f:
f.write("#FAKE EXECUTABLE")

if platform.system() != "Windows":
os.chmod(fakePath / f"python1.0{ext}", stat.S_IRUSR | stat.S_IXUSR)

def mockedFunc(self, *args, **kwargs):
self.env.PATH.append(fakePath)

# Mock append_system_paths to force our fake path into the
# injected sys paths. If we don't do this, the fake path will not
# be set in the ResolvedContext when append_system_paths is True.
with unittest.mock.patch(
"rez.rex.RexExecutor.append_system_paths",
new=mockedFunc,
):
assert rez_pip.rez.getPythonExecutables("1.0.0", "python") == {
"1.0.0": os.fspath(packagePath / f"python1{ext}")
}

0 comments on commit 7c899b0

Please sign in to comment.