Skip to content

Commit

Permalink
Test using manual script to test codeblocks (better for debugging, si…
Browse files Browse the repository at this point in the history
…mpler, no dependency)
  • Loading branch information
elliottower committed Oct 17, 2023
1 parent e06c279 commit 6dc3a11
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
run: python docs/_scripts/gen_envs_mds.py
- name: Documentation test
run: |
xvfb-run -s "-screen 0 1024x768x24" pytest docs --markdown-docs -m markdown-docs --splits 10 --group ${{ matrix.group }}
xvfb-run -s "-screen 0 1024x768x24" pytest test/test_codeblocks.py --splits 10 --group ${{ matrix.group }}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ typeCheckingMode = "basic"
reportMissingImports = false

[tool.pytest.ini_options]
addopts = [ "--ignore-glob=*/__init__.py", "-n=auto", "--ignore=tutorials", "--ignore=docs/_scripts", "--ignore=conf.py"]
addopts = [ "--ignore-glob=*/__init__.py", "-n=auto", "--ignore=tutorials", "--ignore=docs/_scripts", "--ignore=test/test_codeblocks.py", "--ignore=conf.py"]
57 changes: 57 additions & 0 deletions test/test_codeblocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
This script is used to test the code blocks in the documentation.
"""

import os
from pathlib import Path

import pytest

IGNORED_FILES = ["index.md", "README.md"]
DOCS_DIR = Path(__file__).parent.parent


def parse_codeblocks():
# Get all markdown files in the docs directory
markdown_files = []
for root, dirs, files in os.walk(DOCS_DIR):
for file in files:
if file.endswith(".md"):
markdown_files.append(os.path.join(root, file))

# Get all code blocks in the markdown files
code_blocks = []
for markdown_file in markdown_files:
if str(Path(markdown_file).relative_to(DOCS_DIR)) in IGNORED_FILES:
continue
with open(markdown_file, encoding="utf-8") as f:
lines = f.readlines()

in_code_block = False
for line_number, line in enumerate(lines, start=1):
if line.startswith("```python"):
code_blocks.append({})
code_blocks[-1]["code"] = []
code_blocks[-1]["filename"] = markdown_file
code_blocks[-1]["line"] = line_number
in_code_block = True
elif in_code_block:
if line.startswith("```"):
in_code_block = False
else:
code_blocks[-1]["code"].append(line)
return code_blocks


CODE_BLOCKS = parse_codeblocks()


@pytest.mark.parametrize("code_block", CODE_BLOCKS)
def test_codeblocks(code_block):
code_block_rel_path = Path(code_block["filename"]).relative_to(DOCS_DIR)
code_test_filename = f"<doctest {Path(code_block['filename']).relative_to(Path(DOCS_DIR))}[line: {code_block['line']}]>"

print("-" * 80)
print(f"Testing code block: {code_block_rel_path}, line: {code_block['line']}")
exec(compile("\n".join(code_block["code"]), code_test_filename, "exec"))
print("Result: PASSED")

0 comments on commit 6dc3a11

Please sign in to comment.