-
Notifications
You must be signed in to change notification settings - Fork 15
141 lines (118 loc) · 5.03 KB
/
ios-test-workflow.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
name: iOS Tests
on:
pull_request:
jobs:
test:
name: Run Xcode Tests
runs-on: macos-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode.app
- name: Get Available Destination
id: get-destination
run: |
# Get all available destinations and save to a file
xcodebuild -scheme "Xendit-Package" -showdestinations | tee destinations.txt
# Print all destinations for debugging
echo "Available destinations:"
cat destinations.txt
# Extract simulator with latest OS (works for both iPhone and iPhone SE)
DESTINATION=$(grep -E "platform:iOS Simulator.*name:iPhone" destinations.txt | \
sort -t':' -k4,4V | \
tail -n1 | \
sed -e 's/^[[:space:]]*//' | \
sed -e 's/[[:space:]]*$//' | \
sed -e 's/[{}]//g' | \
sed -e 's/platform:/platform=/g' | \
sed -e 's/id:/id=/g' | \
sed -e 's/, /,/g' | \
sed -e 's/,OS.*//g' | \
xargs)
if [ -z "$DESTINATION" ]; then
echo "Error: No iPhone simulator destination found!"
echo "Available destinations were:"
cat destinations.txt
exit 1
fi
# Set the destination as an output
echo "DESTINATION=$DESTINATION" >> "$GITHUB_OUTPUT"
# Print the selected destination for logging
echo "Selected destination: $DESTINATION"
- name: Build and Test
run: |
DESTINATION="${{ steps.get-destination.outputs.DESTINATION }}"
echo "Using destination: $DESTINATION"
xcodebuild test \
-scheme "Xendit-Package" \
-configuration Debug \
-destination "$DESTINATION" \
-enableCodeCoverage YES \
-resultBundlePath TestResults.xcresult \
clean test | xcpretty --report junit && exit ${PIPESTATUS[0]}
- name: Generate Test Report
if: success() || failure()
run: |
xcrun xcresulttool get --format json --path TestResults.xcresult > test_report.json
xcrun xccov view --report --json TestResults.xcresult > coverage_report.json
- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: test-results
path: |
test_report.json
coverage_report.json
TestResults.xcresult
build/reports/
destinations.txt
- name: Create Test Summary
if: always()
run: |
echo "### Test Results Summary" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo "Using destination: ${{ steps.get-destination.outputs.DESTINATION }}" >> $GITHUB_STEP_SUMMARY
if [ -f "TestResults.xcresult" ]; then
# Get test results summary
echo "### Test Results" >> $GITHUB_STEP_SUMMARY
xcrun xcresulttool get --format human-readable --path TestResults.xcresult >> $GITHUB_STEP_SUMMARY
# Get failed tests if any
echo "### Failed Tests" >> $GITHUB_STEP_SUMMARY
xcrun xcresulttool get --format json --path TestResults.xcresult | \
jq -r '.. | select(.identifier? == "com.apple.xcode.tests.failed")? | .._message?' >> $GITHUB_STEP_SUMMARY
# Get code coverage if enabled
echo "### Code Coverage" >> $GITHUB_STEP_SUMMARY
xcrun xccov view --report --json TestResults.xcresult | \
jq -r '.targets[] | "- \(.name): \(.lineCoverage)%"' >> $GITHUB_STEP_SUMMARY
else
echo "❌ No test results found" >> $GITHUB_STEP_SUMMARY
fi
- name: Comment PR with Results
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const testReport = JSON.parse(fs.readFileSync('test_report.json', 'utf8'));
let message = '';
const failures = testReport.actions.testsRef.tests
.filter(test => test.status === 'Failure')
.map(test => `- ${test.identifier}: ${test.message}`);
if (failures.length > 0) {
message = `### ❌ Test Failures\n\n${failures.join('\n')}`;
} else {
const coverage = JSON.parse(fs.readFileSync('coverage_report.json', 'utf8'));
const coverageSummary = coverage.targets
.map(target => `- ${target.name}: ${(target.lineCoverage * 100).toFixed(2)}%`)
.join('\n');
message = `### ✅ All Tests Passed!\n\n` +
`**Code Coverage:**\n${coverageSummary}\n\n` +
`Total tests: ${testReport.actions.testsRef.tests.length}`;
}
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: message
});