82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
"""Tests for the macOS app-bundle CLI wrapper generated by build.sh."""
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def _extract_cli_wrapper_script() -> str:
|
|
build_script = Path("apps/omlx-mac/Scripts/build.sh").read_text()
|
|
match = re.search(
|
|
r"cat > \"\$CLI_WRAPPER\" <<'EOF'\n(?P<script>.*?)\nEOF",
|
|
build_script,
|
|
re.DOTALL,
|
|
)
|
|
assert match is not None
|
|
return match.group("script")
|
|
|
|
|
|
def _write_fake_python(path: Path) -> None:
|
|
path.parent.mkdir(parents=True)
|
|
path.write_text(
|
|
"#!/bin/sh\n"
|
|
'printf "PYTHONHOME=%s\\n" "$PYTHONHOME"\n'
|
|
'printf "PYTHONPATH=%s\\n" "$PYTHONPATH"\n'
|
|
'printf "ARGS=%s\\n" "$*"\n'
|
|
)
|
|
path.chmod(0o755)
|
|
|
|
|
|
def test_app_bundle_cli_wrapper_resolves_symlinked_invocation(tmp_path):
|
|
"""The bundle wrapper must resolve paths from the app, not the symlink."""
|
|
script = _extract_cli_wrapper_script()
|
|
cli = tmp_path / "Applications/oMLX.app/Contents/MacOS/omlx-cli"
|
|
cli.parent.mkdir(parents=True)
|
|
cli.write_text(script)
|
|
cli.chmod(0o755)
|
|
|
|
app_root = tmp_path / "Applications/oMLX.app/Contents"
|
|
python = app_root / "Resources/Python/cpython-3.11/bin/python3"
|
|
_write_fake_python(python)
|
|
|
|
symlink = tmp_path / "usr/local/bin/omlx"
|
|
symlink.parent.mkdir(parents=True)
|
|
symlink.symlink_to(cli)
|
|
|
|
env = os.environ.copy()
|
|
env.pop("PYTHONPATH", None)
|
|
|
|
direct = subprocess.run(
|
|
[str(cli), "--help"],
|
|
env=env,
|
|
text=True,
|
|
capture_output=True,
|
|
check=True,
|
|
)
|
|
linked = subprocess.run(
|
|
[str(symlink), "--help"],
|
|
env=env,
|
|
text=True,
|
|
capture_output=True,
|
|
check=True,
|
|
)
|
|
|
|
expected_home = f"PYTHONHOME={app_root}/Resources/Python/cpython-3.11"
|
|
expected_path = (
|
|
f"PYTHONPATH={app_root}/Resources:"
|
|
f"{app_root}/Resources/Python/framework-mlx-base/lib/python3.11/site-packages"
|
|
)
|
|
expected_args = "ARGS=-m omlx.cli --help"
|
|
|
|
assert direct.stdout.splitlines() == [
|
|
expected_home,
|
|
expected_path,
|
|
expected_args,
|
|
]
|
|
assert linked.stdout.splitlines() == [
|
|
expected_home,
|
|
expected_path,
|
|
expected_args,
|
|
]
|