60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
"""Offline regression checks for release-maintenance failure handling."""
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import dependency_notices as notices
|
|
|
|
|
|
class NoticeTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temp.cleanup)
|
|
self.root = Path(self.temp.name)
|
|
(self.root / "tools").mkdir()
|
|
(self.root / "package").mkdir()
|
|
(self.root / "tools/license_supplements.json").write_text("{}")
|
|
self.metadata = json.dumps({"workspace_members": [], "packages": [{
|
|
"id": "example", "name": "example", "version": "1.0.0", "license": "MIT",
|
|
"manifest_path": str(self.root / "package/Cargo.toml"),
|
|
}]}).encode()
|
|
self.patch = patch.object(notices, "ROOT", self.root)
|
|
self.patch.start()
|
|
self.addCleanup(self.patch.stop)
|
|
|
|
def test_missing_license_fails(self):
|
|
with patch.object(notices.subprocess, "check_output", return_value=self.metadata):
|
|
with self.assertRaisesRegex(RuntimeError, "Missing license"):
|
|
notices.generate()
|
|
|
|
def test_license_text_is_preserved(self):
|
|
original = "Copyright Example \n\nMIT license text\n"
|
|
(self.root / "package/LICENSE").write_text(original)
|
|
with patch.object(notices.subprocess, "check_output", return_value=self.metadata):
|
|
outputs = notices.generate()
|
|
self.assertIn(original, outputs[self.root / "docs/third-party-licenses.txt"])
|
|
self.assertFalse((self.root / "THIRD_PARTY_NOTICES.md").exists())
|
|
|
|
def test_supplement_checksum_mismatch_fails(self):
|
|
(self.root / "tools/license_supplements.json").write_text(json.dumps({
|
|
"example@1.0.0": {"url": "https://example.invalid/LICENSE", "sha256": "0" * 64}
|
|
}))
|
|
with patch.object(notices.subprocess, "check_output", side_effect=[self.metadata, b"bad"]):
|
|
with self.assertRaisesRegex(RuntimeError, "checksum mismatch"):
|
|
notices.generate()
|
|
|
|
def test_check_does_not_overwrite_outdated_file(self):
|
|
path = self.root / "THIRD_PARTY_NOTICES.md"
|
|
path.write_text("old")
|
|
with patch.object(notices, "generate", return_value={path: "new"}):
|
|
with patch("sys.argv", ["dependency_notices.py", "--check"]):
|
|
with self.assertRaisesRegex(SystemExit, "Outdated notices"):
|
|
notices.main()
|
|
self.assertEqual(path.read_text(), "old")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|