Skip to content

Commit a28489a

Browse files
CI: reusable chapter test workflow + chapters matrix
1 parent c83c421 commit a28489a

2 files changed

Lines changed: 83 additions & 42 deletions

File tree

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,92 @@
1-
name: chapter tests (reusable)
1+
name: Chapter Tests (Reusable)
22

33
on:
44
workflow_call:
55
inputs:
66
chapter:
7-
description: "Folder name of the chapter (e.g., ch2_rl_formulation)"
7+
description: "Chapter folder name (e.g., ch7_td_control)"
88
required: true
99
type: string
10+
python-version:
11+
description: "Python version to use"
12+
required: false
13+
default: "3.10"
14+
type: string
15+
extra-deps:
16+
description: "Extra pip packages to install (space-separated)"
17+
required: false
18+
default: ""
19+
type: string
1020

1121
jobs:
1222
test:
13-
name: ${{ inputs.chapter }} (py${{ matrix.python }})
23+
name: Run tests for ${{ inputs.chapter }}
1424
runs-on: ubuntu-latest
15-
strategy:
16-
fail-fast: false
17-
matrix:
18-
python: ["3.10", "3.11", "3.12"]
25+
timeout-minutes: 15
26+
27+
env:
28+
MPLBACKEND: Agg
29+
PYTHONDONTWRITEBYTECODE: "1"
1930

2031
steps:
21-
- name: Check out repository
32+
- name: Checkout
2233
uses: actions/checkout@v4
2334

24-
- name: Set up Python ${{ matrix.python }}
35+
- name: Set up Python ${{ inputs.python-version }}
2536
uses: actions/setup-python@v5
2637
with:
27-
python-version: ${{ matrix.python }}
38+
python-version: ${{ inputs.python-version }}
2839
cache: "pip"
2940

30-
- name: Upgrade pip
31-
run: python -m pip install -U pip
32-
33-
- name: Install dependencies (root requirements.txt)
34-
run: pip install -r requirements.txt
41+
- name: Install dependencies
42+
shell: bash
43+
run: |
44+
python -m pip install --upgrade pip
45+
# Root requirements if present
46+
if [ -f requirements.txt ]; then
47+
pip install -r requirements.txt
48+
fi
49+
# Minimal defaults + optional extras
50+
pip install numpy matplotlib pytest
51+
if [ -n "${{ inputs.extra-deps }}" ]; then
52+
pip install ${{ inputs.extra-deps }}
53+
fi
3554
36-
- name: Show environment
55+
- name: Show environment (debug)
3756
run: |
38-
python -V
57+
python --version
58+
pip --version
3959
pip list
4060
41-
- name: Run tests
61+
- name: Run pytest (scoped to chapter if tests exist)
62+
shell: bash
4263
run: |
43-
echo "Running pytest in ${{ inputs.chapter }}/tests"
44-
pytest -q ${{ inputs.chapter }}/tests
64+
set -euo pipefail
65+
CHAP="${{ inputs.chapter }}"
66+
TARGET=""
67+
if [ -d "tests/${CHAP}" ]; then
68+
TARGET="tests/${CHAP}"
69+
elif [ -f "tests/test_${CHAP}.py" ]; then
70+
TARGET="tests/test_${CHAP}.py"
71+
else
72+
# Fallback: run full suite
73+
TARGET=""
74+
fi
75+
76+
echo "Chapter: ${CHAP}"
77+
if [ -n "${TARGET}" ]; then
78+
echo "Running targeted tests at: ${TARGET}"
79+
pytest -q "${TARGET}"
80+
else
81+
echo "No chapter-specific tests found; running full suite."
82+
pytest -q
83+
fi
84+
85+
# Optional: upload pytest cache / reports if you later add them
86+
# - name: Upload junit report
87+
# if: always()
88+
# uses: actions/upload-artifact@v4
89+
# with:
90+
# name: junit-${{ inputs.chapter }}
91+
# path: reports/junit-${{ inputs.chapter }}.xml
92+
# if-no-files-found: ignore

.github/workflows/python-tests.yml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
1-
name: Python (Chapters)
2-
3-
on:
4-
push:
5-
branches: [ "main" ]
6-
paths:
7-
- "ch2_rl_formulation/**"
8-
- "ch4_dynamic_programming/**"
9-
- "ch5_monte_carlo/**"
10-
- ".github/workflows/python-tests.yml"
11-
- ".github/workflows/_chapter-tests.yml"
12-
- "requirements.txt"
13-
pull_request:
14-
branches: [ "main" ]
15-
paths:
16-
- "ch2_rl_formulation/**"
17-
- "ch4_dynamic_programming/**"
18-
- "ch5_monte_carlo/**"
19-
- ".github/workflows/python-tests.yml"
20-
- ".github/workflows/_chapter-tests.yml"
21-
- "requirements.txt"
22-
231
jobs:
242
ch2:
253
uses: ./.github/workflows/_chapter-tests.yml
264
with:
275
chapter: ch2_rl_formulation
286

7+
ch3:
8+
uses: ./.github/workflows/_chapter-tests.yml
9+
with:
10+
chapter: ch3_multi_armed_bandits
11+
2912
ch4:
3013
uses: ./.github/workflows/_chapter-tests.yml
3114
with:
@@ -35,3 +18,13 @@ jobs:
3518
uses: ./.github/workflows/_chapter-tests.yml
3619
with:
3720
chapter: ch5_monte_carlo
21+
22+
ch6:
23+
uses: ./.github/workflows/_chapter-tests.yml
24+
with:
25+
chapter: ch6_td_learning
26+
27+
ch7:
28+
uses: ./.github/workflows/_chapter-tests.yml
29+
with:
30+
chapter: ch7_td_control

0 commit comments

Comments
 (0)