Files
pkgmgr/tests/e2e/test_path_commands.py
Kevin Veen-Birkenbach f5d428950e
Some checks failed
Mark stable commit / test-unit (push) Has been cancelled
Mark stable commit / test-integration (push) Has been cancelled
Mark stable commit / test-env-virtual (push) Has been cancelled
Mark stable commit / test-env-nix (push) Has been cancelled
Mark stable commit / test-e2e (push) Has been cancelled
Mark stable commit / test-virgin-user (push) Has been cancelled
Mark stable commit / test-virgin-root (push) Has been cancelled
Mark stable commit / mark-stable (push) Has been cancelled
**Replace main.py with module-based entry point and unify CLI execution**
* Remove legacy *main.py* and introduce *pkgmgr* module entry via *python -m pkgmgr*
* Add ***main**.py* as the canonical entry point delegating to the CLI
* Export *PYTHONPATH=src* in Makefile to ensure reliable imports in dev and CI
* Update setup scripts (venv & nix) to use module execution
* Refactor all E2E tests to execute the real module entry instead of file paths

This aligns pkgmgr with standard Python packaging practices and simplifies testing, setup, and execution across environments.

https://chatgpt.com/share/693c9056-716c-800f-b583-fc9245eab2b4
2025-12-12 22:59:46 +01:00

118 lines
3.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
End-to-end tests for the `pkgmgr path` command.
We verify two usage patterns:
1) pkgmgr path --all
- Should print the paths of all configured repositories.
2) pkgmgr path pkgmgr
- Should print the path for the repository identified as "pkgmgr".
Both tests are considered successful if the command completes without
raising an exception and exits with code 0 (or no explicit exit code).
"""
from __future__ import annotations
import io
import runpy
import sys
import unittest
from contextlib import redirect_stdout
class TestPathCommandsE2E(unittest.TestCase):
def _run_pkgmgr_path(self, argv_tail: list[str]) -> str:
"""
Helper to run `pkgmgr path ...` via main.py and return stdout.
Args:
argv_tail: List of arguments that follow the "pkgmgr" executable,
e.g. ["path", "--all"] or ["path", "pkgmgr"].
Returns:
The captured stdout produced by the command.
Raises:
AssertionError if the command exits with a non-zero exit code.
"""
original_argv = sys.argv
cmd_repr = "pkgmgr " + " ".join(argv_tail)
buffer = io.StringIO()
try:
sys.argv = ["pkgmgr"] + argv_tail
try:
# Capture stdout while running the CLI entry point.
with redirect_stdout(buffer):
runpy.run_module("pkgmgr", run_name="__main__")
except SystemExit as exc:
# Determine the exit code (int or string)
exit_code = exc.code
if isinstance(exit_code, int):
numeric_code = exit_code
else:
try:
numeric_code = int(exit_code)
except (TypeError, ValueError):
numeric_code = None
# Treat SystemExit(0) as success.
if numeric_code == 0 or numeric_code is None:
return buffer.getvalue()
# Non-zero exit code → fail with helpful message.
raise AssertionError(
f"{cmd_repr!r} failed with exit code {exit_code!r}. "
"Scroll up to see the full pkgmgr output inside the container."
) from exc
finally:
sys.argv = original_argv
# No SystemExit raised → also treat as success.
return buffer.getvalue()
def test_path_all_repositories(self) -> None:
"""
Run: pkgmgr path --all
The test succeeds if the command exits successfully and prints
at least one non-empty line.
"""
output = self._run_pkgmgr_path(["path", "--all"])
lines = [line for line in output.splitlines() if line.strip()]
# We only assert that something was printed; we do not assume
# that repositories are already cloned on disk.
self.assertGreater(
len(lines),
0,
msg="Expected `pkgmgr path --all` to print at least one path.",
)
def test_path_single_pkgmgr(self) -> None:
"""
Run: pkgmgr path pkgmgr
The test succeeds if the command exits successfully and prints
at least one non-empty line (the resolved directory).
"""
output = self._run_pkgmgr_path(["path", "pkgmgr"])
lines = [line for line in output.splitlines() if line.strip()]
self.assertGreater(
len(lines),
0,
msg="Expected `pkgmgr path pkgmgr` to print at least one path.",
)
if __name__ == "__main__":
unittest.main()