Skip to content

Commit 5e6a200

Browse files
committed
refactor: initialize Android env during runner setup
1 parent ca1f17a commit 5e6a200

5 files changed

Lines changed: 80 additions & 93 deletions

File tree

packages/platform-android/src/adb.ts

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { type AndroidAppLaunchOptions } from '@react-native-harness/platforms';
22
import { spawn, SubprocessError } from '@react-native-harness/tools';
3-
import { withAndroidProcessEnv } from './environment.js';
43

54
const wait = async (ms: number): Promise<void> => {
65
await new Promise((resolve) => {
@@ -66,11 +65,15 @@ export const isAppInstalled = async (
6665
adbId: string,
6766
bundleId: string
6867
): Promise<boolean> => {
69-
const { stdout } = await spawn(
70-
'adb',
71-
['-s', adbId, 'shell', 'pm', 'list', 'packages', bundleId],
72-
withAndroidProcessEnv()
73-
);
68+
const { stdout } = await spawn('adb', [
69+
'-s',
70+
adbId,
71+
'shell',
72+
'pm',
73+
'list',
74+
'packages',
75+
bundleId,
76+
]);
7477
return stdout.trim() !== '';
7578
};
7679

@@ -79,22 +82,20 @@ export const reversePort = async (
7982
port: number,
8083
hostPort: number = port
8184
): Promise<void> => {
82-
await spawn(
83-
'adb',
84-
['-s', adbId, 'reverse', `tcp:${port}`, `tcp:${hostPort}`],
85-
withAndroidProcessEnv()
86-
);
85+
await spawn('adb', [
86+
'-s',
87+
adbId,
88+
'reverse',
89+
`tcp:${port}`,
90+
`tcp:${hostPort}`,
91+
]);
8792
};
8893

8994
export const stopApp = async (
9095
adbId: string,
9196
bundleId: string
9297
): Promise<void> => {
93-
await spawn(
94-
'adb',
95-
['-s', adbId, 'shell', 'am', 'force-stop', bundleId],
96-
withAndroidProcessEnv()
97-
);
98+
await spawn('adb', ['-s', adbId, 'shell', 'am', 'force-stop', bundleId]);
9899
};
99100

100101
export const startApp = async (
@@ -103,15 +104,15 @@ export const startApp = async (
103104
activityName: string,
104105
options?: AndroidAppLaunchOptions
105106
): Promise<void> => {
106-
await spawn(
107-
'adb',
108-
['-s', adbId, ...getStartAppArgs(bundleId, activityName, options)],
109-
withAndroidProcessEnv()
110-
);
107+
await spawn('adb', [
108+
'-s',
109+
adbId,
110+
...getStartAppArgs(bundleId, activityName, options),
111+
]);
111112
};
112113

113114
export const getDeviceIds = async (): Promise<string[]> => {
114-
const { stdout } = await spawn('adb', ['devices'], withAndroidProcessEnv());
115+
const { stdout } = await spawn('adb', ['devices']);
115116
return stdout
116117
.split('\n')
117118
.slice(1) // Skip header
@@ -122,23 +123,21 @@ export const getDeviceIds = async (): Promise<string[]> => {
122123
export const getEmulatorName = async (
123124
adbId: string
124125
): Promise<string | null> => {
125-
const { stdout } = await spawn(
126-
'adb',
127-
['-s', adbId, 'emu', 'avd', 'name'],
128-
withAndroidProcessEnv()
129-
);
126+
const { stdout } = await spawn('adb', ['-s', adbId, 'emu', 'avd', 'name']);
130127
return stdout.split('\n')[0].trim() || null;
131128
};
132129

133130
export const getShellProperty = async (
134131
adbId: string,
135132
property: string
136133
): Promise<string | null> => {
137-
const { stdout } = await spawn(
138-
'adb',
139-
['-s', adbId, 'shell', 'getprop', property],
140-
withAndroidProcessEnv()
141-
);
134+
const { stdout } = await spawn('adb', [
135+
'-s',
136+
adbId,
137+
'shell',
138+
'getprop',
139+
property,
140+
]);
142141
return stdout.trim() || null;
143142
};
144143

@@ -161,7 +160,7 @@ export const isBootCompleted = async (adbId: string): Promise<boolean> => {
161160
};
162161

163162
export const stopEmulator = async (adbId: string): Promise<void> => {
164-
await spawn('adb', ['-s', adbId, 'emu', 'kill'], withAndroidProcessEnv());
163+
await spawn('adb', ['-s', adbId, 'emu', 'kill']);
165164
};
166165

167166
export const installApp = async (
@@ -267,11 +266,13 @@ export const isAppRunning = async (
267266
bundleId: string
268267
): Promise<boolean> => {
269268
try {
270-
const { stdout } = await spawn(
271-
'adb',
272-
['-s', adbId, 'shell', 'pidof', bundleId],
273-
withAndroidProcessEnv()
274-
);
269+
const { stdout } = await spawn('adb', [
270+
'-s',
271+
adbId,
272+
'shell',
273+
'pidof',
274+
bundleId,
275+
]);
275276
return stdout.trim() !== '';
276277
} catch (error) {
277278
if (error instanceof SubprocessError && error.exitCode === 1) {
@@ -286,11 +287,15 @@ export const getAppUid = async (
286287
adbId: string,
287288
bundleId: string
288289
): Promise<number> => {
289-
const { stdout } = await spawn(
290-
'adb',
291-
['-s', adbId, 'shell', 'pm', 'list', 'packages', '-U'],
292-
withAndroidProcessEnv()
293-
);
290+
const { stdout } = await spawn('adb', [
291+
'-s',
292+
adbId,
293+
'shell',
294+
'pm',
295+
'list',
296+
'packages',
297+
'-U',
298+
]);
294299
const line = stdout
295300
.split('\n')
296301
.find((entry) => entry.includes(`package:${bundleId}`));
@@ -307,39 +312,33 @@ export const setHideErrorDialogs = async (
307312
adbId: string,
308313
hide: boolean
309314
): Promise<void> => {
310-
await spawn(
311-
'adb',
312-
[
313-
'-s',
314-
adbId,
315-
'shell',
316-
'settings',
317-
'put',
318-
'global',
319-
'hide_error_dialogs',
320-
hide ? '1' : '0',
321-
],
322-
withAndroidProcessEnv()
323-
);
315+
await spawn('adb', [
316+
'-s',
317+
adbId,
318+
'shell',
319+
'settings',
320+
'put',
321+
'global',
322+
'hide_error_dialogs',
323+
hide ? '1' : '0',
324+
]);
324325
};
325326

326327
export const getLogcatTimestamp = async (adbId: string): Promise<string> => {
327-
const { stdout } = await spawn(
328-
'adb',
329-
['-s', adbId, 'shell', 'date', "+'%m-%d %H:%M:%S.000'"],
330-
withAndroidProcessEnv()
331-
);
328+
const { stdout } = await spawn('adb', [
329+
'-s',
330+
adbId,
331+
'shell',
332+
'date',
333+
"+'%m-%d %H:%M:%S.000'",
334+
]);
332335

333336
return stdout.trim().replace(/^'+|'+$/g, '');
334337
};
335338

336339
export const getAvds = async (): Promise<string[]> => {
337340
try {
338-
const { stdout } = await spawn(
339-
'emulator',
340-
['-list-avds'],
341-
withAndroidProcessEnv()
342-
);
341+
const { stdout } = await spawn('emulator', ['-list-avds']);
343342
return stdout
344343
.split('\n')
345344
.map((line) => line.trim())
@@ -356,11 +355,7 @@ export type AdbDevice = {
356355
};
357356

358357
export const getConnectedDevices = async (): Promise<AdbDevice[]> => {
359-
const { stdout } = await spawn(
360-
'adb',
361-
['devices', '-l'],
362-
withAndroidProcessEnv()
363-
);
358+
const { stdout } = await spawn('adb', ['devices', '-l']);
364359
const lines = stdout.split('\n').slice(1);
365360
const devices: AdbDevice[] = [];
366361

packages/platform-android/src/app-monitor.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from '@react-native-harness/tools';
1717
import * as adb from './adb.js';
1818
import { androidCrashParser } from './crash-parser.js';
19-
import { withAndroidProcessEnv } from './environment.js';
2019

2120
const androidAppMonitorLogger = logger.child('android-app-monitor');
2221

@@ -452,7 +451,6 @@ export const createAndroidAppMonitor = ({
452451
{
453452
stdout: 'pipe',
454453
stderr: 'pipe',
455-
...withAndroidProcessEnv(),
456454
}
457455
);
458456

packages/platform-android/src/environment.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os from 'node:os';
22
import path from 'node:path';
3-
import type { SpawnOptions } from '@react-native-harness/tools';
43

54
const CMDLINE_TOOLS_PATH_SEGMENTS = ['cmdline-tools', 'latest'];
65

@@ -39,9 +38,6 @@ export const getAndroidProcessEnv = (
3938
};
4039
};
4140

42-
export const withAndroidProcessEnv = (
43-
options?: SpawnOptions
44-
): SpawnOptions => ({
45-
...options,
46-
env: getAndroidProcessEnv(options?.env),
47-
});
41+
export const initializeAndroidProcessEnv = (): void => {
42+
Object.assign(process.env, getAndroidProcessEnv());
43+
};

packages/platform-android/src/runner.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import {
99
getAndroidEmulatorPlatformInstance,
1010
getAndroidPhysicalDevicePlatformInstance,
1111
} from './instance.js';
12+
import { initializeAndroidProcessEnv } from './environment.js';
1213

1314
const getAndroidRunner = async (
1415
config: AndroidPlatformConfig,
1516
harnessConfig: HarnessConfig
1617
): Promise<HarnessPlatformRunner> => {
1718
const parsedConfig = AndroidPlatformConfigSchema.parse(config);
1819

20+
initializeAndroidProcessEnv();
21+
1922
if (isAndroidDeviceEmulator(parsedConfig.device)) {
2023
return getAndroidEmulatorPlatformInstance(parsedConfig, harnessConfig);
2124
}

packages/platform-android/src/shared-prefs.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { spawn, SubprocessError } from '@react-native-harness/tools';
2-
import { withAndroidProcessEnv } from './environment.js';
32

43
const DEBUG_HTTP_HOST_BLOCK_START =
54
'<!-- react-native-harness:debug_http_host:start -->';
@@ -114,16 +113,12 @@ const readSharedPrefsFile = async (
114113
bundleId: string
115114
): Promise<string | null> => {
116115
try {
117-
const { stdout } = await spawn(
118-
'adb',
119-
[
120-
'-s',
121-
adbId,
122-
'shell',
123-
`run-as ${bundleId} cat ${getSharedPrefsPath(bundleId)}`,
124-
],
125-
withAndroidProcessEnv()
126-
);
116+
const { stdout } = await spawn('adb', [
117+
'-s',
118+
adbId,
119+
'shell',
120+
`run-as ${bundleId} cat ${getSharedPrefsPath(bundleId)}`,
121+
]);
127122
return stdout;
128123
} catch (error) {
129124
if (error instanceof SubprocessError && error.exitCode === 1) {
@@ -149,7 +144,7 @@ const writeSharedPrefsFile = async (
149144
bundleId
150145
)}'`,
151146
],
152-
withAndroidProcessEnv({ stdin: { string: `${content.trim()}\n` } })
147+
{ stdin: { string: `${content.trim()}\n` } }
153148
);
154149
};
155150

0 commit comments

Comments
 (0)