Skip to content

Commit b367ed1

Browse files
authored
fix: maestro tested app fails to connect to metro on iOS (#1827)
Fixes the application failing to connect to Metro when running Maestro tests with `clearState` command. ### How Has This Been Tested: - open an app in Radon on iOS - try to run a maestro test for the app with `clearState` option passed to the `launchApp` command ``` launchApp: clearState: true ``` - the app should connect to metro ### How Has This Change Been Documented: Bugfix
1 parent a19e5e5 commit b367ed1

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

packages/vscode-extension/shims/maestro/xcrun

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ REAL_XCRUN=$(PATH="${PATH//$SCRIPT_DIR:/}" command -v xcrun)
77

88
# If --set is specified respect that and pass through unchanged
99
for arg in "$@"; do
10-
if [[ "$arg" == "--set" ]]; then
11-
exec "$REAL_XCRUN" "$@"
12-
fi
10+
if [[ "$arg" == "--set" ]]; then
11+
exec "$REAL_XCRUN" "$@"
12+
fi
1313
done
1414

15-
# If the subcommand is 'simctl', inject --set <CUSTOM_DEVICE_SET> after 'simctl'
15+
# If the subcommand is 'simctl', inject --set $__RADON_CUSTOM_DEVICE_SET after 'simctl'
1616
if [[ "$1" == "simctl" ]]; then
17-
exec "$REAL_XCRUN" "$1" --set "$CUSTOM_DEVICE_SET" "${@:2}"
17+
# When launching the app, we may need to pass the metro port as well
18+
if [[ $2 == "launch" && "$*" == *"$__RADON_APP_ID"* ]]; then
19+
exec "$REAL_XCRUN" "$1" --set "$__RADON_CUSTOM_DEVICE_SET" "${@:2}" $__RADON_LAUNCH_ARGS
20+
else
21+
exec "$REAL_XCRUN" "$1" --set "$__RADON_CUSTOM_DEVICE_SET" "${@:2}"
22+
fi
1823
else
19-
exec "$REAL_XCRUN" "$@"
20-
fi
24+
exec "$REAL_XCRUN" "$@"
25+
fi

packages/vscode-extension/src/devices/AndroidDevice.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ export abstract class AndroidDevice extends DeviceBase implements Disposable {
192192
]);
193193
}
194194

195-
public async forwardDevicePort(port: number) {
196-
await exec(ADB_PATH, ["-s", this.serial, "reverse", `tcp:${port}`, `tcp:${port}`]);
195+
public async forwardDevicePort(hostPort: number, devicePort?: number) {
196+
devicePort ??= hostPort;
197+
await exec(ADB_PATH, ["-s", this.serial, "reverse", `tcp:${devicePort}`, `tcp:${hostPort}`]);
197198
}
198199

199200
public setUpKeyboard() {

packages/vscode-extension/src/project/MaestroTestRunner.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getOrCreateDeviceSet, IosSimulatorDevice } from "../devices/IosSimulato
1010
import { extensionContext } from "../utilities/extensionContext";
1111
import { Logger } from "../Logger";
1212
import { getTelemetryReporter } from "../utilities/telemetry";
13+
import { EXPO_GO_BUNDLE_ID, fetchExpoLaunchDeeplink } from "../builders/expoGo";
1314

1415
class MaestroPseudoTerminal implements vscode.Pseudoterminal {
1516
private writeEmitter = new vscode.EventEmitter<string>();
@@ -69,15 +70,16 @@ function getDeviceFamily(deviceInfo: DeviceInfo) {
6970
}
7071

7172
export class MaestroTestRunner implements Disposable {
72-
private readonly device: DeviceBase;
7373
private terminal: vscode.Terminal | undefined;
7474
private pty: MaestroPseudoTerminal | undefined;
7575
private maestroProcess: ChildProcess | undefined;
7676
private onCloseTerminal: vscode.Disposable | undefined;
7777

78-
constructor(device: DeviceBase) {
79-
this.device = device;
80-
}
78+
constructor(
79+
private readonly device: DeviceBase,
80+
private readonly metroPort: number,
81+
private readonly applicationId: string
82+
) {}
8183

8284
private getOrCreateTerminal(): { terminal: vscode.Terminal; pty: MaestroPseudoTerminal } {
8385
if (this.terminal && this.pty) {
@@ -214,6 +216,7 @@ export class MaestroTestRunner implements Disposable {
214216
// similar to what Maestro would do in prebuilt mode, and wrap the xcrun
215217
// command to provide our own device set with the --set flag.
216218
const shimPath = path.resolve(extensionContext.extensionPath, "shims", "maestro");
219+
const launchArgs = await this.setupLaunchArguments();
217220

218221
const maestroProcess = exec("maestro", ["--device", this.device.id, "test", ...fileNames], {
219222
buffer: false,
@@ -222,7 +225,9 @@ export class MaestroTestRunner implements Disposable {
222225
cwd: homedir(),
223226
env: {
224227
PATH: `${shimPath}:${process.env.PATH}`,
225-
CUSTOM_DEVICE_SET: getOrCreateDeviceSet(
228+
__RADON_APP_ID: this.applicationId,
229+
__RADON_LAUNCH_ARGS: launchArgs,
230+
__RADON_CUSTOM_DEVICE_SET: getOrCreateDeviceSet(
226231
this.device instanceof IosSimulatorDevice ? this.device.deviceInfo.id : undefined
227232
),
228233
},
@@ -246,6 +251,21 @@ export class MaestroTestRunner implements Disposable {
246251
await maestroProcess;
247252
}
248253

254+
private async setupLaunchArguments() {
255+
if (this.device.platform !== DevicePlatform.IOS) {
256+
return undefined;
257+
}
258+
259+
const deepLinkChoice = this.applicationId === EXPO_GO_BUNDLE_ID ? "expo-go" : "expo-dev-client";
260+
const expoDeepLink = await fetchExpoLaunchDeeplink(this.metroPort, "ios", deepLinkChoice);
261+
262+
if (expoDeepLink) {
263+
return [`--initialUrl`, expoDeepLink].join(" ");
264+
}
265+
266+
return [`-RCT_jsLocation`, `localhost:${this.metroPort}`].join(" ");
267+
}
268+
249269
private async setupSimulatorSymlink() {
250270
if (this.device.platform !== DevicePlatform.IOS) {
251271
return async () => {

packages/vscode-extension/src/project/applicationSession.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ export class ApplicationSession implements Disposable {
246246
this.disposables.push(this.toolsManager);
247247
this.disposables.push(this.stateManager);
248248

249-
this.maestroTestRunner = new MaestroTestRunner(this.device);
249+
this.maestroTestRunner = new MaestroTestRunner(
250+
this.device,
251+
this.metro.port,
252+
this.packageNameOrBundleId
253+
);
250254
this.disposables.push(this.maestroTestRunner);
251255
}
252256

0 commit comments

Comments
 (0)