-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtest-e2e.mts
More file actions
125 lines (107 loc) · 3.56 KB
/
test-e2e.mts
File metadata and controls
125 lines (107 loc) · 3.56 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
/**
* Reminder that this script is meant to be runnable without installing
* dependencies. It can therefore not rely on any external libraries.
*/
import { spawnSync } from "node:child_process";
import { Socket } from "node:net";
import { isMain } from "../helpers.js";
/**
* Invokes a shell command with optional arguments.
*/
export function $(command: string, ...args: string[]) {
const { status } = spawnSync(command, args, {
stdio: "inherit",
shell: true, // Yarn won't be able to find commands otherwise
});
if (status !== 0) {
throw new Error(
`An error occurred while executing: ${command} ${args.join(" ")}`
);
}
}
/**
* Invokes a shell command with optional arguments. Similar {@link $}, but
* captures and returns stdout/stderr.
*/
export function $$(command: string, ...args: string[]): string {
const { status, stderr, stdout } = spawnSync(command, args, {
stdio: ["ignore", "pipe", "pipe"],
shell: process.platform === "win32",
encoding: "utf-8",
});
if (status !== 0) {
throw new Error(
`An error occurred while executing: ${command} ${args.join(" ")}`
);
}
// Some commands, like `appium driver list --installed` output to stderr
return stdout.trim() || stderr.trim();
}
/**
* Ensures Appium is available.
*/
function ensureAppiumAvailable(): Promise<void> {
return new Promise((resolve, reject) => {
const socket = new Socket();
socket.setTimeout(60 * 1000);
const onError = (e: Error) => {
socket.destroy();
reject(new Error(`Could not connect to Appium server: ${e}`));
};
socket.once("error", onError);
socket.once("timeout", onError);
socket.connect(4723, "127.0.0.1", () => {
socket.end();
resolve();
});
});
}
function prepareAndroid(androidHome = process.env["ANDROID_HOME"]) {
// Note: Ubuntu agents can't run Android emulators — see
// https://github.com/actions/runner-images/issues/6253#issuecomment-1255952240
const adb = androidHome + "/platform-tools/adb";
/*
const android_image = "system-images;android-30;google_atd;x86_64";
const avdmanager = androidHome + "/cmdline-tools/latest/bin/avdmanager";
const emulator = androidHome + "/emulator/emulator";
const sdkmanager = androidHome + "/cmdline-tools/latest/bin/sdkmanager";
if [[ -n $CI ]]; then
# Accept all licenses so we can download an Android image
yes 2> /dev/null | $sdkmanager --licenses
$sdkmanager --install "$android_image"
# Create an Android emulator and boot it up
echo "no" | $avdmanager create avd --package "$android_image" --name Android_E2E
$emulator @Android_E2E -delay-adb -partition-size 4096 -no-snapshot -no-audio -no-boot-anim -no-window -gpu swiftshader_indirect &
fi
*/
// Wait for the emulator to boot up before we install the app
$(adb, "wait-for-device");
$(adb, "install", "android/app/build/outputs/apk/debug/app-debug.apk");
}
export async function test(target: string, args: string[] = []) {
switch (target) {
case "android":
prepareAndroid();
break;
case "ios":
break;
default:
console.error(`Unknown target: ${target}`);
return 1;
}
await ensureAppiumAvailable();
const tests = $$("git", "ls-files", "*.spec.mjs").split("\n");
try {
process.env["TEST_ARGS"] = `${target} ${args.join(" ")}`;
$(process.argv[0], "--test", ...tests);
} finally {
delete process.env["TEST_ARGS"];
}
return 0;
}
const [, , target, ...args] = process.argv;
if (isMain(import.meta.url)) {
test(target, args).then((exitCode) => {
process.exitCode = exitCode;
});
}