diff --git a/Makefile b/Makefile index ec5c49b..8c99527 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ test: @python3 tools/check-code-test.py @python3 tools/check-claims-test.py @python3 tools/next-batch-test.py + @python3 tools/validate-refs-test.py duplicates: @python3 tools/check-duplicates.py --strict diff --git a/tools/validate-refs-test.py b/tools/validate-refs-test.py new file mode 100644 index 0000000..8dc8b71 --- /dev/null +++ b/tools/validate-refs-test.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Unit tests for tools/validate-refs.py""" + +import importlib.util +import unittest +from pathlib import Path +from unittest.mock import MagicMock, patch + +TOOLS_DIR = Path(__file__).resolve().parent +spec = importlib.util.spec_from_file_location("validate_refs", TOOLS_DIR / "validate-refs.py") +validate_refs = importlib.util.module_from_spec(spec) +spec.loader.exec_module(validate_refs) + + +class TestValidateRefs(unittest.TestCase): + def test_clean(self): + self.assertEqual(validate_refs.clean("https://example.com/foo."), "https://example.com/foo") + self.assertEqual(validate_refs.clean("https://example.com/foo,;:!?"), "https://example.com/foo") + self.assertEqual(validate_refs.clean("https://example.com/foo)"), "https://example.com/foo") + self.assertEqual(validate_refs.clean("https://example.com/foo(bar)"), "https://example.com/foo(bar)") + + def test_strip_fences(self): + text = "line1\n```py\nhttps://example.com\n```\nline2" + stripped = validate_refs.strip_fences(text) + self.assertIn("line1", stripped) + self.assertIn("line2", stripped) + self.assertNotIn("https://example.com", stripped) + + def test_probe_success(self): + mock_response = MagicMock() + mock_response.status = 200 + mock_response.__enter__.return_value = mock_response + + with patch("urllib.request.urlopen", return_value=mock_response): + url, status = validate_refs.probe("https://example.com", timeout=5) + self.assertEqual(url, "https://example.com") + self.assertEqual(status, 200) + + +if __name__ == "__main__": + unittest.main()