# 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()