Skip to content

Commit 41082b6

Browse files
committed
fix: add readme tests + doc dry run (#827)
1 parent fca95b1 commit 41082b6

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

docs/build_body.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import json
4+
import os
45

56
categories = {
67
"new" : "I'm new to quantum",
@@ -23,7 +24,10 @@
2324
"qiskit" : "Qiskit",
2425
}
2526

26-
def main():
27+
def main(dry_run: bool = False):
28+
if not os.path.isdir("docs"):
29+
raise RuntimeError("running script from the wrong directory")
30+
2731
with open("docs/ENTRIES.json", 'r') as fp:
2832
# first, check that all categories are used
2933
entries : dict = json.load(fp)
@@ -57,9 +61,10 @@ def main():
5761
main_body+= f"{text}\n\n"
5862

5963

60-
with open("docs/_NOTEBOOKS.md",'w') as fp:
61-
fp.write(main_body)
62-
print("docs/_NOTEBOOK.md updated!")
64+
if not dry_run:
65+
with open("docs/_NOTEBOOKS.md",'w') as fp:
66+
fp.write(main_body)
67+
print("docs/_NOTEBOOK.md updated!")
6368

6469
if __name__ == "__main__":
65-
main()
70+
main()

docs/build_index.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import json
4+
import os
45
from collections import defaultdict
56

67

@@ -61,7 +62,7 @@ def generate_index_section(terms_to_loc, loc_to_key):
6162
markdown = markdown[:-2] + "| <br>\n"
6263
return markdown
6364

64-
def main():
65+
def main(dry_run: bool = False):
6566
"""
6667
build the index
6768
@@ -70,8 +71,10 @@ def main():
7071
- loc/locations - where the notebook is
7172
- terms - keywords or index terms
7273
"""
73-
74-
with open("docs/entries.json", 'r') as fp:
74+
if not os.path.isdir("docs"):
75+
raise RuntimeError("running script from the wrong directory")
76+
77+
with open("docs/ENTRIES.json", "r") as fp:
7578
entries : dict = json.load(fp)
7679

7780
loc_to_key = {v["location"]:v["index_abbrv"] for v in entries.values()}
@@ -98,10 +101,10 @@ def main():
98101
index_section = generate_index_section(terms_to_loc, loc_to_key)
99102

100103
# Update INDEX
101-
with open(index_path, 'w') as f:
102-
f.write(index_section)
103-
104-
print("docs/_INDEX.md updated!")
104+
if not dry_run:
105+
with open(index_path, "w") as f:
106+
f.write(index_section)
107+
print("docs/_INDEX.md updated!")
105108

106109
if __name__ == "__main__":
107-
main()
110+
main()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ dependencies = [
5353
[tool.hatch.envs.default.scripts]
5454
test = """
5555
python -m ipykernel install --user --name "conda_braket" --display-name "conda_braket"
56-
pytest --dist worksteal -n logical -ra -v --durations=0 -m 'not mitiq' test/
56+
pytest -ra -v --durations=0 test/repo_tests/
57+
pytest --dist worksteal -n logical -ra -v --durations=0 -m 'not mitiq' test/integ_tests/
5758
"""
5859

5960
[tool.hatch.envs.lint]

test/integ_tests/test_all_notebooks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,18 @@ def get_mock_paths(notebook_dir, notebook_file):
110110
def html_exporter():
111111
return HTMLExporter(template_name="classic")
112112

113+
@pytest.fixture(autouse=True)
114+
def restore_cwd():
115+
""" after each test, move back to root_path - amazon-braket-examples/"""
116+
yield
117+
os.chdir(root_path)
118+
119+
113120
@pytest.mark.parametrize("notebook_dir, notebook_file", test_notebooks)
114121
def test_all_notebooks(notebook_dir, notebook_file, mock_level):
115122
if notebook_file in EXCLUDED_NOTEBOOKS:
116123
pytest.skip(f"Skipping Notebook: '{notebook_file}'")
117124

118-
os.chdir(root_path)
119125
os.chdir(notebook_dir)
120126
path_to_utils, path_to_mocks = get_mock_paths(notebook_dir, notebook_file)
121127
# Try to use the conda_braket kernel if installed, otherwise fall back to the default value of python3
@@ -132,7 +138,6 @@ def test_all_notebooks(notebook_dir, notebook_file, mock_level):
132138

133139
@pytest.mark.parametrize("notebook_dir, notebook_file", test_notebooks)
134140
def test_notebook_to_html_conversion(notebook_dir, notebook_file, mock_level, html_exporter):
135-
os.chdir(root_path)
136141
os.chdir(notebook_dir)
137142

138143
html_exporter.from_file(notebook_file)
@@ -151,7 +156,6 @@ def test_record():
151156
break
152157
if not notebook_file or not notebook_dir:
153158
pytest.skip(f"Notebook not found: '{notebook_file_search}'")
154-
os.chdir(root_path)
155159
os.chdir(notebook_dir)
156160
path_to_utils, _path_to_mocks = get_mock_paths(notebook_dir, notebook_file)
157161
path_to_utils = path_to_utils.replace("mock_utils.py", "record_utils.py")

test/repo_tests/test_readme.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import json
12
import os
23
import pathlib
34
import re
5+
import sys
6+
from importlib import import_module
47

58
EXCLUDED_DIRS = [
69
# These directories contain notebook files that should not be linked to in the README.
@@ -13,6 +16,7 @@
1316

1417

1518
def test_readme():
19+
""" Each entry in the README should have an actual file in the repository """
1620
root_path = pathlib.Path(__file__).parent.parent.parent.resolve()
1721

1822
examples_path = os.path.join(root_path, "examples")
@@ -40,3 +44,39 @@ def test_readme():
4044
assert (
4145
missing_in_readme == set()
4246
), "There are some new notebooks that haven't been added to the README summary: "
47+
48+
def test_readme_matches_entries():
49+
""" Each entry in the README should come from an ENTRIES.json entry. """
50+
root_path = pathlib.Path(__file__).parent.parent.parent.resolve()
51+
52+
with open(os.path.join(root_path, "docs/ENTRIES.json"), "r") as f:
53+
entries = json.load(f)
54+
55+
with open(os.path.join(root_path, "README.md"), "r") as f:
56+
readme = f.read()
57+
58+
readme_links = set(re.findall(LINK_EXAMPLES_REGEX, readme))
59+
entries_links = {entry["location"] for entry in entries.values()}
60+
61+
missing_in_entries = readme_links - entries_links
62+
extra_in_entries = entries_links - readme_links
63+
64+
assert missing_in_entries == set(), f"README links not in ENTRIES.json: {missing_in_entries}"
65+
assert extra_in_entries == set(), f"ENTRIES.json links not in README: {extra_in_entries}"
66+
67+
def test_readme_build_successful():
68+
""" Doc build should run successful as a dry_run """
69+
root_path = pathlib.Path(__file__).parent.parent.parent.resolve()
70+
original_cwd = os.getcwd()
71+
72+
try:
73+
os.chdir(root_path)
74+
sys.path.insert(0, str(root_path / "docs"))
75+
build_body = import_module("build_body")
76+
build_index = import_module("build_index")
77+
build_body.main(dry_run=True)
78+
build_index.main(dry_run=True)
79+
finally:
80+
os.chdir(original_cwd)
81+
sys.path.pop(0)
82+

0 commit comments

Comments
 (0)