|
| 1 | +""" |
| 2 | +Basic extension tests. |
| 3 | +
|
| 4 | +The tests are structured as .txt files, parsed and executed here. |
| 5 | +The structure of the file is:: |
| 6 | +
|
| 7 | + number of expected autolinks |
| 8 | + # split |
| 9 | + lines to add to the default conf.py |
| 10 | + # split |
| 11 | + index.html content |
| 12 | +""" |
| 13 | +import sys |
| 14 | +import pytest |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | +from bs4 import BeautifulSoup |
| 18 | +from sphinx.cmd.build import main as sphinx_main |
| 19 | + |
| 20 | +# Insert test package root to path for all tests |
| 21 | +sys.path.insert(0, str(Path(__file__).parent / "src")) |
| 22 | + |
| 23 | +default_conf = """ |
| 24 | +extensions = [ |
| 25 | + "sphinx.ext.autodoc", |
| 26 | + "sphinx_codeautolink", |
| 27 | +] |
| 28 | +
|
| 29 | +autodoc_default_options = { |
| 30 | + "members": True, |
| 31 | + "undoc-members": True, |
| 32 | +} |
| 33 | +""" |
| 34 | + |
| 35 | +txt_tests = list(Path(__file__).parent.glob('*.txt')) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize('file', txt_tests) |
| 39 | +def test_extension(file: Path, tmp_path: Path): |
| 40 | + n_links, conf, index = file.read_text('utf-8').split('# split') |
| 41 | + src_dir = tmp_path / 'src' |
| 42 | + src_dir.mkdir() |
| 43 | + (src_dir / 'conf.py').write_text(default_conf + conf, 'utf-8') |
| 44 | + (src_dir / 'index.rst').write_text(index, 'utf-8') |
| 45 | + |
| 46 | + build_dir = tmp_path / 'build' |
| 47 | + sphinx_main(['-M', 'html', str(src_dir), str(build_dir)]) |
| 48 | + |
| 49 | + index_html = build_dir / 'html' / 'index.html' |
| 50 | + text = index_html.read_text('utf-8') |
| 51 | + soup = BeautifulSoup(text, 'html.parser') |
| 52 | + blocks = soup.find_all('a', attrs={'class': 'sphinx-codeautolink-a'}) |
| 53 | + assert len(blocks) == int(n_links.strip()) |
0 commit comments