import signal import time import unittest from docs_review.mentions import doc_mentions class MentionsTest(unittest.TestCase): def test_link_relative_to_document_resolves(self): m = doc_mentions("See [ch5](book/05-forecast.qmd).", "docs/README.md") self.assertIn("docs/book/05-forecast.qmd", m.paths) m = doc_mentions("`../../R/forecast.R`", "docs/book/05-forecast.qmd") self.assertIn("R/forecast.R", m.paths) self.assertIn("forecast.R", m.basenames) def test_repo_rooted_path_is_kept(self): m = doc_mentions("Run scripts/build_x.R first.", "docs/README.md") self.assertIn("scripts/build_x.R", m.paths) self.assertIn("docs/scripts/build_x.R", m.paths) def test_escaping_the_repository_is_dropped(self): m = doc_mentions("`../../x.R`", "README.md") self.assertFalse(any(p.startswith("..") for p in m.paths)) def test_names_need_code_formatting(self): m = doc_mentions("We run `write_table()` and fit_trend( and run the model.", "x.md") self.assertEqual(m.names, frozenset({"write_table", "fit_trend"})) def test_short_names_ignored(self): m = doc_mentions("`run()` and `abc` and `abcd`", "x.md") self.assertEqual(m.names, frozenset({"abcd"})) def test_undecodable_bytes_do_not_crash(self): doc_mentions(b"caf\xe9 `R/a.R`".decode("utf-8", "replace"), "README.md") def test_many_parent_segments_return_quickly(self): def interrupt(*_): raise TimeoutError("path regex backtracked past 1 s") old = signal.signal(signal.SIGALRM, interrupt) signal.setitimer(signal.ITIMER_REAL, 1.0) try: start = time.monotonic() doc_mentions("../" * 40 + "x", "docs/README.md") doc_mentions("a/" * 40 + "../" * 40 + "y.R", "docs/README.md") self.assertLess(time.monotonic() - start, 1.0) finally: signal.setitimer(signal.ITIMER_REAL, 0) signal.signal(signal.SIGALRM, old) def test_dot_directories_still_resolve(self): m = doc_mentions("See `.gitea/workflows/review.yml` and ./R/a.R.", "README.md") self.assertIn(".gitea/workflows/review.yml", m.paths) self.assertIn("R/a.R", m.paths) def test_directory_tokens_in_backticks_or_link_targets(self): m = doc_mentions("| [`book/`](book/README.md) | the book |", "docs/README.md") self.assertIn("docs/book", m.dirs) m = doc_mentions("See [the scripts](../scripts/) and `docs/specs/`.", "docs/README.md") self.assertIn("scripts", m.dirs) self.assertIn("docs/specs", m.dirs) def test_directory_tokens_need_code_or_link_and_a_word(self): m = doc_mentions("and/or either/or, see book/ and `../` and [up](../)", "docs/README.md") self.assertEqual(m.dirs, frozenset()) if __name__ == "__main__": unittest.main()