|
10 | 10 | import re |
11 | 11 | import os |
12 | 12 | from sphinx.cmd.build import main |
| 13 | +from bs4 import BeautifulSoup |
13 | 14 |
|
14 | 15 |
|
15 | 16 | SCRIPT_DIR = os.path.dirname(__file__) |
@@ -41,51 +42,61 @@ def test_viewcode_of_sphinx_project(tmp_path): |
41 | 42 | with file_name.open() as f: |
42 | 43 | contents = f.read() |
43 | 44 |
|
| 45 | + # Looking for tags of the form |
| 46 | + # |
| 47 | + # <a class="reference internal" href="_modules/example.c.html#c.MY_COOL_MACRO"><span class="viewcode-link"><span class="pre">[source]</span></span></a> |
44 | 48 | chosen_links = ( |
45 | | - '<dt id="c.MY_COOL_MACRO">', |
46 | | - '<a class="reference internal" href="_modules/example.c.html#c.MY_COOL_MACRO"><span class="viewcode-link">[source]</span></a>', |
47 | | - '<dt id="c.members_documented_with_napoleon.two.nested_two">', |
48 | | - '<a class="reference internal" href="_modules/example.c.html#c.members_documented_with_napoleon.two.nested_two"><span class="viewcode-link">[source]</span></a>', |
| 49 | + "_modules/example.c.html#c.MY_COOL_MACRO", |
| 50 | + "_modules/example.c.html#c.members_documented_with_napoleon.two.nested_two", |
49 | 51 | ) |
50 | | - for l in chosen_links: |
51 | | - assert l in contents |
| 52 | + |
| 53 | + soup = BeautifulSoup(contents) |
| 54 | + for href in chosen_links: |
| 55 | + tag = soup.find("a", {"href": href}) |
| 56 | + assert "[source]" == tag.text |
52 | 57 |
|
53 | 58 | file_name = tmp_path / "sub_dir" / "file_2.html" |
54 | 59 | with file_name.open() as f: |
55 | 60 | contents = f.read() |
56 | 61 |
|
57 | 62 | chosen_links = ( |
58 | | - '<a class="reference internal" href="../_modules/file_2.c.html#c.unknown_member.foo"><span class="viewcode-link">[source]</span></a>', |
59 | | - '<a class="reference internal" href="../_modules/file_2.c.html#c.file_level_variable"><span class="viewcode-link">[source]</span></a>', |
| 63 | + "../_modules/file_2.c.html#c.unknown_member.foo", |
| 64 | + "../_modules/file_2.c.html#c.file_level_variable", |
60 | 65 | ) |
61 | | - for l in chosen_links: |
62 | | - assert l in contents |
| 66 | + soup = BeautifulSoup(contents) |
| 67 | + for href in chosen_links: |
| 68 | + tag = soup.find("a", {"href": href}) |
| 69 | + assert "[source]" == tag.text |
63 | 70 |
|
64 | 71 | # Test the back links |
65 | 72 | file_name = tmp_path / "_modules" / "example.c.html" |
66 | 73 | with file_name.open() as f: |
67 | 74 | contents = f.read() |
68 | 75 |
|
69 | 76 | chosen_links = ( |
70 | | - '<div class="viewcode-block" id="c.members_documented_with_napoleon.two.nested_two"><a class="viewcode-back" href="../example.html#c.members_documented_with_napoleon.two.nested_two">[docs]</a>', |
71 | | - '<div class="viewcode-block" id="c.MY_COOL_MACRO"><a class="viewcode-back" href="../example.html#c.MY_COOL_MACRO">[docs]</a>', |
| 77 | + "../example.html#c.members_documented_with_napoleon.two.nested_two", |
| 78 | + "../example.html#c.MY_COOL_MACRO", |
72 | 79 | ) |
73 | | - for l in chosen_links: |
74 | | - assert l in contents |
| 80 | + soup = BeautifulSoup(contents) |
| 81 | + for href in chosen_links: |
| 82 | + tag = soup.find("a", {"href": href}) |
| 83 | + assert "[docs]" == tag.text |
75 | 84 |
|
76 | 85 | # Test normal C constructs elsewhere in docs |
77 | 86 | file_name = tmp_path / "viewcode.html" |
78 | 87 | with file_name.open() as f: |
79 | 88 | contents = f.read() |
80 | 89 |
|
81 | 90 | chosen_links = ( |
82 | | - '<a class="reference internal" href="_modules/example.c.html#c.napoleon_documented_function"><span class="viewcode-link">[source]</span></a>', |
| 91 | + "_modules/example.c.html#c.napoleon_documented_function", |
83 | 92 | # One needs to use noindex in order to avoid sphinx warning and once |
84 | 93 | # one uses noindex then the permalinks are no longer generated :( |
85 | | - # '<a class="headerlink" href="#c.napoleon_documented_function" title="Permalink to this definition">', |
| 94 | + # "#c.napoleon_documented_function" |
86 | 95 | ) |
87 | | - for l in chosen_links: |
88 | | - assert l in contents |
| 96 | + soup = BeautifulSoup(contents) |
| 97 | + for href in chosen_links: |
| 98 | + tag = soup.find("a", {"href": href}) |
| 99 | + assert "[source]" == tag.text |
89 | 100 |
|
90 | 101 | # Ensure only the one function that actually had a source file to be able to link to creates a link |
91 | 102 | link_count = len(re.findall("viewcode-link", contents)) |
|
0 commit comments