From 9a9ac39ad4a865ce06daa1d8c0488c6f5ef92cab Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 30 Nov 2026 19:20:37 +0000 Subject: [PATCH] chore(test): run the container test bodies from files instead of inline bash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No code in code for the container tests (operator denial d8b0c6c8; operator choice 13bec2bf: swarm funnel; fits Quality req 018 clean.embedded-code). The five host scripts no longer embed an in-container script as bash -lc; each runs a file with bash -l: test-unit.sh and test-integration.sh call scripts/test/container/unittest.sh with tests/unit or tests/integration, test-e2e.sh calls container/e2e.sh, test-env-virtual.sh calls container/env-virtual.sh, test-env-nix.sh calls container/env-nix.sh with IMAGE and PKGMGR_DISTRO as arguments. Commands, order, strict-mode lines, env vars, volumes and workdir are unchanged. | 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | chore/test-scripts-no-inline | 9 files +116/-127 | 90 % | 4 % | – | – | 100 % | 86 | ✅ 90/5, tokens and velocity <= 100, security 100 % | Evidence: shellcheck -x clean on all 9 files, no bash -lc block left under scripts/, MEDUSA 100 % critical 0 (2 SC1091 scanner artefacts accepted). Break 4 %: unproven until PR CI runs the containers. Operator review a493a79a (re-card of f877c5dd). Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- scripts/test/container/e2e.sh | 37 +++++++++++++++++++++ scripts/test/container/env-nix.sh | 46 +++++++++++++++++++++++++++ scripts/test/container/env-virtual.sh | 16 ++++++++++ scripts/test/container/unittest.sh | 12 +++++++ scripts/test/test-e2e.sh | 46 +-------------------------- scripts/test/test-env-nix.sh | 46 +-------------------------- scripts/test/test-env-virtual.sh | 20 +----------- scripts/test/test-integration.sh | 10 +----- scripts/test/test-unit.sh | 10 +----- 9 files changed, 116 insertions(+), 127 deletions(-) create mode 100755 scripts/test/container/e2e.sh create mode 100755 scripts/test/container/env-nix.sh create mode 100755 scripts/test/container/env-virtual.sh create mode 100755 scripts/test/container/unittest.sh diff --git a/scripts/test/container/e2e.sh b/scripts/test/container/e2e.sh new file mode 100755 index 0000000..d8df18e --- /dev/null +++ b/scripts/test/container/e2e.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [ -f /etc/os-release ]; then + # shellcheck source=/dev/null + . /etc/os-release +fi + +echo "Running tests inside distro: ${ID:-unknown}" + +if [ -f "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ]; then + # shellcheck source=/dev/null + . "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" +fi + +if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then + # shellcheck source=/dev/null + . "$HOME/.nix-profile/etc/profile.d/nix.sh" +fi + +PATH="/nix/var/nix/profiles/default/bin:$HOME/.nix-profile/bin:$PATH" + +command -v nix >/dev/null || { + echo "ERROR: nix not found." + exit 1 +} + +if command -v git >/dev/null 2>&1; then + git config --global --add safe.directory /opt/src/pkgmgr || true + git config --global --add safe.directory /opt/src/pkgmgr/.git || true + git config --global --add safe.directory "*" || true +fi + +nix develop .#default --no-write-lock-file -c \ + python3 -m unittest discover \ + -s /opt/src/pkgmgr/tests/e2e \ + -p "$TEST_PATTERN" diff --git a/scripts/test/container/env-nix.sh b/scripts/test/container/env-nix.sh new file mode 100755 index 0000000..133883f --- /dev/null +++ b/scripts/test/container/env-nix.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Param: $1 IMAGE, the image name shown in the missing-nix error. +# Param: $2 PKGMGR_DISTRO, the distro shown in the missing-nix error. +IMAGE="$1" +PKGMGR_DISTRO="$2" + +if command -v git >/dev/null 2>&1; then + git config --global --add safe.directory /opt/src/pkgmgr || true + git config --global --add safe.directory /opt/src/pkgmgr/.git || true + git config --global --add safe.directory "*" || true +fi + +echo ">>> preflight: nix must exist in image" +if ! command -v nix >/dev/null 2>&1; then + echo "NO_NIX" + echo "ERROR: nix not found in image ${IMAGE} (PKGMGR_DISTRO=${PKGMGR_DISTRO})" + echo "HINT: Ensure Nix is installed during image build for this distro." + exit 1 +fi + +echo ">>> nix version" +nix --version + +if [[ -f /opt/src/pkgmgr/scripts/nix/lib/retry_403.sh ]]; then + # shellcheck source=SCRIPTDIR/../../nix/lib/retry_403.sh + source /opt/src/pkgmgr/scripts/nix/lib/retry_403.sh +elif [[ -f ./scripts/nix/lib/retry_403.sh ]]; then + # shellcheck source=SCRIPTDIR/../../nix/lib/retry_403.sh + source ./scripts/nix/lib/retry_403.sh +else + echo "ERROR: retry helper not found: scripts/nix/lib/retry_403.sh" + exit 1 +fi + +echo ">>> nix flake show" +run_with_github_403_retry nix flake show . --no-write-lock-file >/dev/null + +echo ">>> nix build .#default" +run_with_github_403_retry nix build .#default --no-link --no-write-lock-file + +echo ">>> nix run .#pkgmgr -- --help" +run_with_github_403_retry nix run .#pkgmgr -- --help --no-write-lock-file + +echo ">>> OK: Nix flake-only test succeeded." diff --git a/scripts/test/container/env-virtual.sh b/scripts/test/container/env-virtual.sh new file mode 100755 index 0000000..a97a9b5 --- /dev/null +++ b/scripts/test/container/env-virtual.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "[test-env-virtual] Installing pkgmgr (distro package)..." +make install + +echo "[test-env-virtual] Setting up Python venv..." +make setup-venv + +echo "[test-env-virtual] Activating venv..." +# shellcheck source=/dev/null +. "$HOME/.venvs/pkgmgr/bin/activate" + +echo "[test-env-virtual] Using pkgmgr from:" +command -v pkgmgr +pkgmgr --help diff --git a/scripts/test/container/unittest.sh b/scripts/test/container/unittest.sh new file mode 100755 index 0000000..ff2b212 --- /dev/null +++ b/scripts/test/container/unittest.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -e + +# Param: $1 the test folder to discover, tests/unit or tests/integration. +suite="$1" + +git config --global --add safe.directory /opt/src/pkgmgr || true +nix develop .#default --no-write-lock-file -c \ + python3 -m unittest discover \ + -s "$suite" \ + -t /opt/src/pkgmgr \ + -p "$TEST_PATTERN" diff --git a/scripts/test/test-e2e.sh b/scripts/test/test-e2e.sh index e387391..77194c1 100755 --- a/scripts/test/test-e2e.sh +++ b/scripts/test/test-e2e.sh @@ -14,48 +14,4 @@ docker run --rm \ -e NIX_CONFIG="${NIX_CONFIG}" \ --workdir /opt/src/pkgmgr \ "pkgmgr-${PKGMGR_DISTRO}" \ - bash -lc ' - set -euo pipefail - - # Load distro info - if [ -f /etc/os-release ]; then - . /etc/os-release - fi - - echo "Running tests inside distro: ${ID:-unknown}" - - # Load Nix environment if available - if [ -f "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ]; then - . "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" - fi - - if [ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then - . "$HOME/.nix-profile/etc/profile.d/nix.sh" - fi - - PATH="/nix/var/nix/profiles/default/bin:$HOME/.nix-profile/bin:$PATH" - - command -v nix >/dev/null || { - echo "ERROR: nix not found." - exit 1 - } - - # Mark the mounted repository as safe to avoid Git ownership errors. - # Newer Git (e.g. on Ubuntu) complains about the gitdir (/opt/src/pkgmgr/.git), - # older versions about the worktree (/opt/src/pkgmgr). Nix turns "." into the - # flake input "git+file:///opt/src/pkgmgr", which then uses Git under the hood. - if command -v git >/dev/null 2>&1; then - # Worktree path - git config --global --add safe.directory /opt/src/pkgmgr || true - # Gitdir path shown in the "dubious ownership" error - git config --global --add safe.directory /opt/src/pkgmgr/.git || true - # Ephemeral CI containers: allow all paths as a last resort - git config --global --add safe.directory "*" || true - fi - - # Run the E2E tests inside the Nix development shell - nix develop .#default --no-write-lock-file -c \ - python3 -m unittest discover \ - -s /opt/src/pkgmgr/tests/e2e \ - -p "$TEST_PATTERN" - ' + bash -l scripts/test/container/e2e.sh diff --git a/scripts/test/test-env-nix.sh b/scripts/test/test-env-nix.sh index 0014d5d..d2777bb 100755 --- a/scripts/test/test-env-nix.sh +++ b/scripts/test/test-env-nix.sh @@ -16,48 +16,4 @@ docker run --rm \ -e REINSTALL_PKGMGR=1 \ -e NIX_CONFIG="${NIX_CONFIG}" \ "${IMAGE}" \ - bash -lc ' - set -euo pipefail - - if command -v git >/dev/null 2>&1; then - git config --global --add safe.directory /opt/src/pkgmgr || true - git config --global --add safe.directory /opt/src/pkgmgr/.git || true - git config --global --add safe.directory "*" || true - fi - - echo ">>> preflight: nix must exist in image" - if ! command -v nix >/dev/null 2>&1; then - echo "NO_NIX" - echo "ERROR: nix not found in image '"${IMAGE}"' (PKGMGR_DISTRO='"${PKGMGR_DISTRO}"')" - echo "HINT: Ensure Nix is installed during image build for this distro." - exit 1 - fi - - echo ">>> nix version" - nix --version - - # ------------------------------------------------------------ - # Retry helper for GitHub API rate-limit (HTTP 403) - # ------------------------------------------------------------ - if [[ -f /opt/src/pkgmgr/scripts/nix/lib/retry_403.sh ]]; then - # shellcheck source=./scripts/nix/lib/retry_403.sh - source /opt/src/pkgmgr/scripts/nix/lib/retry_403.sh - elif [[ -f ./scripts/nix/lib/retry_403.sh ]]; then - # shellcheck source=./scripts/nix/lib/retry_403.sh - source ./scripts/nix/lib/retry_403.sh - else - echo "ERROR: retry helper not found: scripts/nix/lib/retry_403.sh" - exit 1 - fi - - echo ">>> nix flake show" - run_with_github_403_retry nix flake show . --no-write-lock-file >/dev/null - - echo ">>> nix build .#default" - run_with_github_403_retry nix build .#default --no-link --no-write-lock-file - - echo ">>> nix run .#pkgmgr -- --help" - run_with_github_403_retry nix run .#pkgmgr -- --help --no-write-lock-file - - echo ">>> OK: Nix flake-only test succeeded." - ' + bash -l scripts/test/container/env-nix.sh "${IMAGE}" "${PKGMGR_DISTRO}" diff --git a/scripts/test/test-env-virtual.sh b/scripts/test/test-env-virtual.sh index 0c8aefc..4e6381a 100755 --- a/scripts/test/test-env-virtual.sh +++ b/scripts/test/test-env-virtual.sh @@ -12,31 +12,13 @@ echo "[test-env-virtual] Inspect image metadata:" docker image inspect "${IMAGE}" | sed -n '1,40p' echo -# ------------------------------------------------------------ -# Run VENV-based pkgmgr test inside container -# ------------------------------------------------------------ if OUTPUT=$(docker run --rm \ -e REINSTALL_PKGMGR=1 \ -v "$(pwd):/opt/src/pkgmgr" \ -w /opt/src/pkgmgr \ -e NIX_CONFIG="${NIX_CONFIG}" \ "${IMAGE}" \ - bash -lc ' - set -euo pipefail - - echo "[test-env-virtual] Installing pkgmgr (distro package)..." - make install - - echo "[test-env-virtual] Setting up Python venv..." - make setup-venv - - echo "[test-env-virtual] Activating venv..." - . "$HOME/.venvs/pkgmgr/bin/activate" - - echo "[test-env-virtual] Using pkgmgr from:" - command -v pkgmgr - pkgmgr --help - ' 2>&1); then + bash -l scripts/test/container/env-virtual.sh 2>&1); then echo "$OUTPUT" echo diff --git a/scripts/test/test-integration.sh b/scripts/test/test-integration.sh index 3cf4828..6febb98 100755 --- a/scripts/test/test-integration.sh +++ b/scripts/test/test-integration.sh @@ -14,12 +14,4 @@ docker run --rm \ -e TEST_PATTERN="${TEST_PATTERN}" \ -e NIX_CONFIG="${NIX_CONFIG}" \ "pkgmgr-${PKGMGR_DISTRO}" \ - bash -lc ' - set -e; - git config --global --add safe.directory /opt/src/pkgmgr || true; - nix develop .#default --no-write-lock-file -c \ - python3 -m unittest discover \ - -s tests/integration \ - -t /opt/src/pkgmgr \ - -p "$TEST_PATTERN"; - ' + bash -l scripts/test/container/unittest.sh tests/integration diff --git a/scripts/test/test-unit.sh b/scripts/test/test-unit.sh index 6cfedc6..8d4406c 100755 --- a/scripts/test/test-unit.sh +++ b/scripts/test/test-unit.sh @@ -14,12 +14,4 @@ docker run --rm \ -e TEST_PATTERN="${TEST_PATTERN}" \ -e NIX_CONFIG="${NIX_CONFIG}" \ "pkgmgr-${PKGMGR_DISTRO}" \ - bash -lc ' - set -e; - git config --global --add safe.directory /opt/src/pkgmgr || true; - nix develop .#default --no-write-lock-file -c \ - python3 -m unittest discover \ - -s tests/unit \ - -t /opt/src/pkgmgr \ - -p "$TEST_PATTERN"; - ' + bash -l scripts/test/container/unittest.sh tests/unit