ci(python): one Python (3.14) for the lock, a clone and the ci image
.python-version (3.14) is the source. dev-setup.sh and lock-requirements.sh require exactly that minor; the ci image builds /opt/venv from its baked python-build-standalone of that version (ARG PYTHON_VERSION) instead of the distribution's python3, and derives the python<MINOR> links for ansible-test from it; `check_image_versions.py pins` fails when .python-version, the lock header or an `ARG PYTHON_VERSION` differs. The lock already carries the 3.14 header (Renovate's -full container runs pip-compile on its own python 3.14.8); renovate.yml now says that python's minor version must equal .python-version. ci image 0.2.25 -> 0.2.26. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
1 parent
3a777ac015
commit
b0607a39e2
12 files changed
+285
-43
No files matched your search
@@ -559,6 +559,96 @@ class PureTest(unittest.TestCase):
|
||||
) # backwards
|
||||
|
||||
|
||||
class PythonPinTest(unittest.TestCase):
|
||||
LOCK = "#\n# This file is autogenerated by pip-compile with Python 3.13\n# by the following command:\n"
|
||||
DOCKERFILE = "FROM x\nARG PYTHON_VERSION=3.13\nRUN true\n"
|
||||
|
||||
def check(self, pin="3.13\n", lock=None, dockerfiles=None):
|
||||
return civ.check_python_pin(
|
||||
pin,
|
||||
self.LOCK if lock is None else lock,
|
||||
{"images/ci/Dockerfile": self.DOCKERFILE}
|
||||
if dockerfiles is None
|
||||
else dockerfiles,
|
||||
)
|
||||
|
||||
def test_agreeing_files_pass(self):
|
||||
self.assertEqual(self.check(), [])
|
||||
|
||||
def test_pin_must_be_one_minor_version(self):
|
||||
for bad in ("", "3", "3.13.2", "python3.13", "3.13\n3.14"):
|
||||
errors = self.check(pin=bad)
|
||||
self.assertEqual(len(errors), 1, bad)
|
||||
self.assertIn(".python-version must hold", errors[0])
|
||||
|
||||
def test_lock_header_must_match(self):
|
||||
errors = self.check(lock=self.LOCK.replace("3.13", "3.14"))
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertIn("header says Python 3.14", errors[0])
|
||||
self.assertIn("lock-requirements.sh", errors[0])
|
||||
|
||||
def test_lock_without_header_is_an_error(self):
|
||||
errors = self.check(lock="six==1.0 \\\n")
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertIn("no 'autogenerated by pip-compile", errors[0])
|
||||
|
||||
def test_dockerfile_arg_must_match(self):
|
||||
errors = self.check(dockerfiles={"d": self.DOCKERFILE.replace("3.13", "3.14")})
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertIn("ARG PYTHON_VERSION=3.14", errors[0])
|
||||
|
||||
def test_some_dockerfile_must_carry_the_arg(self):
|
||||
for dockerfiles in ({}, {"d": "FROM x\n"}):
|
||||
errors = self.check(dockerfiles=dockerfiles)
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertIn("no image Dockerfile has an ARG PYTHON_VERSION=", errors[0])
|
||||
|
||||
def test_arg_spellings_are_all_held_to_the_pin(self):
|
||||
for line in (
|
||||
"ARG PYTHON_VERSION=3.14",
|
||||
" ARG PYTHON_VERSION=3.14",
|
||||
"arg PYTHON_VERSION=3.14",
|
||||
'ARG PYTHON_VERSION="3.14"',
|
||||
"ARG PYTHON_VERSION='3.14'",
|
||||
"ARG PYTHON_VERSION=3.14 # moved to 3.14",
|
||||
"ARG PYTHON_VERSION=3.14\r",
|
||||
):
|
||||
errors = self.check(dockerfiles={"d": f"FROM x\n{line}\n"})
|
||||
self.assertEqual(len(errors), 1, line)
|
||||
self.assertIn("ARG PYTHON_VERSION=3.14 but", errors[0], line)
|
||||
|
||||
def test_one_disagreeing_dockerfile_fails_even_when_another_agrees(self):
|
||||
errors = self.check(
|
||||
dockerfiles={
|
||||
"a": self.DOCKERFILE,
|
||||
"b": self.DOCKERFILE.replace("3.13", "3.12"),
|
||||
}
|
||||
)
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertTrue(errors[0].startswith("b:"))
|
||||
|
||||
def test_both_arg_lines_of_a_multi_stage_dockerfile_are_checked(self):
|
||||
two = "FROM x AS a\nARG PYTHON_VERSION=3.13\nFROM x AS b\nARG PYTHON_VERSION=3.12\n"
|
||||
errors = self.check(dockerfiles={"d": two})
|
||||
self.assertEqual(len(errors), 1)
|
||||
self.assertIn("ARG PYTHON_VERSION=3.12", errors[0])
|
||||
|
||||
def test_the_committed_files_agree(self):
|
||||
root = pathlib.Path(__file__).resolve().parents[2]
|
||||
self.assertEqual(
|
||||
civ.check_python_pin(
|
||||
(root / ".python-version").read_text(),
|
||||
(root / "requirements-dev.lock").read_text(),
|
||||
{"images/ci/Dockerfile": (root / "images/ci/Dockerfile").read_text()},
|
||||
),
|
||||
[],
|
||||
)
|
||||
|
||||
def test_a_commented_arg_does_not_count(self):
|
||||
errors = self.check(dockerfiles={"d": "# ARG PYTHON_VERSION=3.13\n"})
|
||||
self.assertEqual(len(errors), 1)
|
||||
|
||||
|
||||
class GitTest(unittest.TestCase):
|
||||
"""Run the script as CI does, in a throwaway repository."""
|
||||
|
||||
@@ -573,7 +663,15 @@ class GitTest(unittest.TestCase):
|
||||
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
)
|
||||
self.write(
|
||||
"images/ci/Dockerfile", labelled("ci", CI_DOCKERFILE.split("\n", 1)[1])
|
||||
"images/ci/Dockerfile",
|
||||
labelled(
|
||||
"ci", CI_DOCKERFILE.split("\n", 1)[1] + "ARG PYTHON_VERSION=3.13\n"
|
||||
),
|
||||
)
|
||||
self.write(".python-version", "3.13\n")
|
||||
self.write(
|
||||
"requirements-dev.lock",
|
||||
"# This file is autogenerated by pip-compile with Python 3.13\n",
|
||||
)
|
||||
self.write("images/mol/Dockerfile", labelled("mol"))
|
||||
self.write("docs/images/ci.md", "ci\n")
|
||||
|
||||
@@ -21,8 +21,14 @@ from pathlib import Path
|
||||
SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "dev-setup.sh"
|
||||
|
||||
PYTHON_STUB = """#!/bin/sh
|
||||
# Stub interpreter: every -c check passes; `-m venv DIR` lays out DIR/bin with the stubs.
|
||||
if [ "$1" = "-c" ]; then exit 0; fi
|
||||
# Stub interpreter: a -c check passes, unless STUB_PY_MINOR is set and the check does not compare against
|
||||
# that minor version (the pinned-version check of dev-setup.sh); `-m venv DIR` lays out DIR/bin with the stubs.
|
||||
if [ "$1" = "-c" ]; then
|
||||
if [ -n "${STUB_PY_MINOR:-}" ]; then
|
||||
case "$2" in *"== '$STUB_PY_MINOR'"*) exit 0 ;; *) exit 1 ;; esac
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
if [ "$1" = "-m" ] && [ "$2" = "venv" ]; then
|
||||
mkdir -p "$3/bin"
|
||||
cp "$STUB_DIR/python" "$3/bin/python"
|
||||
@@ -113,6 +119,7 @@ class DevSetupTest(unittest.TestCase):
|
||||
repo = self.tmp / name
|
||||
(repo / "scripts").mkdir(parents=True)
|
||||
shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh")
|
||||
(repo / ".python-version").write_text("3.13\n")
|
||||
(repo / "requirements-dev.txt").write_text("")
|
||||
(repo / "requirements-dev.lock").write_text("")
|
||||
(repo / "requirements.yml").write_text("---\ncollections: []\n")
|
||||
@@ -144,6 +151,42 @@ class DevSetupTest(unittest.TestCase):
|
||||
def hook(self, repo: Path, name: str = "pre-commit") -> Path:
|
||||
return repo / ".git" / "hooks" / name
|
||||
|
||||
def test_a_python_other_than_the_pin_is_refused(self) -> None:
|
||||
repo = self.make_repo()
|
||||
wrong = self.stubs / "wrong-python"
|
||||
wrong.write_text("#!/bin/sh\nexit 1\n")
|
||||
wrong.chmod(0o755)
|
||||
proc = self.run_setup(repo, KAMINO_PYTHON=str(wrong))
|
||||
self.assertNotEqual(proc.returncode, 0)
|
||||
self.assertIn("is not Python 3.13 (.python-version)", proc.stderr)
|
||||
self.assertFalse((repo / ".venv").exists())
|
||||
|
||||
def test_an_interpreter_of_another_minor_is_refused_and_the_pinned_one_accepted(
|
||||
self,
|
||||
) -> None:
|
||||
repo = self.make_repo()
|
||||
proc = self.run_setup(repo, STUB_PY_MINOR="3.12")
|
||||
self.assertNotEqual(proc.returncode, 0)
|
||||
self.assertIn("is not Python 3.13 (.python-version)", proc.stderr)
|
||||
self.assertFalse((repo / ".venv").exists())
|
||||
proc = self.run_setup(repo, STUB_PY_MINOR="3.13")
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
self.assertTrue((repo / ".venv").is_dir())
|
||||
|
||||
def test_a_missing_pin_file_is_refused_with_a_message(self) -> None:
|
||||
repo = self.make_repo()
|
||||
(repo / ".python-version").unlink()
|
||||
proc = self.run_setup(repo)
|
||||
self.assertNotEqual(proc.returncode, 0)
|
||||
self.assertIn(".python-version is missing", proc.stderr)
|
||||
|
||||
def test_a_malformed_pin_file_is_refused(self) -> None:
|
||||
repo = self.make_repo()
|
||||
(repo / ".python-version").write_text("3.13.2\n")
|
||||
proc = self.run_setup(repo)
|
||||
self.assertNotEqual(proc.returncode, 0)
|
||||
self.assertIn(".python-version must hold one MAJOR.MINOR", proc.stderr)
|
||||
|
||||
def test_normal_run(self) -> None:
|
||||
repo = self.make_repo()
|
||||
result = self.run_setup(repo)
|
||||
|
||||
Reference in new issue
Block a user