#!/usr/bin/env python3 """Offline contract tests: never invoke the real publisher or use real tokens.""" import os import json from pathlib import Path import shutil import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] class ReleaseWrapper(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory(prefix="fader release tests ") self.addCleanup(self.temp.cleanup) self.base = Path(self.temp.name) self.repo = self.base / "repository with spaces" self.repo.mkdir() shutil.copy(ROOT / "release.sh", self.repo) shutil.copy(ROOT / "rust-release.toml", self.repo) self.bin = self.base / "bin" self.bin.mkdir() # Restricted PATH also makes the missing-publisher case deterministic. for command in ("dirname", "jq"): (self.bin / command).symlink_to(shutil.which(command)) self.script("cargo", '''#!/bin/sh test "${GITEA_TOKEN+x}" != x || exit 98 test "$*" = "metadata --offline --no-deps --format-version 1 --manifest-path $EXPECTED_REPO/Cargo.toml" || exit 99 printf '{"packages":[{"name":"fader_master","version":"%s"}]}\n' "$TEST_VERSION" ''') self.script("rust_realease", '''#!/bin/sh test "$GITEA_TOKEN" = "fixture-secret" || exit 91 test "$1" = "$EXPECTED_REPO" || exit 92 test "$2" = --package && test "$3" = fader_master || exit 93 test "$4" = --artifact-format && test "$5" = both || exit 94 test -f "$1/rust-release.toml" || exit 95 shift 5 printf '<%s>\n' "$@" exit "$TEST_EXIT" ''') self.token = self.base / "token with spaces" self.token.write_text("fixture-secret\nignored second line\n") self.env = dict(os.environ, PATH=str(self.bin), EXPECTED_REPO=str(self.repo), TEST_VERSION="0.1.0", TEST_EXIT="0", GITEA_TOKEN="inherited-token") def script(self, name, content): path = self.bin / name path.write_text(content) path.chmod(0o755) def run_wrapper(self, *args): result = subprocess.run(["/bin/sh", str(self.repo / "release.sh"), *map(str, args)], cwd=self.base, env=self.env, text=True, capture_output=True) self.assertNotIn("fixture-secret", result.stdout + result.stderr) return result def test_stable_and_exact_forwarding(self): result = self.run_wrapper(self.token, "--dry-run", "option with spaces", "*literal*") self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout, "<--dry-run>\n