|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from sphinxnotes.recentupdate import path2doc |
| 5 | + |
| 6 | + |
| 7 | +def _make_repo(working_dir='/srv/docs'): |
| 8 | + repo = MagicMock() |
| 9 | + repo.working_dir = working_dir |
| 10 | + return repo |
| 11 | + |
| 12 | + |
| 13 | +def _make_env(path2doc_impl): |
| 14 | + env = MagicMock() |
| 15 | + env.path2doc = path2doc_impl |
| 16 | + return env |
| 17 | + |
| 18 | + |
| 19 | +class TestPath2Doc(unittest.TestCase): |
| 20 | + def test_root_doc_gets_leading_slash(self): |
| 21 | + """env.path2doc returns 'index' -> result should be '/index'.""" |
| 22 | + repo = _make_repo('/srv/docs') |
| 23 | + env = _make_env(lambda p: 'index') |
| 24 | + self.assertEqual(path2doc(repo, env, 'index.rst'), '/index') |
| 25 | + |
| 26 | + def test_nested_doc_gets_leading_slash(self): |
| 27 | + """env.path2doc returns 'subdir/page' -> result should be '/subdir/page'.""" |
| 28 | + repo = _make_repo('/srv/docs') |
| 29 | + env = _make_env(lambda p: 'subdir/page') |
| 30 | + self.assertEqual(path2doc(repo, env, 'subdir/page.rst'), '/subdir/page') |
| 31 | + |
| 32 | + def test_returns_none_when_env_returns_none(self): |
| 33 | + """env.path2doc returns None (not a doc file) -> should return None.""" |
| 34 | + repo = _make_repo('/srv/docs') |
| 35 | + env = _make_env(lambda p: None) |
| 36 | + self.assertIsNone(path2doc(repo, env, 'image.png')) |
| 37 | + |
| 38 | + def test_returns_none_when_env_returns_abs_path(self): |
| 39 | + """env.path2doc returns an absolute path (e.g. excluded file) -> should return None.""" |
| 40 | + repo = _make_repo('/srv/docs') |
| 41 | + env = _make_env(lambda p: '/excluded/file') |
| 42 | + self.assertIsNone(path2doc(repo, env, 'excluded/file')) |
| 43 | + |
| 44 | + def test_passes_joined_path_to_env(self): |
| 45 | + """blob_path is joined with repo.working_dir before calling env.path2doc.""" |
| 46 | + repo = _make_repo('/srv/docs') |
| 47 | + env = MagicMock() |
| 48 | + env.path2doc = MagicMock(return_value=None) |
| 49 | + path2doc(repo, env, 'subdir/page.rst') |
| 50 | + env.path2doc.assert_called_once_with('/srv/docs/subdir/page.rst') |
0 commit comments