-
Notifications
You must be signed in to change notification settings - Fork 1.4k
244 lines (233 loc) · 11 KB
/
Copy pathunity-tests.yml
File metadata and controls
244 lines (233 loc) · 11 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
name: Unity Tests
on:
workflow_dispatch: {}
workflow_call:
inputs:
ref:
description: "Git ref to test (defaults to the triggering ref)."
type: string
required: false
default: ""
push:
# Exclude beta and main: those branches re-trigger this workflow via
# workflow_call from beta-release.yml / release.yml. Without the exclusion,
# a push to beta that touches MCPForUnity/** fires this workflow twice
# for the same SHA, and GitHub's auto-generated concurrency group
# (`unity-tests-refs/heads/beta`) cancels the older run with the noisy
# "higher priority waiting request" annotation.
branches-ignore: [beta, main]
paths:
- TestProjects/UnityMCPTests/**
- MCPForUnity/Editor/**
- MCPForUnity/Runtime/**
- .github/workflows/unity-tests.yml
# Same-repo PRs get a unity-tests status check on every open / push via this trigger
# (mirrors python-tests.yml). Fork PRs ALSO fire this trigger but run in the fork's
# context without secrets — the detect step downstream writes unity_ok=false and the
# job reports a green check having compiled and tested nothing. That skip is stated
# loudly in the job's step summary so it is never mistaken for a pass.
#
# There is deliberately no pull_request_target trigger here. Running fork-authored
# C# through game-ci/unity-test-runner with UNITY_* secrets in scope is the classic
# "pwn request" shape — an [InitializeOnLoad] script in the PR is enough to read
# them. To test a fork PR, review the diff and push its branch into this repo; the
# push trigger above then runs the full suite in a genuinely trusted context.
pull_request:
branches: [main, beta]
paths:
- TestProjects/UnityMCPTests/**
- MCPForUnity/Editor/**
- MCPForUnity/Runtime/**
- .github/workflows/unity-tests.yml
# Dedup runs for the same branch across push / pull_request / workflow_call.
# Same-repo PRs would otherwise fire both push (on the branch SHA) AND pull_request (on the PR);
# concurrency keeps only the newer in-flight run per branch.
concurrency:
group: unity-tests-${{ github.head_ref || github.ref }}
cancel-in-progress: true
jobs:
matrix:
name: Compute Unity version matrix
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
versions: ${{ steps.set.outputs.versions }}
steps:
- name: Checkout version manifest
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref || github.event.pull_request.head.sha || github.ref }}
sparse-checkout: tools/unity-versions.json
sparse-checkout-cone-mode: false
persist-credentials: false
- name: Select versions for this trigger
id: set
env:
EVENT_NAME: ${{ github.event_name }}
GH_REF: ${{ github.ref }}
FULL_MATRIX_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'full-matrix') }}
run: |
set -euo pipefail
# Full matrix on: beta push, workflow_call (release pipelines), workflow_dispatch,
# or a PR carrying the 'full-matrix' label. Note the label is only read when the
# workflow fires, so applying it to an open PR takes effect on the next push.
# Default (single defaultVersion from tools/unity-versions.json) otherwise — fast PR feedback.
if [[ "$EVENT_NAME" == "workflow_dispatch" ]] || \
[[ "$EVENT_NAME" == "workflow_call" ]] || \
{ [[ "$EVENT_NAME" == "push" ]] && [[ "$GH_REF" == "refs/heads/beta" ]]; } || \
{ [[ "$EVENT_NAME" == "pull_request" ]] && [[ "$FULL_MATRIX_LABEL" == "true" ]]; }; then
versions=$(jq -c '[.versions[].id]' tools/unity-versions.json)
echo "Trigger '$EVENT_NAME' on ref '$GH_REF' (full_matrix_label=$FULL_MATRIX_LABEL) → full matrix: $versions"
else
versions=$(jq -c '[.defaultVersion]' tools/unity-versions.json)
echo "Trigger '$EVENT_NAME' on ref '$GH_REF' → default only: $versions"
fi
echo "versions=$versions" >> "$GITHUB_OUTPUT"
testAllModes:
name: Test in ${{ matrix.testMode }} on Unity ${{ matrix.unityVersion }}
needs: matrix
runs-on: ubuntu-latest
permissions:
contents: read
strategy:
fail-fast: false
matrix:
projectPath:
- TestProjects/UnityMCPTests
testMode:
- editmode
unityVersion: ${{ fromJson(needs.matrix.outputs.versions) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
lfs: true
ref: ${{ inputs.ref || github.event.pull_request.head.sha || github.ref }}
persist-credentials: false
- name: Detect Unity license secrets
id: detect
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
run: |
set -e
if [ -n "$UNITY_LICENSE" ] || { [ -n "$UNITY_EMAIL" ] && [ -n "$UNITY_PASSWORD" ] && [ -n "$UNITY_SERIAL" ]; }; then
echo "unity_ok=true" >> "$GITHUB_OUTPUT"
else
echo "unity_ok=false" >> "$GITHUB_OUTPUT"
fi
# A skipped run and a real pass both report a green check, because step-level
# `if:` conditions produce step-conclusion `skipped`, which contributes nothing
# to the job conclusion. Make the difference unmissable on the run page so a
# reviewer never reads this green check as "the code compiled".
- name: Skip Unity tests (missing license secrets)
if: steps.detect.outputs.unity_ok != 'true'
run: |
echo "::warning::Unity tests SKIPPED - no license secrets in scope (normal for fork PRs). This check is NOT a pass: nothing was compiled or tested."
{
echo "## :warning: Unity tests were SKIPPED"
echo
echo "No Unity license secrets were in scope for this run, so **no C# was compiled and no test was executed**."
echo "The green check means the job exited cleanly - **not** that this code works."
echo
echo "GitHub withholds repository secrets from workflow runs triggered by a fork's pull request."
echo "To get real signal, a maintainer must run the suite against this code from a trusted context."
} >> "$GITHUB_STEP_SUMMARY"
- uses: actions/cache@v4
with:
path: ${{ matrix.projectPath }}/Library
key: Library-${{ matrix.projectPath }}-${{ matrix.unityVersion }}
restore-keys: |
Library-${{ matrix.projectPath }}-
Library-
# Run domain reload tests first (they're [Explicit] so need explicit category)
- name: Run domain reload tests
if: steps.detect.outputs.unity_ok == 'true'
uses: game-ci/unity-test-runner@v4
id: domain-tests
env:
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
with:
projectPath: ${{ matrix.projectPath }}
unityVersion: ${{ matrix.unityVersion }}
testMode: ${{ matrix.testMode }}
customParameters: -testCategory domain_reload
- name: Run tests
if: steps.detect.outputs.unity_ok == 'true'
uses: game-ci/unity-test-runner@v4
id: tests
continue-on-error: true
env:
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
with:
projectPath: ${{ matrix.projectPath }}
unityVersion: ${{ matrix.unityVersion }}
testMode: ${{ matrix.testMode }}
- name: Check test results
if: steps.detect.outputs.unity_ok == 'true'
env:
ARTIFACTS_PATH: ${{ steps.tests.outputs.artifactsPath }}
run: |
set -euo pipefail
# `|| true` so a missing $ARTIFACTS_PATH (Unity crashed before producing any) doesn't trip
# `pipefail` and skip the explicit empty-check diagnostic below.
RESULTS_XML=$(find "$ARTIFACTS_PATH" -name '*.xml' 2>/dev/null | head -1 || true)
if [ -z "$RESULTS_XML" ]; then
echo "::error::No test results XML found — Unity may have crashed"
exit 1
fi
python3 - "$RESULTS_XML" <<'PY'
import sys, xml.etree.ElementTree as ET
# Escape workflow-command payloads so test-controlled XML can't break annotation
# rendering or inject extra workflow commands.
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
def esc_data(s):
return s.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
def esc_prop(s):
return esc_data(s).replace(":", "%3A").replace(",", "%2C")
root = ET.parse(sys.argv[1]).getroot()
totals = root.attrib
passed = totals.get("passed", "?")
failed = totals.get("failed", "?")
total = totals.get("total", "?")
incon = totals.get("inconclusive", "?")
skipped = totals.get("skipped", "?")
print(f"Results: {passed} passed, {failed} failed, {incon} inconclusive, {skipped} skipped (total: {total})")
fails = [tc for tc in root.iter("test-case") if tc.attrib.get("result") == "Failed"]
if not fails:
sys.exit(0)
# Surface every failure inline so a CI watcher doesn't need to download the NUnit XML artifact.
for tc in fails:
name = tc.attrib.get("fullname") or tc.attrib.get("name") or "<unknown>"
f = tc.find("failure")
msg = (f.findtext("message") or "").strip() if f is not None else ""
stack = (f.findtext("stack-trace") or "").strip() if f is not None else ""
# First line of the message becomes the GitHub annotation title.
first_line = msg.splitlines()[0] if msg else "(no message)"
# GitHub annotations don't render multi-line bodies, so emit the full failure inside a collapsible group.
print(f"::error title=Failed: {esc_prop(name)}::{esc_data(first_line)}")
print(f"::group::Failure details — {esc_data(name)}")
if msg:
print("Message:")
print(msg)
if stack:
print("Stack trace:")
print(stack)
print("::endgroup::")
print(f"::error::{len(fails)} test(s) failed")
sys.exit(1)
PY
- uses: actions/upload-artifact@v4
if: always() && steps.detect.outputs.unity_ok == 'true' && steps.tests.outcome != 'skipped'
with:
name: Test results for ${{ matrix.testMode }} on Unity ${{ matrix.unityVersion }}
path: ${{ steps.tests.outputs.artifactsPath }}