-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathemerge-snapshots
More file actions
executable file
·270 lines (232 loc) · 8.47 KB
/
emerge-snapshots
File metadata and controls
executable file
·270 lines (232 loc) · 8.47 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
#!/usr/bin/env bash
#
# TODO: Potentially put this behind a CLI
#
#
# Benefits pulled from Emerge's internal iOS snapshot infrastructure:
#
# - Crash report collection: after any xcodebuild failure, captures .ips files from
# ~/Library/Logs/DiagnosticReports scoped to the test run window
# - Retry on crash: distinguishes crash failures from legitimate test failures and
# retries automatically (configurable via max_retries)
# - Zero-size image detection: scans extracted PNGs with sips after xcresulttool
# export to catch renders that "succeed" but produce empty/corrupt images
# - Multi-device orchestration: runs against multiple simulators (iPhone, iPad, OS
# versions) and collects results per-device before aggregating
# - Permission reset before run: calls xcrun simctl privacy reset all before each
# run to prevent permission dialogs from blocking renders
# - Config file support: YAML config for scheme, test target, bundle ID, and
# destinations — no bash knowledge required for customers
# - Simulator lifecycle management: boots simulators, waits for ready state, and
# tears down cleanly between runs
# - Structured summary manifest: emits a JSON manifest with per-device image counts,
# attempt numbers, and any captured crash report filenames
#
# Potential follow-ups (not yet implemented):
# - Regex-based exclusion filtering: filter extracted PNGs by filename pattern before
# upload, driven by exclusion rules in the config file
# - Per-test timing: extract per-test duration from xcresult and include in the
# manifest so Sentry can surface slow previews
# - Image metadata enrichment: emit a sidecar JSON per image with device name, OS
# version, orientation, and color scheme for richer Sentry UI context
# - Sharding: split the test suite across multiple CI machines by enumerating test
# method names (via xcodebuild build-for-testing + xctest list), accepting
# shard/num_shards config params, and merging per-shard image directories
# afterward; requires the customer's test target to expose one method per preview
set -euo pipefail
CONFIG_FILE=".github/emerge-snapshots.yml"
OUTPUT_DIR="snapshot-images"
XCRESULT_DIR=".."
while [[ $# -gt 0 ]]; do
case "$1" in
--config) CONFIG_FILE="$2"; shift 2 ;;
--output-dir) OUTPUT_DIR="$2"; shift 2 ;;
--xcresult-dir) XCRESULT_DIR="$2"; shift 2 ;;
--help|-h) echo "Usage: emerge-snapshots [--config PATH] [--output-dir PATH] [--xcresult-dir PATH]"; exit 0 ;;
*) echo "Unknown argument: $1"; exit 1 ;;
esac
done
[[ -f "$CONFIG_FILE" ]] || { echo "Config not found: $CONFIG_FILE"; exit 1; }
parse_config() {
ruby -ryaml -e "
c = YAML.load_file(ARGV[0])
%w[scheme test_target bundle_id].each { |k| abort(\"#{k} required\") unless c[k] }
puts \"SCHEME=#{c['scheme']}\"
puts \"TEST_TARGET=#{c['test_target']}\"
puts \"BUNDLE_ID=#{c['bundle_id']}\"
puts \"MAX_RETRIES=#{c.fetch('max_retries', 1)}\"
puts '---'
(c['destinations'] || []).select { |d| d['enabled'] }.each do |d|
puts [d['name'], d['sdk'], d['xcode_flags']].join('|')
end
" "$1"
}
config_output=$(parse_config "$CONFIG_FILE")
eval "$(echo "$config_output" | awk '/^---$/{exit} {print}')"
DESTINATIONS=()
while IFS= read -r line; do
DESTINATIONS+=("$line")
done < <(echo "$config_output" | awk '/^---$/{found=1; next} found{print}')
[[ ${#DESTINATIONS[@]} -gt 0 ]] || { echo "No enabled destinations in config"; exit 1; }
shopt -s nullglob
TOTAL_IMAGE_COUNT=0
declare -a DEVICE_SUMMARIES=()
declare -a CAPTURED_CRASH_REPORTS=()
boot_simulator() {
local device_name="$1"
xcrun simctl boot "$device_name" || true
xcrun simctl bootstatus "$device_name" -b
CURRENT_UDID=$(xcrun simctl list devices booted --json \
| jq -r '.devices | to_entries[] | .value[] | select(.state == "Booted") | .udid' \
| head -1)
}
reset_simulator_permissions() {
local udid="$1"
local bundle_id="$2"
if [ -z "$udid" ]; then
echo "Warning: UDID empty, skipping permission reset"
return 0
fi
xcrun simctl privacy "$udid" reset all "$bundle_id"
}
run_tests() {
local device_name="$1"
local sdk="$2"
local xcode_flags="$3"
local result_path="$4"
rm -rf "$result_path"
touch /tmp/snapshot_test_start_marker
TEST_START_EPOCH=$(date +%s)
set +e
set -o pipefail
xcodebuild test \
-scheme "$SCHEME" \
-sdk "$sdk" \
-destination "platform=iOS Simulator,name=${device_name}" \
-only-testing:"$TEST_TARGET" \
-resultBundlePath "$result_path" \
$xcode_flags \
ONLY_ACTIVE_ARCH=YES \
SUPPORTS_MACCATALYST=NO \
| xcpretty
local exit_code=$?
set -e
return $exit_code
}
collect_crash_reports() {
local dest_dir="$1"
[ -d ~/Library/Logs/DiagnosticReports ] || return 0
local count=0
mkdir -p "$dest_dir"
while IFS= read -r -d '' ips_file; do
cp "$ips_file" "$dest_dir/"
CAPTURED_CRASH_REPORTS+=("$(basename "$ips_file")")
count=$((count + 1))
done < <(find ~/Library/Logs/DiagnosticReports -name "*.ips" -newer /tmp/snapshot_test_start_marker -print0 2>/dev/null)
echo "Crash reports collected: $count"
return $count
}
extract_images() {
local result_path="$1"
local dest_dir="$2"
xcrun xcresulttool export attachments \
--path "$result_path" \
--output-path "$dest_dir"
}
check_zero_size_images() {
local dir="$1"
command -v sips &>/dev/null || return 0
for f in "$dir"/*.png; do
local width
width=$(sips -g pixelWidth "$f" | awk '/pixelWidth/{print $2}')
if [ -z "$width" ] || [ "$width" -eq 0 ]; then
echo "Error: Zero-size image detected: $f"
exit 1
fi
done
}
run_device() {
local device_name="$1"
local sdk="$2"
local xcode_flags="$3"
local label="$4"
boot_simulator "$device_name"
reset_simulator_permissions "$CURRENT_UDID" "$BUNDLE_ID"
local attempt=1
local exit_code=0
local xcresult=""
while true; do
xcresult="$XCRESULT_DIR/SnapshotResults-${label}-attempt${attempt}.xcresult"
run_tests "$device_name" "$sdk" "$xcode_flags" "$xcresult" || exit_code=$?
if [ "$exit_code" -eq 0 ]; then
break
fi
local crash_count=0
collect_crash_reports "$OUTPUT_DIR/crash-reports" || crash_count=$?
if [ "$crash_count" -gt 0 ] && [ "$attempt" -le "$MAX_RETRIES" ]; then
echo "Crash detected (attempt $attempt), retrying..."
attempt=$((attempt + 1))
exit_code=0
continue
else
exit 1
fi
done
extract_images "$xcresult" "$XCRESULT_DIR/snapshots-$label"
check_zero_size_images "$XCRESULT_DIR/snapshots-$label"
local copied=0
for png in "$XCRESULT_DIR/snapshots-$label/"*.png; do
cp "$png" "$OUTPUT_DIR/"
copied=$((copied + 1))
done
TOTAL_IMAGE_COUNT=$((TOTAL_IMAGE_COUNT + copied))
DEVICE_SUMMARIES+=("${label}|${device_name}|${copied}|${attempt}")
}
emit_summary() {
echo ""
echo "=== Snapshot Capture Summary ==="
printf "%-35s %-8s %-9s\n" "Device" "Images" "Attempts"
printf "%-35s %-8s %-9s\n" "------" "------" "--------"
local devices_json="[]"
for entry in ${DEVICE_SUMMARIES[@]+"${DEVICE_SUMMARIES[@]}"}; do
IFS='|' read -r label name count attempts <<< "$entry"
printf "%-35s %-8s %-9s\n" "$name" "$count" "$attempts"
if command -v jq &>/dev/null; then
devices_json=$(echo "$devices_json" | jq \
--arg label "$label" \
--arg name "$name" \
--argjson count "$count" \
--argjson attempts "$attempts" \
'. + [{"label": $label, "name": $name, "image_count": $count, "attempts": $attempts}]')
fi
done
echo "--------------------------------"
echo "Total images: $TOTAL_IMAGE_COUNT"
if ! command -v jq &>/dev/null; then
echo "Warning: jq not available, skipping manifest write"
return 0
fi
local crash_json="[]"
for cr in ${CAPTURED_CRASH_REPORTS[@]+"${CAPTURED_CRASH_REPORTS[@]}"}; do
crash_json=$(echo "$crash_json" | jq --arg cr "$cr" '. + [$cr]')
done
jq --null-input \
--arg ts "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
--argjson devices "$devices_json" \
--argjson total "$TOTAL_IMAGE_COUNT" \
--argjson crashes "$crash_json" \
'{
generated_at: $ts,
devices: $devices,
total_image_count: $total,
crash_reports_captured: $crashes
}' > "$OUTPUT_DIR/snapshot-manifest.json"
}
mkdir -p "$OUTPUT_DIR"
for dest in "${DESTINATIONS[@]}"; do
IFS='|' read -r device_name sdk xcode_flags <<< "$dest"
label=$(echo "$device_name" | tr ' (),' '-' | tr '[:upper:]' '[:lower:]' | tr -s '-')
run_device "$device_name" "$sdk" "$xcode_flags" "$label"
done
emit_summary
echo "Done. $TOTAL_IMAGE_COUNT images in $OUTPUT_DIR/"