Skip to content

Commit b830874

Browse files
author
Hông-Lan Botterman
committed
execute code in rst files and improve unique workflow for unit tests and build docs
1 parent d2b157a commit b830874

File tree

3 files changed

+80
-65
lines changed

3 files changed

+80
-65
lines changed

.github/workflows/docs.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1-
name: Unit tests
1+
name: Unit tests and docs generation
22

33
on:
44
push:
55
branches:
66
- "**"
77
pull_request:
8+
branches:
9+
- "**"
810
types: [opened, synchronize, reopened, ready_for_review]
911
workflow_dispatch:
1012

1113
jobs:
1214
check:
1315
if: github.event.pull_request.draft == false
14-
runs-on: ${{matrix.os}}
16+
runs-on: ubuntu-latest
1517
strategy:
1618
matrix:
1719
os: [ubuntu-latest, windows-latest]
18-
python-version: ['3.8', '3.9', '3.10', '3.11']
20+
python-version: [3.8, 3.9, 3.10, 3.11]
21+
include:
22+
- os: ubuntu-latest
23+
python-version: 3.11
1924
defaults:
2025
run:
2126
shell: bash -l {0}
2227

2328
steps:
29+
- name: Set OS and Python version
30+
id: set-vars
31+
run: |
32+
if [[ "${{ github.ref }}" == "refs/heads/main" || "${{ github.ref }}" == "refs/heads/dev" ]]; then
33+
echo "os-matrix=ubuntu-latest,windows-latest" >> $GITHUB_ENV
34+
echo "python-matrix=3.8,3.9,3.10,3.11" >> $GITHUB_ENV
35+
else
36+
echo "os-matrix=ubuntu-latest" >> $GITHUB_ENV
37+
echo "python-matrix=3.11" >> $GITHUB_ENV
2438
- name: Checkout
2539
uses: actions/checkout@v3
2640
- name: Python
2741
uses: actions/setup-python@v4
2842
with:
2943
python-version: ${{ matrix.python-version }}
44+
- name: Cache Poetry
45+
uses: actions/cache@v3
46+
with:
47+
path: |
48+
~/.cache/pypoetry
49+
~/.cache/pip
50+
key: ${{ runner.os }}-poetry-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
51+
restore-keys: |
52+
${{ runner.os }}-poetry-${{ matrix.python-version }}-
3053
- name: Poetry
3154
uses: snok/install-poetry@v1
3255
with:
@@ -41,3 +64,42 @@ jobs:
4164
uses: codecov/codecov-action@v3
4265
env:
4366
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
67+
68+
docs:
69+
runs-on: ubuntu-latest
70+
needs: check
71+
if: github.event_name == 'push' || github.event_name == 'pull_request'
72+
73+
steps:
74+
- name: Checkout
75+
uses: actions/checkout@v3
76+
- name: Python
77+
uses: actions/setup-python@v4
78+
with:
79+
python-version: ${{ matrix.python-version }}
80+
- name: Cache Poetry
81+
uses: actions/cache@v3
82+
with:
83+
path: |
84+
~/.cache/pypoetry
85+
~/.cache/pip
86+
key: ${{ runner.os }}-poetry-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
87+
restore-keys: |
88+
${{ runner.os }}-poetry-${{ matrix.python-version }}-
89+
- name: Poetry
90+
uses: snok/install-poetry@v1
91+
with:
92+
version: 1.8.3
93+
- name: Lock
94+
run: poetry lock --no-update
95+
- name: Install
96+
run: poetry install
97+
- name: Check Changed Files
98+
id: changed-files
99+
run: |
100+
git fetch origin ${{ github.base_ref }} --depth=1
101+
git diff --name-only origin/${{ github.base_ref }} > changed_files.txt
102+
- name: Build Docs
103+
if: contains(fromJSON('["docs/", ".rst"]').join(','), fromJSON('["${{ steps.changed-files.outputs.files }}"]').join(','))
104+
run: |
105+
poetry run sphinx-build -b html docs/ _build/html

tests/conftest.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def pytest_sessionstart(session: Any) -> None:
6262
"""Run tests (hook) on specified .rst files at pytest session start.
6363
6464
This function reads through a list of predefined .rst files,
65-
extracts Python code blocks, and ensures that the code is syntactically
66-
valid and that all necessary imports work correctly.
65+
extracts Python code blocks, concatenates them, and executes the code.
6766
The function will scan each file listed in `RST_FILES_TO_TEST`.
6867
6968
This function is invoked automatically by pytest when the session starts.
@@ -90,39 +89,23 @@ def pytest_sessionstart(session: Any) -> None:
9089
with open(rst_file, encoding="utf-8") as f:
9190
content = f.read()
9291
code_blocks = extract_python_blocks(content)
92+
code = "\n".join(code_blocks)
9393

94-
for i, code_block in enumerate(code_blocks):
94+
if code:
9595
try:
96-
tree = ast.parse(code_block)
97-
for node in ast.walk(tree):
98-
if isinstance(node, ast.Import):
99-
for name in node.names:
100-
try:
101-
__import__(name.name)
102-
except ImportError as e:
103-
pytest.fail(
104-
f"Cannot import {name.name} in "
105-
f"{rst_file}: {str(e)}"
106-
)
107-
elif isinstance(node, ast.ImportFrom):
108-
if node.module:
109-
try:
110-
__import__(node.module) # noqa
111-
except ImportError as e:
112-
pytest.fail(
113-
f"Cannot import {node.module} in "
114-
f"{rst_file}: {str(e)}."
115-
)
116-
else:
117-
pytest.fail(
118-
f"Module name is None in {rst_file} "
119-
"in ImportFrom statement."
120-
)
96+
tree = ast.parse(code)
97+
exec(code, globals(), locals())
12198
except SyntaxError as e:
12299
pytest.fail(
123-
"Invalid Python syntax in code block "
124-
f"{i + 1} in {rst_file}: "
125-
f"\n{code_block}\nError: {str(e)}"
100+
"Syntax error in code block "
101+
f"in {rst_file}: \n{code}\nError: {str(e)}."
126102
)
103+
except Exception as e:
104+
pytest.fail(
105+
f"Error while executing the code in {rst_file}: "
106+
f"{str(e)}."
107+
)
108+
else:
109+
logging.info(f"No Python code blocks found in {rst_file}.")
127110
else:
128-
logging.info(f"File {rst_file} does not exist, skippin...")
111+
logging.info(f"File {rst_file} does not exist, skipping.")

0 commit comments

Comments
 (0)