Skip to content

Commit

Permalink
Merge branch 'mr/leger/master/skip-dll-closure-tests-on-windows' into…
Browse files Browse the repository at this point in the history
… 'master'

Do not use `cygpath` calls in shared library closure check anymore

See merge request it/e3-core!96
  • Loading branch information
grouigrokon committed Jan 23, 2025
2 parents 9a1302b + c66362e commit 64ffd67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
33 changes: 18 additions & 15 deletions src/e3/os/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
from typing import Any, Literal
from collections.abc import Callable

CYGPATH_MATCH: re.Pattern[str] = re.compile(
"/((cygdrive/|mnt/)?(?P<drive>[a-zA-Z])/)?(?P<path>.*)", re.IGNORECASE
)


class OSFSError(e3.error.E3Error):
pass
Expand Down Expand Up @@ -253,8 +257,8 @@ def ldd_output_to_posix(ldd_output: str) -> str:
This method does not have any impact when the ``ldd`` output has
been executed on a Unix host, because paths are already POSIX there.
It applies only to ``ldd`` outputs on Windows, where the paths are
transformed by a call to ``cygpath -m``.
It applies only to ``ldd`` outputs on Windows, where the paths starting
with `/c/`, `/mnt/c/` or `/cygdrive/c/` are modified to `C:/`.
For instance, ``/c/WINDOWS/System32/ntdll.dll`` is transformed to
``C:/WINDOWS/System32/ntdll.dll``.
Expand All @@ -268,16 +272,14 @@ def ldd_output_to_posix(ldd_output: str) -> str:
:return: An ``ldd`` output with POSIX paths only.
"""
# Module may not be imported at toplevel, as it depends on this module.
from e3.os.process import Run

posix_content: str = ldd_output

if sys.platform == "win32" and Path(which("cygpath")).exists():
if sys.platform == "win32":
transformed: dict[str, str] = {}
posix_path: str
file_path: str | None = None
lines: list[str] = posix_content.splitlines()
drive: str = Path.cwd().drive.rstrip(":")

for line in lines:
if line.strip().endswith(":"):
Expand All @@ -289,16 +291,17 @@ def ldd_output_to_posix(ldd_output: str) -> str:

# Do not run on already transformed paths.
if file_path and file_path not in transformed:
cygpath_run: Run = Run(["cygpath", "-m", file_path])

# The value returned by cygpath may contain a trailing "\n",
# better remove it.
posix_path = cygpath_run.out.strip() if cygpath_run.out else file_path
match = CYGPATH_MATCH.match(file_path)
if match:
# If there is no drive, replace it with current drive.
posix_path = (
f"{match.group('drive') or drive}:/{match.group('path')}"
)

if posix_path != file_path:
# Transform all occurrences in the ldd output.
posix_content = posix_content.replace(file_path, posix_path)
transformed[file_path] = posix_path
if posix_path != file_path:
# Transform all occurrences in the ldd output.
posix_content = posix_content.replace(file_path, posix_path)
transformed[file_path] = posix_path

return posix_content

Expand Down
5 changes: 3 additions & 2 deletions tests/tests_e3/anod/spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from e3.anod.spec import Anod, __version__, check_api_version, has_primitive
from e3.env import Env
from e3.fs import cp
from e3.os.fs import ldd_output_to_posix
from e3.os.process import Run
from e3.platform_db.knowledge_base import OS_INFO

Expand Down Expand Up @@ -248,7 +249,7 @@ def test_spec_check_dll_closure_single_file(ldd) -> None: # type: ignore[no-unt
test_spec.sandbox = SandBox(root_dir=os.getcwd())

# Get the ldd output of the current executable.
ldd_output = Run(["ldd"] + [sys.executable]).out or ""
ldd_output = ldd_output_to_posix(Run(["ldd"] + [sys.executable]).out or "")

# Extract the first dll with a path from the ldd output.
for line in ldd_output.splitlines():
Expand Down Expand Up @@ -281,7 +282,7 @@ def test_spec_check_dll_closure_single_file(ldd) -> None: # type: ignore[no-unt
try:
result = test_spec.check_shared_libraries_closure(prefix=str(prefix))
assert len(result) == 1
assert Path(sys.executable).as_posix() in result
assert Path(shlib_path).as_posix() in result
except AnodError as ae:
assert shlib_path in ae.messages[0]

Expand Down

0 comments on commit 64ffd67

Please sign in to comment.