Repository navigation
334 lines (289 loc) · 13.3 KB
/
Copy pathpython-publish.yml
File metadata and controls
334 lines (289 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
name: MidManStudio.Mdix — Python Tests
on:
workflow_dispatch:
permissions:
contents: write
actions: read
jobs:
# ── 1. Check trigger ───────────────────────────────────────────────────────
parse-trigger:
runs-on: ubuntu-latest
outputs:
run_py: ${{ steps.decide.outputs.run_py }}
steps:
- name: Decide whether to run Python tests
id: decide
run: |
RUN=false
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
RUN=true
elif [ "${{ github.event_name }}" = "pull_request" ]; then
MSG="${{ github.event.pull_request.title }}"
echo "$MSG" | grep -qiE '\-\-mdix-py\b' && RUN=true || true
else
MSG="${{ github.event.head_commit.message }}"
echo "$MSG" | grep -qiE '\-\-mdix-py\b' && RUN=true || true
fi
echo "run_py=$RUN" >> $GITHUB_OUTPUT
echo "Run Python tests: $RUN"
# ── 2. Build wheel + run tests ─────────────────────────────────────────────
build-and-test:
runs-on: ubuntu-latest
needs: parse-trigger
if: needs.parse-trigger.outputs.run_py == 'true'
steps:
- uses: actions/checkout@v4
# ── Rust toolchain ─────────────────────────────────────────────────────
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ubuntu-py-test-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ubuntu-py-test-
# ── Python setup ───────────────────────────────────────────────────────
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
# ── Build mdix-cli + generate enum-only DLM test fixtures ──────────────
# test_enum_dlm.py needs pre-compiled .mdix.enc/.mdix.key fixtures (see
# its own module doc comment) -- this binding only exposes
# load_encrypted, not a compile entry point, unlike mdix-wasm which
# builds its DLM fixtures in-memory at test time. Generating them
# fresh here, from whatever mdix-cli/dixscript this run just built,
# means they can never go stale the way a checked-in binary
# regenerated by a local script someone forgets to re-run would.
- name: Build mdix-cli
run: cargo build -p mdix-cli --release
- name: Generate enum-only DLM test fixtures
run: |
set -e
MDIX="target/release/mdix"
SRC="mdix_files/tests/dlm"
OUT="$(mktemp -d)"
DST="mdix-python/tests/fixtures/enum_dlm"
mkdir -p "$DST"
for name in 11_enum_only_gzip 12_enum_only_aes256 13_enum_only_gzip_aes256; do
"$MDIX" compile "$SRC/$name.mdix" -o "$OUT"
cp "$OUT/$name.mdix.enc" "$DST/"
cp "$OUT/$name.mdix.key" "$DST/"
done
echo "Generated fixtures:"
ls -lh "$DST"
# ── Build wheel with maturin ───────────────────────────────────────────
- name: Install maturin
run: pip install maturin
- name: Build wheel
run: |
maturin build \
--release \
--out dist \
--manifest-path mdix-python/Cargo.toml
echo "Built wheel:"
ls -lh dist/
# ── Install wheel + test dependencies ─────────────────────────────────
- name: Install wheel and test dependencies
run: |
pip install dist/midmanstudio_mdix-*.whl
pip install \
pytest>=7.0 \
pytest-json-report>=1.5 \
numpy>=1.21 \
pandas>=1.3
# ── Run tests ──────────────────────────────────────────────────────────
- name: Run pytest
continue-on-error: true
run: |
python -m pytest mdix-python/tests/ \
--json-report \
--json-report-file=py-test-results.json \
--tb=short \
-v \
2>&1 | tee py-test-console.txt
echo "TEST_EXIT=${PIPESTATUS[0]}" >> $GITHUB_ENV
# ── Parse JSON report → CI-friendly JSON ──────────────────────────────
- name: Parse test results
env:
BUILD_NUM: ${{ github.run_number }}
BRANCH: ${{ github.ref_name }}
COMMIT: ${{ github.sha }}
BUILD_DATE: ${{ github.event.head_commit.timestamp }}
run: |
python3 - << 'PYEOF'
import json, os, sys
with open("py-test-results.json") as f:
raw = json.load(f)
summary_raw = raw.get("summary", {})
tests_raw = raw.get("tests", [])
suites: dict = {}
for t in tests_raw:
node = t.get("nodeid", "")
parts = node.split("::")
module = parts[0].replace("mdix-python/tests/", "").replace(".py", "") if parts else "unknown"
cls = parts[1] if len(parts) >= 3 else module
name = parts[-1]
outcome = t.get("outcome", "failed")
dur_ms = round(t.get("duration", 0) * 1000, 2)
output = ""
call = t.get("call", {})
if call.get("longrepr"):
output = str(call["longrepr"])[:500]
elif t.get("setup", {}).get("longrepr"):
output = str(t["setup"]["longrepr"])[:500]
key = f"{module}.{cls}"
if key not in suites:
suites[key] = {
"name": cls,
"module": module,
"passed": 0, "failed": 0, "skipped": 0,
"duration_s": 0.0,
"tests": [],
}
status = (
"passed" if outcome == "passed" else
"skipped" if outcome == "skipped" else
"failed"
)
suites[key]["tests"].append({
"name": name,
"status": status,
"duration_ms": dur_ms,
"output": output,
})
suites[key][status] += 1
suites[key]["duration_s"] += dur_ms / 1000
suite_list = [
{**v, "duration_s": round(v["duration_s"], 4)}
for v in suites.values()
]
total = summary_raw.get("total", 0)
passed = summary_raw.get("passed", 0)
failed = summary_raw.get("failed", 0)
skipped = summary_raw.get("skipped", 0)
dur_s = round(sum(s["duration_s"] for s in suite_list), 3)
out = {
"build": os.environ.get("BUILD_NUM", "0"),
"branch": os.environ.get("BRANCH", "unknown"),
"commit": os.environ.get("COMMIT", "")[:8],
"date": os.environ.get("BUILD_DATE", ""),
"python": "3.11",
"summary": {
"total": total, "passed": passed,
"failed": failed, "skipped": skipped,
"duration_s": dur_s,
},
"suites": suite_list,
}
with open("py-test-output.json", "w") as f:
json.dump(out, f, indent=2)
print(f"Suites:{len(suite_list)} Total:{total} "
f"Passed:{passed} Failed:{failed} Skipped:{skipped}")
PYEOF
# ── Build HTML report ──────────────────────────────────────────────────
- name: Build HTML report
run: |
cp .github/python-test-template.html py-test-index.html
sed -i "s/BUILD_NUM_PLACEHOLDER/${{ github.run_number }}/g" py-test-index.html
sed -i "s/BRANCH_PLACEHOLDER/${{ github.ref_name }}/g" py-test-index.html
sed -i "s/COMMIT_PLACEHOLDER/${GITHUB_SHA:0:8}/g" py-test-index.html
sed -i "s|DATE_PLACEHOLDER|$(date -u)|g" py-test-index.html
# ── Upload artifacts ───────────────────────────────────────────────────
- name: Upload artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: py-test-results-${{ github.run_number }}
path: |
py-test-output.json
py-test-index.html
py-test-console.txt
py-test-results.json
dist/
retention-days: 30
# continue-on-error on "Run pytest" above exists so Parse/Report/
# Upload still run (and get uploaded) even on a failing suite — same
# reason wasm-test.yml uses the identical pattern. That pattern only
# works with this explicit re-check at the very end, after everything
# else has had its `if: always()` chance to run: without it, TEST_EXIT
# is captured and never read, and a real pytest failure still reports
# this job — and this whole workflow run — as successful.
- name: Fail if pytest actually failed
if: always()
run: |
if [ "${TEST_EXIT:-0}" != "0" ]; then
echo "::error::pytest failed — see py-test-console.txt in the uploaded artifacts, or the 'Run pytest' step's log above."
exit 1
fi
# ── 3. Deploy to gh-pages ──────────────────────────────────────────────────
deploy-results:
runs-on: ubuntu-latest
needs: [parse-trigger, build-and-test]
if: |
always() &&
needs.build-and-test.result == 'success' &&
(github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' ||
github.event_name == 'workflow_dispatch')
steps:
- uses: actions/checkout@v4
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: py-test-results-${{ github.run_number }}
path: deploy/
- name: Deploy to gh-pages
run: |
git config --global user.name "DixScript CI"
git config --global user.email "ci@dixscript.dev"
BUILD_NUM=${{ github.run_number }}
WORK=$(mktemp -d)
if git fetch origin gh-pages:gh-pages 2>/dev/null; then
git worktree add "$WORK" gh-pages
else
git worktree add --orphan -b gh-pages "$WORK"
fi
mkdir -p "$WORK/python" "$WORK/python/builds/build_${BUILD_NUM}"
cp deploy/py-test-output.json "$WORK/python/test-results.json"
cp deploy/py-test-index.html "$WORK/python/index.html"
cp deploy/py-test-output.json "$WORK/python/builds/build_${BUILD_NUM}/test-results.json"
cp deploy/py-test-index.html "$WORK/python/builds/build_${BUILD_NUM}/index.html"
cp .github/landing-index.html "$WORK/index.html"
cd "$WORK"
git add .
if git diff --staged --quiet; then
echo "Nothing new to deploy"
exit 0
fi
git commit -m "Python Tests — Build #${BUILD_NUM} [skip ci]"
git push \
"https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" \
gh-pages
echo "Deployed → https://mid-d-man.github.io/DixScript-Rust/python/"
# ── 4. Summary ─────────────────────────────────────────────────────────────
summary:
runs-on: ubuntu-latest
needs: [parse-trigger, build-and-test, deploy-results]
if: always()
steps:
- name: Print summary
run: |
echo "============================================="
echo " MidManStudio.Mdix — Python Test Summary"
echo "============================================="
echo "Build: #${{ github.run_number }}"
echo "Branch: ${{ github.ref_name }}"
echo "Trigger: ${{ github.event_name }}"
echo "Tests: ${{ needs.build-and-test.result || 'skipped' }}"
echo "Deploy: ${{ needs.deploy-results.result || 'skipped' }}"
echo ""
if [ "${{ needs.parse-trigger.outputs.run_py }}" != "true" ]; then
echo "Skipped — commit message did not contain --mdix-py"
fi
echo ""
echo "Results → https://mid-d-man.github.io/DixScript-Rust/python/"