Coverage for tests / ext / test_packagetoctree.py: 34%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-17 08:41 +0000

1"""Tests for lsst.sphinxutils.ext.packagetoctree (package-toctree and 

2module-toctree directives). 

3""" 

4 

5from __future__ import annotations 

6 

7from typing import IO 

8from unittest import TestCase 

9 

10import pytest 

11from bs4 import BeautifulSoup, Tag 

12from sphinx.application import Sphinx 

13from sphinx.util import logging 

14 

15from lsst.sphinxutils.ext.packagetoctree import _filter_index_pages 

16 

17 

18@pytest.mark.sphinx("html", testroot="packagetoctree") 

19def test_packagetoctree(app: Sphinx, status: IO[str], warning: IO[str]) -> None: 

20 """Test the packagetoctree extension on a test site.""" 

21 app.verbosity = 2 

22 logging.setup(app, status, warning) 

23 app.builder.build_all() 

24 

25 index_html = (app.outdir / "index.html").read_text() 

26 index_soup = BeautifulSoup(index_html, "html.parser") 

27 print(f"index.html: {app.outdir / 'index.html'}") 

28 

29 # Check the module toctree 

30 wrapper = index_soup.find("div", class_="module-toctree") 

31 if isinstance(wrapper, Tag): 

32 wrapper_entries = [ 

33 (item.text, item.a.get("href")) 

34 for item in wrapper.find_all("li") 

35 if isinstance(item, Tag) and item.a is not None 

36 ] 

37 else: 

38 wrapper_entries = [] 

39 # Note that `lsst.skipthis` is *not* present 

40 assert wrapper_entries == [ 

41 ("lsst.packageA", "modules/lsst.packageA/index.html"), 

42 ("lsst.packageB", "modules/lsst.packageB/index.html"), 

43 ] 

44 

45 # Check the EUPS package toctree 

46 wrapper = index_soup.find("div", class_="package-toctree") 

47 if isinstance(wrapper, Tag): 

48 wrapper_entries = [ 

49 (item.text, item.a.get("href")) 

50 for item in wrapper.find_all("li") 

51 if isinstance(item, Tag) and item.a is not None 

52 ] 

53 else: 

54 wrapper_entries = [] 

55 # Note that `skipthis` is *not* present 

56 assert wrapper_entries == [ 

57 ("A", "packages/A/index.html"), 

58 ("B", "packages/B/index.html"), 

59 ] 

60 

61 

62class TestFilterIndexPages(TestCase): 

63 """Test _filter_index_pages.""" 

64 

65 def test_filter_index_pages(self) -> None: 

66 """Test _filter_index_pages.""" 

67 docnames = [ 

68 "index", 

69 "basedir/A/index", 

70 "basedir/B/index", 

71 "basedir/B/subdir/indexotherdir/C/index", 

72 ] 

73 expected = [ 

74 "basedir/A/index", 

75 "basedir/B/index", 

76 ] 

77 assert set(expected) == set(_filter_index_pages(docnames, "basedir"))