Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions tools/validate-refs-test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading