2025-12-07 20:36:39 +01:00
|
|
|
|
import os
|
2026-07-27 15:17:35 +02:00
|
|
|
|
import runpy
|
2025-12-06 17:47:46 +01:00
|
|
|
|
import subprocess
|
2026-07-27 15:17:35 +02:00
|
|
|
|
import sys
|
|
|
|
|
|
import unittest
|
2025-12-05 20:20:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2025-12-06 17:47:46 +01:00
|
|
|
|
def nix_profile_list_debug(label: str) -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Print `nix profile list` for debugging inside the test container.
|
|
|
|
|
|
Never fails the test.
|
|
|
|
|
|
"""
|
|
|
|
|
|
print(f"\n--- NIX PROFILE LIST ({label}) ---")
|
|
|
|
|
|
proc = subprocess.run(
|
|
|
|
|
|
["nix", "profile", "list"],
|
|
|
|
|
|
capture_output=True,
|
|
|
|
|
|
text=True,
|
|
|
|
|
|
check=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
stdout = proc.stdout.strip()
|
|
|
|
|
|
stderr = proc.stderr.strip()
|
|
|
|
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
|
|
print(stdout)
|
|
|
|
|
|
if stderr:
|
|
|
|
|
|
print("stderr:", stderr)
|
|
|
|
|
|
print("--- END ---\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_pkgmgr_from_nix_profile() -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Best-effort cleanup before running the integration test.
|
|
|
|
|
|
|
|
|
|
|
|
We *do not* try to parse profile indices here, because modern `nix profile list`
|
|
|
|
|
|
prints a descriptive format without an index column inside the container.
|
|
|
|
|
|
|
|
|
|
|
|
Instead, we directly try to remove possible names:
|
2025-12-09 22:57:11 +01:00
|
|
|
|
- 'pkgmgr'
|
|
|
|
|
|
- 'package-manager'
|
2025-12-06 17:47:46 +01:00
|
|
|
|
"""
|
|
|
|
|
|
for spec in ("pkgmgr", "package-manager"):
|
|
|
|
|
|
subprocess.run(
|
|
|
|
|
|
["nix", "profile", "remove", spec],
|
|
|
|
|
|
check=False, # never fail on cleanup
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
def configure_git_safe_directory() -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Configure Git to treat /src as a safe directory.
|
|
|
|
|
|
|
|
|
|
|
|
Needed because /src is a bind-mounted repository in CI, often owned by a
|
|
|
|
|
|
different UID. Modern Git aborts with:
|
|
|
|
|
|
'fatal: detected dubious ownership in repository at /src/.git'
|
|
|
|
|
|
|
|
|
|
|
|
This fix applies ONLY inside this test container.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
subprocess.run(
|
|
|
|
|
|
["git", "config", "--global", "--add", "safe.directory", "/src"],
|
|
|
|
|
|
check=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
print("[WARN] git not found – skipping safe.directory configuration")
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-07 20:36:39 +01:00
|
|
|
|
def pkgmgr_help_debug() -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Run `pkgmgr --help` after installation *inside an interactive bash shell*,
|
|
|
|
|
|
print its output and return code, but never fail the test.
|
|
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
This ensures the installer’s shell RC changes are actually loaded.
|
2025-12-07 20:36:39 +01:00
|
|
|
|
"""
|
|
|
|
|
|
print("\n--- PKGMGR HELP (after installation, via bash -i) ---")
|
|
|
|
|
|
|
|
|
|
|
|
proc = subprocess.run(
|
|
|
|
|
|
["bash", "-i", "-c", "pkgmgr --help"],
|
|
|
|
|
|
capture_output=True,
|
|
|
|
|
|
text=True,
|
|
|
|
|
|
check=False,
|
|
|
|
|
|
env=os.environ.copy(),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
stdout = proc.stdout.strip()
|
|
|
|
|
|
stderr = proc.stderr.strip()
|
|
|
|
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
|
|
print(stdout)
|
|
|
|
|
|
if stderr:
|
|
|
|
|
|
print("stderr:", stderr)
|
|
|
|
|
|
|
|
|
|
|
|
print(f"returncode: {proc.returncode}")
|
|
|
|
|
|
print("--- END ---\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-06 17:47:46 +01:00
|
|
|
|
class TestIntegrationInstalPKGMGRShallow(unittest.TestCase):
|
|
|
|
|
|
def test_install_pkgmgr_self_install(self) -> None:
|
2025-12-09 17:54:41 +01:00
|
|
|
|
"""
|
|
|
|
|
|
End-to-end test that runs "python main.py install pkgmgr ..." inside
|
|
|
|
|
|
the test container.
|
2025-12-06 17:47:46 +01:00
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
HOME is isolated to avoid permission problems with Nix & repositories.
|
2025-12-09 17:54:41 +01:00
|
|
|
|
"""
|
|
|
|
|
|
temp_home = "/tmp/pkgmgr-self-install"
|
|
|
|
|
|
os.makedirs(temp_home, exist_ok=True)
|
2025-12-05 20:20:33 +01:00
|
|
|
|
|
|
|
|
|
|
original_argv = sys.argv
|
2025-12-09 17:54:41 +01:00
|
|
|
|
original_environ = os.environ.copy()
|
|
|
|
|
|
|
2025-12-05 20:20:33 +01:00
|
|
|
|
try:
|
2025-12-09 17:54:41 +01:00
|
|
|
|
# Isolate HOME so that ~ expands to /tmp/pkgmgr-self-install
|
|
|
|
|
|
os.environ["HOME"] = temp_home
|
|
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Optional XDG override for a fully isolated environment
|
2025-12-09 17:54:41 +01:00
|
|
|
|
os.environ.setdefault("XDG_CONFIG_HOME", os.path.join(temp_home, ".config"))
|
|
|
|
|
|
os.environ.setdefault("XDG_CACHE_HOME", os.path.join(temp_home, ".cache"))
|
2025-12-18 14:04:44 +01:00
|
|
|
|
os.environ.setdefault(
|
|
|
|
|
|
"XDG_DATA_HOME", os.path.join(temp_home, ".local", "share")
|
|
|
|
|
|
)
|
2025-12-09 17:54:41 +01:00
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# 🔧 IMPORTANT FIX: allow Git to access /src safely
|
|
|
|
|
|
configure_git_safe_directory()
|
|
|
|
|
|
|
2025-12-09 17:54:41 +01:00
|
|
|
|
# Debug before cleanup
|
|
|
|
|
|
nix_profile_list_debug("BEFORE CLEANUP")
|
|
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Cleanup: drop any pkgmgr entries from nix profile
|
2025-12-09 17:54:41 +01:00
|
|
|
|
remove_pkgmgr_from_nix_profile()
|
|
|
|
|
|
|
|
|
|
|
|
# Debug after cleanup
|
|
|
|
|
|
nix_profile_list_debug("AFTER CLEANUP")
|
|
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Prepare argv for module execution
|
2025-12-05 20:20:33 +01:00
|
|
|
|
sys.argv = [
|
2025-12-06 17:47:46 +01:00
|
|
|
|
"python",
|
2025-12-05 20:20:33 +01:00
|
|
|
|
"install",
|
|
|
|
|
|
"pkgmgr",
|
|
|
|
|
|
"--clone-mode",
|
|
|
|
|
|
"shallow",
|
|
|
|
|
|
"--no-verification",
|
|
|
|
|
|
]
|
2025-12-09 17:54:41 +01:00
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Execute installation via main.py
|
2025-12-12 22:59:46 +01:00
|
|
|
|
runpy.run_module("pkgmgr", run_name="__main__")
|
2025-12-07 20:36:39 +01:00
|
|
|
|
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Debug: interactive shell test
|
2025-12-07 20:36:39 +01:00
|
|
|
|
pkgmgr_help_debug()
|
2025-12-09 17:54:41 +01:00
|
|
|
|
|
2025-12-05 20:20:33 +01:00
|
|
|
|
finally:
|
2025-12-09 22:57:11 +01:00
|
|
|
|
# Restore system state
|
2025-12-05 20:20:33 +01:00
|
|
|
|
sys.argv = original_argv
|
2025-12-09 17:54:41 +01:00
|
|
|
|
os.environ.clear()
|
|
|
|
|
|
os.environ.update(original_environ)
|
2025-12-05 20:20:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
unittest.main()
|