|
1 | | -name: chapter tests (reusable) |
| 1 | +name: Chapter Tests (Reusable) |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | workflow_call: |
5 | 5 | inputs: |
6 | 6 | chapter: |
7 | | - description: "Folder name of the chapter (e.g., ch2_rl_formulation)" |
| 7 | + description: "Chapter folder name (e.g., ch7_td_control)" |
8 | 8 | required: true |
9 | 9 | 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 |
10 | 20 |
|
11 | 21 | jobs: |
12 | 22 | test: |
13 | | - name: ${{ inputs.chapter }} (py${{ matrix.python }}) |
| 23 | + name: Run tests for ${{ inputs.chapter }} |
14 | 24 | 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" |
19 | 30 |
|
20 | 31 | steps: |
21 | | - - name: Check out repository |
| 32 | + - name: Checkout |
22 | 33 | uses: actions/checkout@v4 |
23 | 34 |
|
24 | | - - name: Set up Python ${{ matrix.python }} |
| 35 | + - name: Set up Python ${{ inputs.python-version }} |
25 | 36 | uses: actions/setup-python@v5 |
26 | 37 | with: |
27 | | - python-version: ${{ matrix.python }} |
| 38 | + python-version: ${{ inputs.python-version }} |
28 | 39 | cache: "pip" |
29 | 40 |
|
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 |
35 | 54 |
|
36 | | - - name: Show environment |
| 55 | + - name: Show environment (debug) |
37 | 56 | run: | |
38 | | - python -V |
| 57 | + python --version |
| 58 | + pip --version |
39 | 59 | pip list |
40 | 60 |
|
41 | | - - name: Run tests |
| 61 | + - name: Run pytest (scoped to chapter if tests exist) |
| 62 | + shell: bash |
42 | 63 | 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 |
0 commit comments