Refactor pkgmgr CLI into modular core and add E2E tests for config/release/make/tools (see ChatGPT conversation 2025-12-08 https://chatgpt.com/share/6936ffa5-4868-800f-ab63-6e367093adce)

This commit is contained in:
Kevin Veen-Birkenbach
2025-12-08 17:41:27 +01:00
parent ccf3b1aa3c
commit 0b96270f78
18 changed files with 1612 additions and 766 deletions

View File

@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration tests for the `pkgmgr config` command.
We only exercise non-interactive, read-only subcommands here:
- pkgmgr config show --all
- pkgmgr config show pkgmgr
Interactive or mutating subcommands like `add`, `edit`, `init`,
`delete`, `ignore` are intentionally not covered in E2E tests to keep
the CI environment non-interactive and side-effect free.
"""
from __future__ import annotations
import runpy
import sys
import unittest
def _run_pkgmgr_config(extra_args: list[str]) -> None:
"""
Run `pkgmgr config ...` with the given extra args.
Any non-zero SystemExit is treated as a test failure and turned into
an AssertionError with diagnostics.
"""
cmd_repr = "pkgmgr " + " ".join(extra_args)
original_argv = list(sys.argv)
try:
sys.argv = ["pkgmgr"] + extra_args
try:
runpy.run_module("main", run_name="__main__")
except SystemExit as exc:
code = exc.code if isinstance(exc.code, int) else str(exc.code)
if code != 0:
print("[TEST] SystemExit caught while running", cmd_repr)
print(f"[TEST] Exit code: {code}")
raise AssertionError(
f"{cmd_repr!r} failed with exit code {code}. "
"Scroll up to inspect the output printed before failure."
) from exc
# exit code 0 is success
finally:
sys.argv = original_argv
class TestIntegrationConfigCommands(unittest.TestCase):
"""
E2E tests for `pkgmgr config` subcommands.
"""
def test_config_show_all(self) -> None:
"""
Run: pkgmgr config show --all
"""
_run_pkgmgr_config(["config", "show", "--all"])
def test_config_show_pkgmgr(self) -> None:
"""
Run: pkgmgr config show pkgmgr
Uses 'pkgmgr' as the standard repository identifier.
"""
_run_pkgmgr_config(["config", "show", "pkgmgr"])
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration tests for the `pkgmgr make` command.
We exercise the wrapper around `make` using the pkgmgr repository as
the standard target, but only in --preview mode to avoid side effects.
"""
from __future__ import annotations
import os
import runpy
import sys
import unittest
from test_integration_version_commands import _load_pkgmgr_repo_dir
class TestIntegrationMakeCommands(unittest.TestCase):
"""
E2E tests for the pkgmgr `make` wrapper.
"""
@classmethod
def setUpClass(cls) -> None:
# Reuse the helper from the version tests to locate the pkgmgr repo
cls.pkgmgr_repo_dir = _load_pkgmgr_repo_dir()
def _run_pkgmgr_make(self, extra_args: list[str]) -> None:
"""
Run `pkgmgr make ...` with the given extra args, from inside
the pkgmgr repository.
Any non-zero exit code is treated as test failure.
"""
cmd_repr = "pkgmgr " + " ".join(extra_args)
original_argv = list(sys.argv)
original_cwd = os.getcwd()
try:
os.chdir(self.pkgmgr_repo_dir)
sys.argv = ["pkgmgr"] + extra_args
try:
runpy.run_module("main", run_name="__main__")
except SystemExit as exc:
code = exc.code if isinstance(exc.code, int) else str(exc.code)
if code != 0:
print("[TEST] SystemExit caught while running", cmd_repr)
print(f"[TEST] Working directory: {os.getcwd()}")
print(f"[TEST] Exit code: {code}")
raise AssertionError(
f"{cmd_repr!r} failed with exit code {code}. "
"Scroll up to inspect the output printed before failure."
) from exc
# exit code 0 is success
finally:
os.chdir(original_cwd)
sys.argv = original_argv
def test_make_install_pkgmgr_preview(self) -> None:
"""
Run: pkgmgr make pkgmgr install --preview
- 'pkgmgr' is used as the standard repository identifier.
- '--preview' ensures that no destructive make commands are
actually executed inside the container.
"""
self._run_pkgmgr_make(
["make", "install", "--preview", "pkgmgr"]
)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration tests for the `pkgmgr release` command.
We deliberately only test a *negative* path here, to avoid mutating
the real repositories (bumping versions, editing changelogs) during
CI runs.
The test verifies that:
- Calling `pkgmgr release` with a non-existent repository identifier
results in a non-zero exit code and a helpful error.
"""
from __future__ import annotations
import runpy
import sys
import unittest
class TestIntegrationReleaseCommand(unittest.TestCase):
"""
E2E tests for `pkgmgr release`.
"""
def _run_release_expect_failure(self) -> None:
cmd_repr = "pkgmgr release patch does-not-exist-xyz"
original_argv = list(sys.argv)
try:
sys.argv = [
"pkgmgr",
"release",
"patch",
"does-not-exist-xyz",
]
try:
runpy.run_module("main", run_name="__main__")
except SystemExit as exc:
code = exc.code if isinstance(exc.code, int) else str(exc.code)
# Hier wirklich verifizieren:
assert code != 0, f"{cmd_repr!r} unexpectedly succeeded with exit code 0"
print("[TEST] pkgmgr release failed as expected")
print(f"[TEST] Command : {cmd_repr}")
print(f"[TEST] Exit code : {code}")
else:
# Kein SystemExit -> auf jeden Fall falsch
raise AssertionError(
f"{cmd_repr!r} returned normally (expected non-zero exit)."
)
finally:
sys.argv = original_argv
def test_release_for_unknown_repo_fails_cleanly(self) -> None:
self._run_release_expect_failure()
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,95 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration tests for the "tools" commands:
- pkgmgr code
- pkgmgr explore
- pkgmgr terminal
These commands spawn external GUI tools (VS Code, Nautilus,
GNOME Terminal) which are usually not available in a headless CI
container. Therefore, the entire test class is skipped by default.
If you run the tests on a local desktop environment where these
commands exist and can be spawned, you can remove or modify the
@skip decorator.
"""
from __future__ import annotations
import os
import runpy
import sys
import unittest
from test_integration_version_commands import _load_pkgmgr_repo_dir
@unittest.skip(
"Requires GUI tools (code, nautilus, gnome-terminal) inside the "
"test environment; skipped by default in CI."
)
class TestIntegrationToolsCommands(unittest.TestCase):
"""
E2E tests for pkgmgr 'code', 'explore', and 'terminal' commands.
"""
@classmethod
def setUpClass(cls) -> None:
cls.pkgmgr_repo_dir = _load_pkgmgr_repo_dir()
def _run_pkgmgr_tools_command(self, extra_args: list[str]) -> None:
"""
Run a 'tools' style command (code/explore/terminal) for pkgmgr.
Any non-zero exit code is treated as a test failure.
"""
cmd_repr = "pkgmgr " + " ".join(extra_args)
original_argv = list(sys.argv)
original_cwd = os.getcwd()
try:
os.chdir(self.pkgmgr_repo_dir)
sys.argv = ["pkgmgr"] + extra_args
try:
runpy.run_module("main", run_name="__main__")
except SystemExit as exc:
code = exc.code if isinstance(exc.code, int) else str(exc.code)
if code != 0:
print("[TEST] SystemExit caught while running", cmd_repr)
print(f"[TEST] Working directory: {os.getcwd()}")
print(f"[TEST] Exit code: {code}")
raise AssertionError(
f"{cmd_repr!r} failed with exit code {code}. "
"Scroll up to inspect the output printed before failure."
) from exc
# exit code 0 is success
finally:
os.chdir(original_cwd)
sys.argv = original_argv
def test_code_workspace_for_pkgmgr(self) -> None:
"""
Run: pkgmgr code pkgmgr
"""
self._run_pkgmgr_tools_command(["code", "pkgmgr"])
def test_explore_pkgmgr(self) -> None:
"""
Run: pkgmgr explore pkgmgr
"""
self._run_pkgmgr_tools_command(["explore", "pkgmgr"])
def test_terminal_pkgmgr(self) -> None:
"""
Run: pkgmgr terminal pkgmgr
"""
self._run_pkgmgr_tools_command(["terminal", "pkgmgr"])
if __name__ == "__main__":
unittest.main()