Files
Kamino/tests/scripts/test_check_requirements_lock.py
T
Dragonish 1184938f1e
Build / build (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 1m37s
Molecule / molecule (pull_request) Successful in 5m26s
feat(ci): pin hook revisions by SHA and install dev tools from a hashed lock
Refs #113.

- .pre-commit-config.yaml: every hook repo is pinned by commit SHA with the release in a
  `# frozen:` comment, so a moved tag cannot change the code a hook environment is built from.
- requirements-dev.lock: hashed lock of requirements-dev.txt, installed with
  `pip install --require-hashes` by scripts/dev-setup.sh, the ci image and the fallback
  install of the Lint and Build workflows.
- scripts/lock-requirements.sh regenerates the lock; scripts/check_requirements_lock.py
  (offline, a pre-commit hook) fails when it drifts from the direct pins.
- check_bot_pr_diff.py accepts a `rev:` line with a `# frozen:` comment and nothing else.
- ci image 0.2.16 -> 0.2.17 (image inputs changed).
2026-11-14 15:14:39 +00:00

108 lines
3.6 KiB
Python

# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Run with: python3 -m unittest discover -s tests/scripts"""
import pathlib
import sys
import tempfile
import unittest
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
sys.path.insert(0, str(SCRIPTS))
import check_requirements_lock as crl
H = "a" * 64
G = "b" * 64
TXT = "# pins\nansible-core==2.22.0\nRuamel.Yaml==0.18.0\n"
LOCK = (
"#\n# autogenerated\n#\n"
f"ansible-core==2.22.0 \\\n --hash=sha256:{H} \\\n --hash=sha256:{G}\n"
" # via -r requirements-dev.txt\n"
f"ruamel-yaml==0.18.0 \\\n --hash=sha256:{H}\n"
)
class CheckTest(unittest.TestCase):
def test_matching_lock_is_clean(self):
self.assertEqual(crl.check(TXT, LOCK), [])
def test_name_spelling_is_normalised(self):
self.assertEqual(crl.canonical("Ruamel.Yaml"), "ruamel-yaml")
def test_missing_direct_pin(self):
findings = crl.check(TXT + "reuse==6.3.0\n", LOCK)
self.assertTrue(any("reuse==6.3.0" in f and "missing" in f for f in findings))
def test_version_drift(self):
findings = crl.check(TXT.replace("2.22.0", "2.22.1"), LOCK)
self.assertTrue(
any("2.22.1 in requirements-dev.txt but 2.22.0" in f for f in findings)
)
def test_entry_without_hash(self):
findings = crl.check(TXT, LOCK + "evil==1.0\n")
self.assertTrue(any("evil==1.0 has no --hash" in f for f in findings))
def test_non_pin_lines_are_refused(self):
for line in (
"evil @ https://x/y.tgz",
"-e .",
"--index-url https://evil/simple",
"evil>=1",
):
with self.subTest(line=line):
findings = crl.check(TXT, LOCK + line + "\n")
self.assertTrue(
any("not a `name==version` entry" in f for f in findings)
)
def test_hash_before_any_package(self):
findings = crl.check(TXT, f" --hash=sha256:{H}\n" + LOCK)
self.assertTrue(any("no package before it" in f for f in findings))
def test_short_hash_is_refused(self):
findings = crl.check(TXT, LOCK.replace(G, "b" * 63))
self.assertTrue(findings)
def test_duplicate_entry(self):
findings = crl.check(
TXT, LOCK + f"ansible-core==2.22.0 \\\n --hash=sha256:{H}\n"
)
self.assertTrue(any("listed twice" in f for f in findings))
def test_environment_marker_is_allowed(self):
lock = (
LOCK
+ f'colorama==0.4.6 ; sys_platform == "win32" \\\n --hash=sha256:{H}\n'
)
self.assertEqual(crl.check(TXT, lock), [])
def test_the_repository_lock_matches_its_pins(self):
root = SCRIPTS.parent
self.assertEqual(
crl.check(
(root / "requirements-dev.txt").read_text(encoding="utf-8"),
(root / "requirements-dev.lock").read_text(encoding="utf-8"),
),
[],
)
class MainTest(unittest.TestCase):
def test_exit_codes(self):
with tempfile.TemporaryDirectory() as tmp:
txt, lock = pathlib.Path(tmp, "r.txt"), pathlib.Path(tmp, "r.lock")
txt.write_text(TXT)
lock.write_text(LOCK)
self.assertEqual(crl.main(["x", str(txt), str(lock)]), 0)
lock.write_text("")
self.assertEqual(crl.main(["x", str(txt), str(lock)]), 1)
self.assertEqual(
crl.main(["x", str(txt), str(pathlib.Path(tmp, "none"))]), 1
)
self.assertEqual(crl.main(["x", "only-one"]), 2)
if __name__ == "__main__":
unittest.main()