Repository navigation
Expand file tree
/
Copy pathrun-tests-ios.js
More file actions
1379 lines (1182 loc) · 45.2 KB
/
Copy pathrun-tests-ios.js
File metadata and controls
1379 lines (1182 loc) · 45.2 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Runs TestRunner on an iOS simulator and streams runtime logs directly.
// This path intentionally avoids XCTest integration so output comes from the app itself.
//
// Optional args:
// - junit output path (defaults to build/test-results/ios-junit.xml)
// - destination (e.g. "platform=iOS Simulator,id=..." or simulator UDID)
//
// Env:
// - IOS_DESTINATION overrides destination argument.
// - IOS_TEST_SKIP_BUILD=1 skips auto-build of iOS simulator runtime artifacts.
// - IOS_TEST_CLEAN_BUILD=1 deletes derived data before building TestRunner.app.
// - IOS_TEST_ENGINE selects the runtime engine build to use when runtime
// artifacts need rebuilding. Supported: v8, hermes, quickjs, jsc. Defaults to v8.
// - IOS_SWIFT_VERSION overrides default Swift version (default: 5.0).
// - IOS_COMMAND_TIMEOUT_MS overrides timeout for build/install/simctl commands (default: 3 minutes).
// - IOS_SIMCTL_QUERY_TIMEOUT_MS overrides timeout for polling simctl queries (default: 10 seconds).
// - IOS_APP_CONTAINER_READY_TIMEOUT_MS overrides how long to wait for simctl to expose app containers (default: 60 seconds).
// - IOS_BUILD_TIMEOUT_MS overrides timeout for xcodebuild app build (default: IOS_COMMAND_TIMEOUT_MS).
// - IOS_COMMAND_MAX_BUFFER_BYTES overrides spawnSync maxBuffer for captured command output (default: 64 MiB).
// - IOS_TEST_TIMEOUT_MS overrides max test runtime (default: 10 minutes).
// - IOS_LOG_JUNIT=1 enables streaming TKUnit/JUnit lines to console.
// - IOS_TESTS filters test modules (comma-separated substrings passed to app as -tests).
// - IOS_SPECS filters individual Jasmine specs by description substring (comma-separated, passed as -specs).
// - IOS_TEST_INACTIVITY_TIMEOUT_MS overrides max no-log interval (default: 2 minutes).
// - IOS_TEST_LOG_STREAM=0 disables parallel simulator log stream (enabled by default).
// - IOS_TEST_VERBOSE_SPECS=1 enables per-spec start/done logs from the app.
// - IOS_SIM_LOG_LOOKBACK sets log-show window used for post-failure diagnostics (default: 45s).
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const crypto = require("crypto");
const projectPath = path.join(__dirname, "../platforms/apple/NativeScriptRuntime.xcodeproj");
const scheme = "TestRunner";
const bundleId = "com.descendra.TestRunner";
const resultsDir = path.join(__dirname, "../build", "test-results");
const defaultJunitPath = path.join(resultsDir, "ios-junit.xml");
const derivedDataPath = path.join(__dirname, "../build", "derived-data", "ios-tests");
const testRunnerAppSourcePath = path.join(__dirname, "../platforms/apple/test/runtime/runner", "app");
const buildStatePath = path.join(derivedDataPath, ".ios-test-build-state.json");
const metadataGeneratorRoot = path.join(__dirname, "../metadata-generator");
const metadataGeneratorBinary = path.join(
metadataGeneratorRoot,
"dist",
"arm64",
"bin",
"objc-metadata-generator"
);
const metadataGeneratorBuildStepScript = path.join(
metadataGeneratorRoot,
"dist",
"arm64",
"bin",
"build-step-metadata-generator.py"
);
const metadataGeneratorSymbolAnalyzer = path.join(
metadataGeneratorRoot,
"dist",
"arm64",
"bin",
"ns-metadata-symbols"
);
const metadataGeneratorSourceInputs = [
path.join(metadataGeneratorRoot, "src"),
path.join(metadataGeneratorRoot, "include"),
path.join(metadataGeneratorRoot, "tests"),
path.join(metadataGeneratorRoot, "symbol-analyzer", "Cargo.toml"),
path.join(metadataGeneratorRoot, "symbol-analyzer", "Cargo.lock"),
path.join(metadataGeneratorRoot, "symbol-analyzer", "src"),
path.join(metadataGeneratorRoot, "symbol-analyzer", "tests"),
path.join(metadataGeneratorRoot, "build-step-metadata-generator.py"),
path.join(metadataGeneratorRoot, "CMakeLists.txt"),
path.join(__dirname, "build_metadata_generator.sh")
];
const nativeScriptSourceRoot = path.join(__dirname, "../NativeScript");
const nativeScriptXCFramework = path.join(__dirname, "../dist", "NativeScript.xcframework");
const tkLiveSyncXCFramework = path.join(__dirname, "../dist", "TKLiveSync.xcframework");
const iosBuildInputs = [
path.join(__dirname, "../platforms/apple/NativeScriptRuntime.xcodeproj", "project.pbxproj"),
path.join(__dirname, "../platforms/apple/test/runtime/runner", "Source Files"),
path.join(__dirname, "../platforms/apple/test/runtime/runner", "Info.plist"),
path.join(__dirname, "../platforms/apple/test/runtime/fixtures"),
path.join(__dirname, "../platforms/apple/TKLiveSync"),
...metadataGeneratorSourceInputs,
nativeScriptXCFramework,
tkLiveSyncXCFramework
];
function parseTimeoutMs(name, fallback) {
const value = Number(process.env[name] || fallback);
if (!Number.isFinite(value) || value <= 0) {
return fallback;
}
return value;
}
function parsePositiveInt(name, fallback) {
const value = Number(process.env[name] || fallback);
if (!Number.isFinite(value) || value <= 0) {
return fallback;
}
return Math.floor(value);
}
const commandTimeoutMs = parseTimeoutMs("IOS_COMMAND_TIMEOUT_MS", 3 * 60 * 1000);
// Clean CI runners often need substantially longer for the first iOS build than
// for simulator control commands like boot/install/log collection.
const buildTimeoutMs = parseTimeoutMs("IOS_BUILD_TIMEOUT_MS", 10 * 60 * 1000);
const simctlQueryTimeoutMs = parseTimeoutMs(
"IOS_SIMCTL_QUERY_TIMEOUT_MS",
Math.min(commandTimeoutMs, 10 * 1000)
);
const appContainerReadyTimeoutMs = parseTimeoutMs(
"IOS_APP_CONTAINER_READY_TIMEOUT_MS",
Math.max(60 * 1000, simctlQueryTimeoutMs * 6)
);
const commandMaxBufferBytes = parsePositiveInt("IOS_COMMAND_MAX_BUFFER_BYTES", 64 * 1024 * 1024);
const testTimeoutMs = Number(process.env.IOS_TEST_TIMEOUT_MS || 10 * 60 * 1000);
const inactivityTimeoutMs = Number(process.env.IOS_TEST_INACTIVITY_TIMEOUT_MS || 2 * 60 * 1000);
const emitJunitLogs = process.env.IOS_LOG_JUNIT === "1";
const requestedTests = (process.env.IOS_TESTS || "").trim();
const requestedSpecs = (process.env.IOS_SPECS || "").trim();
const requestedEngine = (process.env.IOS_TEST_ENGINE || "v8").trim().toLowerCase();
const enableLiveLogStream = process.env.IOS_TEST_LOG_STREAM !== "0";
const verboseSpecLogs = process.env.IOS_TEST_VERBOSE_SPECS === "1";
const simulatorLogLookback = process.env.IOS_SIM_LOG_LOOKBACK || "45s";
const consoleLogMarker = "CONSOLE LOG:";
function looksLikeDestination(value) {
return value.includes("platform=iOS Simulator") || /^[0-9A-Fa-f-]{36}$/.test(value) || value.includes("id=");
}
function parseArgs() {
const args = process.argv.slice(2).filter(Boolean);
let junitOutPath = defaultJunitPath;
let destinationArg;
for (const arg of args) {
if (looksLikeDestination(arg) && !destinationArg) {
destinationArg = arg;
continue;
}
junitOutPath = path.resolve(arg);
}
return { junitOutPath, destinationArg };
}
function run(command, args, options = {}) {
const effectiveTimeout = options.timeout ?? commandTimeoutMs;
const result = cp.spawnSync(command, args, {
encoding: "utf8",
timeout: effectiveTimeout,
maxBuffer: commandMaxBufferBytes,
...options
});
if (result.error) {
if (result.error.code === "ETIMEDOUT") {
throw new Error(`Command timed out after ${effectiveTimeout}ms: ${command} ${args.join(" ")}`);
}
throw result.error;
}
return result;
}
function runAndRequireSuccess(command, args, timeoutMs = commandTimeoutMs) {
const result = cp.spawnSync(command, args, { stdio: "inherit", timeout: timeoutMs });
if (result.error && result.error.code === "ETIMEDOUT") {
console.error(`ERROR: Command timed out after ${timeoutMs}ms: ${command} ${args.join(" ")}`);
process.exit(1);
}
if (result.status !== 0) {
process.exit(result.status || 1);
}
}
function ensureMetadataGeneratorBuilt() {
const sourceMtime = metadataGeneratorSourceInputs.reduce(
(latest, inputPath) => Math.max(latest, getPathStats(inputPath).maxMtimeMs),
0
);
const artifactMtime = Math.min(
getPathStats(metadataGeneratorBinary).maxMtimeMs,
getPathStats(metadataGeneratorSymbolAnalyzer).maxMtimeMs,
getPathStats(metadataGeneratorBuildStepScript).maxMtimeMs
);
if (artifactMtime > 0 && artifactMtime >= sourceMtime) {
return;
}
console.log("Metadata generator is missing or stale; running build-metagen...");
const hostArch = process.arch === "x64" ? "x86_64" : process.arch;
runAndRequireSuccess(
"env",
[`METADATA_GENERATOR_ARCHS=${hostArch}`, "npm", "run", "build-metagen"],
buildTimeoutMs
);
}
function getPathStats(targetPath) {
if (!fs.existsSync(targetPath)) {
return { files: 0, bytes: 0, maxMtimeMs: 0 };
}
const queue = [targetPath];
let files = 0;
let bytes = 0;
let maxMtimeMs = 0;
while (queue.length > 0) {
const currentPath = queue.pop();
let stats;
try {
stats = fs.lstatSync(currentPath);
} catch (_) {
continue;
}
if (stats.mtimeMs > maxMtimeMs) {
maxMtimeMs = stats.mtimeMs;
}
if (stats.isDirectory()) {
const entries = fs.readdirSync(currentPath);
for (const entry of entries) {
queue.push(path.join(currentPath, entry));
}
continue;
}
if (stats.isFile() || stats.isSymbolicLink()) {
files += 1;
bytes += stats.size;
}
}
return { files, bytes, maxMtimeMs };
}
function createBuildFingerprint(inputs) {
const snapshot = inputs.map((inputPath) => ({
path: inputPath,
...getPathStats(inputPath)
}));
return crypto.createHash("sha1").update(JSON.stringify(snapshot)).digest("hex");
}
function readBuildState() {
if (!fs.existsSync(buildStatePath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(buildStatePath, "utf8"));
} catch (_) {
return null;
}
}
function writeBuildState(nativeFingerprint, swiftVersion) {
fs.mkdirSync(path.dirname(buildStatePath), { recursive: true });
fs.writeFileSync(buildStatePath, JSON.stringify({
nativeFingerprint,
swiftVersion,
createdAt: Date.now()
}));
}
function syncAppResources(destinationAppResourcesPath) {
fs.mkdirSync(destinationAppResourcesPath, { recursive: true });
const result = run("rsync", [
"-a",
"--delete",
`${testRunnerAppSourcePath}/`,
`${destinationAppResourcesPath}/`
]);
if (result.status !== 0) {
throw new Error(`Failed to sync TestRunner app resources: ${result.stderr || result.stdout || "unknown rsync error"}`);
}
}
function stripConsoleLogPrefix(line) {
const markerIndex = line.indexOf(consoleLogMarker);
if (markerIndex < 0) {
return line;
}
return line.slice(markerIndex + consoleLogMarker.length).trimStart();
}
function stripConsoleLogPrefixes(text) {
return text.replace(/^.*CONSOLE LOG:\s?/gm, "");
}
function parseRuntimeKey(runtimeKey) {
const match = runtimeKey.match(/iOS[- ](\d+)[- ](\d+)/);
if (!match) {
return { major: 0, minor: 0 };
}
return {
major: Number(match[1]),
minor: Number(match[2])
};
}
function listAvailableIphoneSimulators() {
const simctl = run("xcrun", ["simctl", "list", "devices", "--json"]);
if (simctl.status !== 0) {
throw new Error(simctl.stderr || "Failed to list simulators");
}
const json = JSON.parse(simctl.stdout);
const devicesByRuntime = json.devices || {};
const devices = [];
for (const runtimeKey of Object.keys(devicesByRuntime)) {
if (!runtimeKey.includes("iOS")) {
continue;
}
for (const device of devicesByRuntime[runtimeKey] || []) {
if (!device.isAvailable || !device.name.startsWith("iPhone")) {
continue;
}
devices.push({
runtimeKey,
runtimeVersion: parseRuntimeKey(runtimeKey),
udid: device.udid,
name: device.name,
state: device.state
});
}
}
devices.sort((a, b) => {
if (b.runtimeVersion.major !== a.runtimeVersion.major) {
return b.runtimeVersion.major - a.runtimeVersion.major;
}
if (b.runtimeVersion.minor !== a.runtimeVersion.minor) {
return b.runtimeVersion.minor - a.runtimeVersion.minor;
}
return a.name.localeCompare(b.name);
});
return devices;
}
function pickPreferredSimulator(candidates) {
if (candidates.length === 0) {
throw new Error("No available iPhone simulator found.");
}
const preferredDeviceNames = [
"iPhone 16e",
"iPhone 16",
"iPhone 16 Plus",
"iPhone 16 Pro",
"iPhone 16 Pro Max",
"iPhone 15",
"iPhone 15 Plus",
"iPhone 15 Pro",
"iPhone 15 Pro Max"
];
const score = (device) => {
const preferredIndex = preferredDeviceNames.indexOf(device.name);
if (preferredIndex >= 0) {
return preferredIndex;
}
if (device.name.includes("Pro")) {
return 10_000;
}
return 5_000;
};
const sorted = [...candidates].sort((a, b) => score(a) - score(b));
return sorted.find((d) => d.state === "Booted") || sorted[0];
}
function normalizeDestinationArg(destinationArg) {
if (!destinationArg) {
return undefined;
}
if (/^[0-9A-Fa-f-]{36}$/.test(destinationArg)) {
return `platform=iOS Simulator,id=${destinationArg}`;
}
return destinationArg;
}
function resolveDestinationAndSimulator(destinationArg) {
const normalized = normalizeDestinationArg(destinationArg);
const devices = listAvailableIphoneSimulators();
if (!normalized) {
const selected = pickPreferredSimulator(devices);
return {
destination: `platform=iOS Simulator,id=${selected.udid}`,
simulator: selected
};
}
const idMatch = normalized.match(/id=([0-9A-Fa-f-]{36})/);
if (idMatch) {
const byId = devices.find((d) => d.udid.toLowerCase() === idMatch[1].toLowerCase());
if (!byId) {
throw new Error(`Requested simulator not found or unavailable: ${idMatch[1]}`);
}
return {
destination: `platform=iOS Simulator,id=${byId.udid}`,
simulator: byId
};
}
const nameMatch = normalized.match(/name=([^,]+)/);
if (nameMatch) {
const requestedName = nameMatch[1];
const sameName = devices.filter((d) => d.name === requestedName);
if (sameName.length === 0) {
throw new Error(`Requested simulator name not found or unavailable: ${requestedName}`);
}
const selected = sameName[0];
return {
destination: `platform=iOS Simulator,id=${selected.udid}`,
simulator: selected
};
}
throw new Error(`Unsupported destination format: ${normalized}`);
}
function bootSimulator(udid) {
run("xcrun", ["simctl", "boot", udid]);
const bootstatus = run("xcrun", ["simctl", "bootstatus", udid, "-b"], { stdio: "inherit" });
if (bootstatus.status !== 0) {
process.exit(bootstatus.status || 1);
}
}
function hasSimulatorSlice(xcframeworkPath) {
if (!fs.existsSync(xcframeworkPath)) {
return false;
}
const entries = fs.readdirSync(xcframeworkPath, { withFileTypes: true });
return entries.some((entry) => entry.isDirectory() && entry.name.includes("simulator"));
}
function buildTKLiveSyncSimulatorXCFramework() {
const intermediates = path.join(__dirname, "../dist", "intermediates", "ios-test-tklivesync");
const sourcePath = path.join(intermediates, "dummy.c");
const frameworkPath = path.join(intermediates, "TKLiveSync.framework");
const arm64Binary = path.join(intermediates, "TKLiveSync-arm64");
const x64Binary = path.join(intermediates, "TKLiveSync-x86_64");
const fatBinary = path.join(frameworkPath, "TKLiveSync");
const infoPlistPath = path.join(frameworkPath, "Info.plist");
fs.rmSync(intermediates, { recursive: true, force: true });
fs.mkdirSync(intermediates, { recursive: true });
fs.mkdirSync(frameworkPath, { recursive: true });
fs.writeFileSync(sourcePath, "void TKLivesyncDummy(void) {}\n");
console.log("Building minimal TKLiveSync simulator binary for test linking...");
runAndRequireSuccess("xcrun", [
"--sdk", "iphonesimulator",
"clang",
"-target", "arm64-apple-ios13.0-simulator",
"-dynamiclib",
sourcePath,
"-install_name", "@rpath/TKLiveSync.framework/TKLiveSync",
"-o", arm64Binary
]);
runAndRequireSuccess("xcrun", [
"--sdk", "iphonesimulator",
"clang",
"-target", "x86_64-apple-ios13.0-simulator",
"-dynamiclib",
sourcePath,
"-install_name", "@rpath/TKLiveSync.framework/TKLiveSync",
"-o", x64Binary
]);
runAndRequireSuccess("xcrun", [
"lipo",
"-create",
arm64Binary,
x64Binary,
"-output",
fatBinary
]);
fs.writeFileSync(infoPlistPath, `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key><string>org.nativescript.TKLiveSync</string>
<key>CFBundleExecutable</key><string>TKLiveSync</string>
<key>CFBundleName</key><string>TKLiveSync</string>
<key>CFBundlePackageType</key><string>FMWK</string>
<key>CFBundleShortVersionString</key><string>1.0</string>
<key>CFBundleVersion</key><string>1</string>
</dict>
</plist>
`);
fs.rmSync(tkLiveSyncXCFramework, { recursive: true, force: true });
runAndRequireSuccess("xcodebuild", [
"-create-xcframework",
"-framework", frameworkPath,
"-output", tkLiveSyncXCFramework
]);
}
function ensureIOSSimulatorArtifacts() {
const cachePath = path.join(__dirname, "../dist", "intermediates", "ios-sim", "CMakeCache.txt");
const sourceInputs = [
nativeScriptSourceRoot,
path.join(__dirname, "build_nativescript.sh")
];
const sourceMtime = sourceInputs.reduce(
(latest, inputPath) => Math.max(latest, getPathStats(inputPath).maxMtimeMs),
0
);
const artifactMtime = getPathStats(nativeScriptXCFramework).maxMtimeMs;
const hasNativeScriptSimulator = hasSimulatorSlice(nativeScriptXCFramework);
const hasTKLiveSyncSimulator = hasSimulatorSlice(tkLiveSyncXCFramework);
let configuredEngine = null;
if (fs.existsSync(cachePath)) {
try {
const cache = fs.readFileSync(cachePath, "utf8");
const match = cache.match(/^TARGET_ENGINE:STRING=(.+)$/m);
if (match) {
configuredEngine = match[1].trim().toLowerCase();
}
} catch (_) {
configuredEngine = null;
}
}
const supportedEngines = new Set(["v8", "hermes", "quickjs", "jsc"]);
if (!supportedEngines.has(requestedEngine)) {
throw new Error(`Unsupported IOS_TEST_ENGINE: ${requestedEngine}`);
}
const needsNativeScriptRebuild =
artifactMtime === 0 ||
artifactMtime < sourceMtime ||
configuredEngine !== requestedEngine;
if (!hasNativeScriptSimulator || needsNativeScriptRebuild) {
console.log(
`NativeScript simulator artifacts are missing, stale, or built for '${configuredEngine ?? "unknown"}'; running ${requestedEngine} build...`
);
runAndRequireSuccess(path.join(__dirname, "build_nativescript.sh"), [
"--no-iphone",
"--simulator",
"--no-macos",
`--${requestedEngine}`
], buildTimeoutMs);
}
if (!hasTKLiveSyncSimulator) {
buildTKLiveSyncSimulatorXCFramework();
}
}
function buildTestRunnerApp(destination, swiftVersion) {
const appPath = path.join(derivedDataPath, "Build", "Products", "Debug-iphonesimulator", "TestRunner.app");
const appResourcesPath = path.join(appPath, "app");
if (process.env.IOS_TEST_CLEAN_BUILD === "1") {
fs.rmSync(derivedDataPath, { recursive: true, force: true });
}
ensureMetadataGeneratorBuilt();
const nativeFingerprint = `${requestedEngine}:${createBuildFingerprint(iosBuildInputs)}`;
const existingBuildState = readBuildState();
const canReuseBuild = process.env.IOS_TEST_CLEAN_BUILD !== "1" &&
fs.existsSync(appPath) &&
existingBuildState &&
existingBuildState.nativeFingerprint === nativeFingerprint &&
existingBuildState.swiftVersion === swiftVersion;
console.log("Building...");
if (!canReuseBuild) {
const args = [
"-project", projectPath,
"-scheme", scheme,
"-configuration", "Debug",
"-quiet",
"-destination", destination,
"-destination-timeout", "120",
"-derivedDataPath", derivedDataPath,
`SWIFT_VERSION=${swiftVersion}`,
"CODE_SIGN_STYLE=Manual",
"CODE_SIGNING_ALLOWED=NO",
"CODE_SIGNING_REQUIRED=NO",
"CODE_SIGN_IDENTITY=",
"DEVELOPMENT_TEAM=",
"build"
];
const result = cp.spawnSync("xcodebuild", args, {
encoding: "utf8",
timeout: buildTimeoutMs,
maxBuffer: commandMaxBufferBytes
});
if (result.error && result.error.code === "ETIMEDOUT") {
if (result.stdout && result.stdout.trim().length > 0) {
console.error(result.stdout.trimEnd());
}
if (result.stderr && result.stderr.trim().length > 0) {
console.error(result.stderr.trimEnd());
}
throw new Error(`xcodebuild timed out after ${buildTimeoutMs}ms while building TestRunner.`);
}
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
if (result.stdout && result.stdout.trim().length > 0) {
console.error(result.stdout.trimEnd());
}
if (result.stderr && result.stderr.trim().length > 0) {
console.error(result.stderr.trimEnd());
}
throw new Error(`xcodebuild failed while building TestRunner (exit ${result.status}).`);
}
}
syncAppResources(appResourcesPath);
writeBuildState(nativeFingerprint, swiftVersion);
if (!fs.existsSync(appPath)) {
throw new Error(`Built app not found at expected path: ${appPath}`);
}
return { appPath, reusedBuild: canReuseBuild };
}
const appContainerPathCache = new Map();
function getAppContainerPath(udid, containerType, options = {}) {
const cacheKey = `${udid}:${containerType}`;
if (appContainerPathCache.has(cacheKey)) {
return appContainerPathCache.get(cacheKey);
}
let result;
try {
result = run("xcrun", ["simctl", "get_app_container", udid, bundleId, containerType], {
timeout: options.timeout ?? simctlQueryTimeoutMs
});
} catch (error) {
if (!options.quiet) {
console.warn(`WARNING: Unable to resolve ${containerType} container yet (${error.message}).`);
}
return null;
}
if (result.status !== 0) {
if (!options.quiet) {
const stderr = (result.stderr || "").trim();
console.warn(
`WARNING: Unable to resolve ${containerType} container yet (simctl exited ${result.status}${stderr ? `: ${stderr}` : ""}).`
);
}
return null;
}
const out = (result.stdout || "").trim();
if (out) {
appContainerPathCache.set(cacheKey, out);
}
return out || null;
}
function getAppDataContainerPath(udid, options = {}) {
return getAppContainerPath(udid, "data", options);
}
function getInstalledAppPath(udid, options = {}) {
return getAppContainerPath(udid, "app", options);
}
async function waitForAppContainerPath(udid, containerType, timeoutMs = appContainerReadyTimeoutMs) {
const deadline = Date.now() + timeoutMs;
let attempt = 0;
while (Date.now() < deadline) {
const remaining = Math.max(1, deadline - Date.now());
const containerPath = getAppContainerPath(udid, containerType, {
quiet: attempt > 0,
timeout: Math.min(simctlQueryTimeoutMs, remaining)
});
if (containerPath) {
return containerPath;
}
attempt += 1;
await sleep(Math.min(250 * attempt, 2000));
}
console.warn(`WARNING: ${containerType} container was not available after ${timeoutMs}ms.`);
return null;
}
function waitForAppDataContainerPath(udid, timeoutMs) {
return waitForAppContainerPath(udid, "data", timeoutMs);
}
function removeOldJunitFile(udid, options = {}) {
const dataContainer = getAppDataContainerPath(udid, options);
if (!dataContainer) {
return;
}
const junitPath = path.join(dataContainer, "Documents", "junit-result.xml");
if (fs.existsSync(junitPath)) {
fs.rmSync(junitPath, { force: true });
}
}
function startLaunchWithConsole(udid, appArgs = [], useConsolePty = true, captureOutput = true) {
const args = [
"simctl",
"launch",
"--terminate-running-process",
udid,
bundleId
];
if (useConsolePty) {
args.splice(2, 0, "--console-pty");
}
if (appArgs.length > 0) {
args.push("--args", ...appArgs);
}
console.log(`Launching app and streaming logs: xcrun ${args.join(" ")}`);
return cp.spawn("xcrun", args, {
stdio: captureOutput ? ["ignore", "pipe", "pipe"] : "ignore"
});
}
function wireOutput(launchProcess, state) {
const onStdout = (chunk) => {
const text = chunk.toString();
state.lastActivityAt = Date.now();
state.logs += text;
if (state.logs.length > 4 * 1024 * 1024) {
state.logs = state.logs.slice(state.logs.length - 2 * 1024 * 1024);
}
process.stdout.write(stripConsoleLogPrefixes(text));
};
const onStderr = (chunk) => {
const text = chunk.toString();
state.lastActivityAt = Date.now();
state.logs += text;
if (state.logs.length > 4 * 1024 * 1024) {
state.logs = state.logs.slice(state.logs.length - 2 * 1024 * 1024);
}
process.stderr.write(stripConsoleLogPrefixes(text));
};
launchProcess.stdout.on("data", onStdout);
launchProcess.stderr.on("data", onStderr);
}
function startSimulatorLogStream(udid) {
const args = [
"simctl",
"spawn",
udid,
"log",
"stream",
"--style",
"compact",
"--predicate",
'process == "TestRunner"'
];
console.log(`Streaming simulator logs: xcrun ${args.join(" ")}`);
return cp.spawn("xcrun", args, {
stdio: ["ignore", "pipe", "pipe"]
});
}
function stripAnsi(text) {
return text.replace(/\u001b\[[0-9;]*m/g, "");
}
function wireLogStreamOutput(logStreamProcess, state) {
const buffers = {
stdout: "",
stderr: ""
};
const includeLine = (normalized) => {
return normalized.includes("CONSOLE LOG:") ||
normalized.includes("SPEC START:") ||
normalized.includes("SPEC DONE:") ||
normalized.includes("TKUnit:") ||
normalized.includes("Application Start!") ||
normalized.includes("SUCCESS:") ||
normalized.includes("FAILURE:") ||
normalized.includes("NativeScriptException") ||
/Uncaught Exception/i.test(normalized) ||
/EXC_BAD_ACCESS|EXC_CRASH|Abort trap/i.test(normalized) ||
/heap corruption|malloc: \*\*\*/i.test(normalized) ||
normalized.includes("Using metadata from pointer");
};
const fatalPattern = /NativeScriptException::OnUncaughtError|Uncaught Exception|heap corruption|EXC_BAD_ACCESS|EXC_CRASH|Abort trap|malloc: \*\*\*/i;
const flushLine = (line, streamName) => {
if (!line) {
return;
}
const normalized = stripAnsi(line);
state.recentRawLines.push(normalized);
if (state.recentRawLines.length > 400) {
state.recentRawLines.splice(0, state.recentRawLines.length - 400);
}
if (!includeLine(normalized)) {
return;
}
state.logs += normalized + "\n";
state.lastActivityAt = Date.now();
if (state.logs.length > 4 * 1024 * 1024) {
state.logs = state.logs.slice(state.logs.length - 2 * 1024 * 1024);
}
if (state.jasmineSummary == null) {
const summary = parseJasmineSummary(normalized);
if (summary) {
state.jasmineSummary = summary;
}
}
if (fatalPattern.test(normalized)) {
state.fatalDetected = true;
}
const displayLine = stripConsoleLogPrefix(line);
if (streamName === "stderr") {
process.stderr.write(displayLine + "\n");
} else {
process.stdout.write(displayLine + "\n");
}
};
const onChunk = (streamName, chunk) => {
const key = streamName;
buffers[key] += chunk.toString();
const lines = buffers[key].split(/\r?\n/);
buffers[key] = lines.pop() || "";
for (const line of lines) {
flushLine(line, streamName);
}
};
logStreamProcess.stdout.on("data", (chunk) => onChunk("stdout", chunk));
logStreamProcess.stderr.on("data", (chunk) => onChunk("stderr", chunk));
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function readCompletedJunitFileIfPresent(udid) {
const dataContainer = getAppDataContainerPath(udid);
if (!dataContainer) {
return null;
}
const junitPath = path.join(dataContainer, "Documents", "junit-result.xml");
if (!fs.existsSync(junitPath)) {
return null;
}
const xml = fs.readFileSync(junitPath, "utf8");
if (!xml.includes("</testsuites>")) {
return null;
}
return { xml, junitPath };
}
function waitForLaunchProcessClose(launchProcess, timeoutMs) {
if (launchProcess.exitCode !== null && launchProcess.exitCode !== undefined) {
return Promise.resolve({
code: launchProcess.exitCode ?? 0,
signal: launchProcess.signalCode || null
});
}
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
if (launchProcess.exitCode === null || launchProcess.exitCode === undefined) {
launchProcess.kill("SIGTERM");
}
reject(new Error(`Timed out waiting for TestRunner process to exit (${timeoutMs}ms).`));
}, timeoutMs);
launchProcess.on("close", (code, signal) => {
clearTimeout(timer);
resolve({ code: code ?? 0, signal: signal || null });
});
launchProcess.on("error", (error) => {
clearTimeout(timer);
reject(error);
});
});
}
async function waitForCompletedJunitOrLaunchExit(udid, launchProcess, timeoutMs, state) {
let launchResult = null;
launchProcess.on("close", (code, signal) => {
launchResult = { code: code ?? 0, signal: signal || null };
});
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (state.jasmineSummary) {
return { junitResult: null, launchResult, timedOut: false };
}
if (state.fatalDetected) {
return { junitResult: null, launchResult, timedOut: false };
}
const junitResult = readCompletedJunitFileIfPresent(udid);
if (junitResult) {
return { junitResult, launchResult, timedOut: false };
}
if (Date.now() - state.lastActivityAt >= inactivityTimeoutMs) {
const launchPid = extractLaunchPid(state.logs);
const appRunning = isAppProcessRunning(udid, launchPid);
if (appRunning === false) {
return { junitResult: null, launchResult, timedOut: true, inactive: true };
}
}
await sleep(250);
}
return { junitResult: null, launchResult, timedOut: true, inactive: false };
}
function extractLaunchPid(logs) {
const match = logs.match(/com\.descendra\.TestRunner:\s*(\d+)/);
return match ? Number(match[1]) : null;
}
function collectRecentSimulatorLogs(udid, pid) {
const predicate = Number.isInteger(pid)
? `processID == ${pid}`
: 'process == "TestRunner"';
const result = run("xcrun", [
"simctl",
"spawn",
udid,
"log",
"show",
"--style",
"compact",
"--last",
simulatorLogLookback,
"--predicate",
predicate
]);