"""mSCP subprocess uses a real Python when frozen (not the PyInstaller binary).""" from __future__ import annotations import platform import sys from pathlib import Path import pytest from applepy import mscp def test_python_for_mscp_respects_applepy_python(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: fake = tmp_path / "mypython" fake.write_text("#!/bin/sh\necho ok\n", encoding="utf-8") fake.chmod(0o755) monkeypatch.setenv("APPLEPY_PYTHON", str(fake)) monkeypatch.delenv("APPLEPY_MACOS_SECURITY_ROOT", raising=False) got = mscp._python_for_mscp_subprocess() assert got == str(fake) def test_python_for_mscp_frozen_non_darwin_uses_meipass_python( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.delenv("APPLEPY_PYTHON", raising=False) monkeypatch.setattr(sys, "frozen", True, raising=False) monkeypatch.setattr(sys, "_MEIPASS", str(tmp_path), raising=False) monkeypatch.setattr(platform, "system", lambda: "Linux") py = tmp_path / "python3" py.write_text("#!\n", encoding="utf-8") py.chmod(0o755) got = mscp._python_for_mscp_subprocess() assert got == str(py) def test_python_for_mscp_frozen_darwin_skips_meipass_python( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: """macOS bundle `python` is libPython, not an interpreter — use system or PATH.""" monkeypatch.delenv("APPLEPY_PYTHON", raising=False) monkeypatch.setattr(sys, "frozen", True, raising=False) me = tmp_path / "meipass" me.mkdir() fake_internal = me / "python3" fake_internal.write_text("# would be dylib on real bundle\n", encoding="utf-8") fake_internal.chmod(0o755) monkeypatch.setattr(sys, "_MEIPASS", str(me), raising=False) monkeypatch.setattr(platform, "system", lambda: "Darwin") monkeypatch.setattr(mscp, "_SYSTEM_PYTHON3_CANDIDATES", ()) good = tmp_path / "path_python3" good.write_text("#!/bin/sh\necho\n", encoding="utf-8") good.chmod(0o755) monkeypatch.setattr(mscp.shutil, "which", lambda _cmd: str(good)) got = mscp._python_for_mscp_subprocess() assert got == str(good) def test_python_for_mscp_non_frozen_uses_sys_executable(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("APPLEPY_PYTHON", raising=False) monkeypatch.setattr(sys, "frozen", False, raising=False) assert mscp._python_for_mscp_subprocess() == sys.executable def test_run_subprocess_via_logfiles_captures_streams() -> None: code, out, err = mscp._run_subprocess_via_logfiles( [sys.executable, "-c", "import sys; print('hello'); print('warn', file=sys.stderr)"], cwd=None, timeout=30.0, ) assert code == 0 assert "hello" in out assert "warn" in err