The molecule-controller image gets a requirements file and lock of its own (ADR 0003), so the lock script takes the file to lock: requirements-dev.txt by default, as before, or requirements-molecule.txt. Any other argument is a usage error, so a typo cannot write a lock somewhere else. The new test runs the script against a stub interpreter, so it needs no network. Refs #477 Assisted-by: Claude Code
88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
# SPDX-FileCopyrightText: Defame1297 and contributors
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Tests for scripts/lock-requirements.sh with a stub interpreter in place of PyPI and pip-tools.
|
|
|
|
Run with: python3 -m unittest discover -s tests/scripts
|
|
"""
|
|
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
|
SCRIPT = REPO / "scripts" / "lock-requirements.sh"
|
|
|
|
# `-c` (the version check) passes; `-m venv <dir>` makes a venv whose pip does nothing and whose
|
|
# pip-compile records its arguments.
|
|
PYTHON_STUB = """#!/usr/bin/env bash
|
|
[ "$1" = "-c" ] && exit 0
|
|
[ "$1 $2" = "-m venv" ] || exit 1
|
|
mkdir -p "$3/bin"
|
|
printf '#!/bin/sh\\nexit 0\\n' > "$3/bin/pip"
|
|
printf '#!/bin/sh\\necho "$*" >> "$STUB_LOG"\\n' > "$3/bin/pip-compile"
|
|
chmod +x "$3/bin/pip" "$3/bin/pip-compile"
|
|
"""
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("bash"), "bash is required")
|
|
class LockRequirementsTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-lock-")).resolve()
|
|
self.addCleanup(shutil.rmtree, self.tmp, True)
|
|
(self.tmp / "scripts").mkdir()
|
|
shutil.copy(SCRIPT, self.tmp / "scripts")
|
|
(self.tmp / ".python-version").write_text("3.14\n")
|
|
for name in ("requirements-dev.txt", "requirements-molecule.txt"):
|
|
(self.tmp / name).write_text("a==1\n")
|
|
python = self.tmp / "python"
|
|
python.write_text(PYTHON_STUB)
|
|
python.chmod(0o755)
|
|
self.log = self.tmp / "log"
|
|
|
|
def lock(self, *args):
|
|
proc = subprocess.run(
|
|
["bash", str(self.tmp / "scripts" / "lock-requirements.sh"), *args],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
env={
|
|
**os.environ,
|
|
"KAMINO_PYTHON": str(self.tmp / "python"),
|
|
"STUB_LOG": str(self.log),
|
|
},
|
|
)
|
|
compiled = self.log.read_text() if self.log.exists() else ""
|
|
return proc.returncode, compiled, proc.stderr
|
|
|
|
def test_the_default_locks_the_dev_requirements(self):
|
|
code, compiled, err = self.lock()
|
|
self.assertEqual(code, 0, err)
|
|
self.assertIn(
|
|
"--output-file requirements-dev.lock requirements-dev.txt", compiled
|
|
)
|
|
self.assertNotIn("--upgrade", compiled)
|
|
|
|
def test_a_requirements_file_is_locked_next_to_itself(self):
|
|
code, compiled, err = self.lock("--upgrade", "requirements-molecule.txt")
|
|
self.assertEqual(code, 0, err)
|
|
self.assertIn(
|
|
"--upgrade --output-file requirements-molecule.lock requirements-molecule.txt",
|
|
compiled,
|
|
)
|
|
|
|
def test_a_file_that_is_not_a_root_requirements_file_is_refused(self):
|
|
for bad in ("requirements-none.txt", "setup.cfg", "../requirements-dev.txt"):
|
|
with self.subTest(bad=bad):
|
|
code, compiled, _err = self.lock(bad)
|
|
self.assertNotEqual(code, 0)
|
|
self.assertEqual(compiled, "")
|
|
|
|
def test_an_unknown_option_is_a_usage_error(self):
|
|
code, compiled, err = self.lock("--frobnicate")
|
|
self.assertEqual(code, 2)
|
|
self.assertIn("usage:", err)
|
|
self.assertEqual(compiled, "")
|