-
Notifications
You must be signed in to change notification settings - Fork 0
200 lines (173 loc) · 6.76 KB
/
UnitTestsAndCodeCoverage.yml
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
name: Unit Tests And Code Coverage
on:
pull_request:
branches:
- main
jobs:
testAllModes:
name: Test in ${{ matrix.testMode }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
projectPath:
- temp_package
unityVersion: [2022.3.21f1]
testMode:
- playmode
- editmode
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create temp_package directory
run: mkdir temp_package
- name: Move package to temp_package directory
run: |
shopt -s extglob
mv !(temp_package|.github) temp_package/
mv .github temp_package/.github
- name: Verify package.json existence
run: |
if [ ! -f temp_package/package.json ]; then
echo "package.json not found in temp_package"
exit 1
fi
- name: Get runner's uid and gid
id: runner-info
run: |
echo "uid=$(id -u)" >> $GITHUB_ENV
echo "gid=$(id -g)" >> $GITHUB_ENV
- name: Run Unity Tests
uses: game-ci/unity-test-runner@v4
id: tests
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
with:
packageMode: true
projectPath: ${{ matrix.projectPath }}
unityVersion: ${{ matrix.unityVersion }}
testMode: ${{ matrix.testMode }}
artifactsPath: ${{ matrix.testMode }}-artifacts
githubToken: ${{ secrets.GITHUB_TOKEN }}
checkName: ${{ matrix.testMode }} Test Results
coverageOptions: 'generateAdditionalMetrics;generateHtmlReport;generateBadgeReport;assemblyFilters:+Soobak.SoobakUnityLibrary.*'
- name: Upload Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: Test results for ${{ matrix.testMode }}
path: ${{ matrix.testMode }}-artifacts
- name: Upload Coverage Results
uses: actions/upload-artifact@v4
if: always()
with:
name: Coverage results for ${{ matrix.testMode }}
path: CodeCoverage/
- name: Upload Badge Coverage Report
uses: actions/upload-artifact@v4
if: always()
with:
name: Badge coverage report for ${{ matrix.testMode }}
path: CodeCoverage/Report/badge_linecoverage.svg
- name: Check if Summary.xml Exists
id: check_summary
run: |
if [ -f CodeCoverage/Report/Summary.xml ]; then
echo "SUMMARY_EXISTS=true" >> $GITHUB_ENV
else
echo "SUMMARY_EXISTS=false" >> $GITHUB_ENV
fi
- name: Setup Node.js
if: env.SUMMARY_EXISTS == 'true'
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Install Dependencies
if: env.SUMMARY_EXISTS == 'true'
run: npm install xml2js @actions/github@latest
- name: Create Node.js Script
if: env.SUMMARY_EXISTS == 'true'
run: |
cat << EOF > script.js
const fs = require('fs');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const xml = fs.readFileSync('CodeCoverage/Report/Summary.xml', 'utf8');
const { context, getOctokit } = require('@actions/github');
const github = getOctokit(process.env.GITHUB_TOKEN);
async function run() {
try {
const summary = await parseXML();
const commentBody = createCommentBody(summary);
await deleteExistingCommentAndCreateNew(commentBody);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
function parseXML() {
return new Promise((resolve, reject) => {
parser.parseString(xml, (err, result) => {
if (err) reject(err);
else resolve(result.CoverageReport.Summary[0]);
});
});
}
function createCommentBody(summary) {
const lineCoverage = \${summary.Linecoverage[0]} === "100" ?
`\$\\textbf{\\color{green}{\${summary.Linecoverage[0]}\\%}}` :
`\$\\textbf{\\color{red}{\${summary.Linecoverage[0]}\\%}}`;
const methodCoverage = \${summary.Methodcoverage[0]} === "100" ?
`\$\\textbf{\\color{green}{\${summary.Methodcoverage[0]}\\%}}` :
`\$\\textbf{\\color{red}{\${summary.Methodcoverage[0]}\\%}}`;
return `
## 🍉 Code Coverage Summary 🧐
| Metric | Value |
|----------------------|------------------------------|
| Generated on | \${summary.Generatedon[0]} |
| Assemblies | \${summary.Assemblies[0]} |
| Classes | \${summary.Classes[0]} |
| Files | \${summary.Files[0]} |
| Covered Lines | \${summary.Coveredlines[0]} |
| Uncovered Lines | \${summary.Uncoveredlines[0]} |
| Coverable Lines | \${summary.Coverablelines[0]} |
| Total Lines | \${summary.Totallines[0]} |
| Line Coverage | \${lineCoverage} |
| Covered Methods | \${summary.Coveredmethods[0]} |
| Total Methods | \${summary.Totalmethods[0]} |
| Method Coverage | \${methodCoverage} |
---
`;
}
async function deleteExistingCommentAndCreateNew(body) {
const { data: comments } = await github.rest.issues.listComments({
...context.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Code Coverage Summary')
);
if (botComment) {
await github.rest.issues.deleteComment({
...context.repo,
comment_id: botComment.id,
});
console.log('Deleted existing comment');
}
await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body,
});
console.log('Created new comment');
}
run();
EOF
- name: Run Node.js Script
if: env.SUMMARY_EXISTS == 'true'
run: node script.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}