Skip to content

Commit ee7f81b

Browse files
authored
fix: write android runtime hints via stdin (#203)
1 parent 55d69f7 commit ee7f81b

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/daemon/__tests__/runtime-hints.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ async function withMockedAdb(
1717
argsLogPath: string;
1818
readFilePath: string;
1919
scriptFilePath: string;
20+
stdinFilePath: string;
2021
}) => Promise<void>,
2122
): Promise<void> {
2223
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-runtime-hints-android-'));
2324
const adbPath = path.join(tmpDir, 'adb');
2425
const argsLogPath = path.join(tmpDir, 'args.log');
2526
const readFilePath = path.join(tmpDir, 'existing.xml');
2627
const scriptFilePath = path.join(tmpDir, 'write-script.sh');
28+
const stdinFilePath = path.join(tmpDir, 'write-stdin.xml');
2729
await fs.writeFile(
2830
adbPath,
2931
[
@@ -52,6 +54,7 @@ async function withMockedAdb(
5254
' exit "${AGENT_DEVICE_TEST_RUN_AS_ID_EXIT_CODE:-0}"',
5355
'fi',
5456
'if [ "$1" = "shell" ] && [ "$2" = "run-as" ] && [ "$4" = "sh" ] && [ "$5" = "-c" ]; then',
57+
' cat > "$AGENT_DEVICE_TEST_STDIN_FILE"',
5558
' if [ -n "$AGENT_DEVICE_TEST_RUN_AS_WRITE_STDOUT" ]; then',
5659
' printf "%s" "$AGENT_DEVICE_TEST_RUN_AS_WRITE_STDOUT"',
5760
' fi',
@@ -76,6 +79,7 @@ async function withMockedAdb(
7679
const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
7780
const previousReadFile = process.env.AGENT_DEVICE_TEST_READ_FILE;
7881
const previousScriptFile = process.env.AGENT_DEVICE_TEST_SCRIPT_FILE;
82+
const previousStdinFile = process.env.AGENT_DEVICE_TEST_STDIN_FILE;
7983
const previousRunAsIdExitCode = process.env.AGENT_DEVICE_TEST_RUN_AS_ID_EXIT_CODE;
8084
const previousRunAsIdStdout = process.env.AGENT_DEVICE_TEST_RUN_AS_ID_STDOUT;
8185
const previousRunAsIdStderr = process.env.AGENT_DEVICE_TEST_RUN_AS_ID_STDERR;
@@ -86,6 +90,7 @@ async function withMockedAdb(
8690
process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
8791
process.env.AGENT_DEVICE_TEST_READ_FILE = readFilePath;
8892
process.env.AGENT_DEVICE_TEST_SCRIPT_FILE = scriptFilePath;
93+
process.env.AGENT_DEVICE_TEST_STDIN_FILE = stdinFilePath;
8994

9095
const device: DeviceInfo = {
9196
platform: 'android',
@@ -96,12 +101,13 @@ async function withMockedAdb(
96101
};
97102

98103
try {
99-
await run({ device, argsLogPath, readFilePath, scriptFilePath });
104+
await run({ device, argsLogPath, readFilePath, scriptFilePath, stdinFilePath });
100105
} finally {
101106
process.env.PATH = previousPath;
102107
restoreEnv('AGENT_DEVICE_TEST_ARGS_FILE', previousArgsFile);
103108
restoreEnv('AGENT_DEVICE_TEST_READ_FILE', previousReadFile);
104109
restoreEnv('AGENT_DEVICE_TEST_SCRIPT_FILE', previousScriptFile);
110+
restoreEnv('AGENT_DEVICE_TEST_STDIN_FILE', previousStdinFile);
105111
restoreEnv('AGENT_DEVICE_TEST_RUN_AS_ID_EXIT_CODE', previousRunAsIdExitCode);
106112
restoreEnv('AGENT_DEVICE_TEST_RUN_AS_ID_STDOUT', previousRunAsIdStdout);
107113
restoreEnv('AGENT_DEVICE_TEST_RUN_AS_ID_STDERR', previousRunAsIdStderr);
@@ -175,7 +181,7 @@ test('resolveRuntimeTransportHints derives host, port, and scheme from bundle UR
175181
});
176182

177183
test('applyRuntimeHintsToApp writes React Native Android dev prefs', async () => {
178-
await withMockedAdb(async ({ device, argsLogPath, readFilePath, scriptFilePath }) => {
184+
await withMockedAdb(async ({ device, argsLogPath, readFilePath, scriptFilePath, stdinFilePath }) => {
179185
await fs.writeFile(
180186
readFilePath,
181187
[
@@ -199,11 +205,13 @@ test('applyRuntimeHintsToApp writes React Native Android dev prefs', async () =>
199205

200206
const loggedArgs = await fs.readFile(argsLogPath, 'utf8');
201207
const script = await fs.readFile(scriptFilePath, 'utf8');
208+
const stdinPayload = await fs.readFile(stdinFilePath, 'utf8');
202209
assert.match(loggedArgs, /shell run-as com\.example\.demo cat shared_prefs\/ReactNativeDevPrefs\.xml/);
203210
assert.match(loggedArgs, /shell run-as com\.example\.demo sh -c/);
204-
assert.match(script, /<string name="keep">value<\/string>/);
205-
assert.match(script, /<string name="debug_http_host">10\.0\.0\.10:8082<\/string>/);
206-
assert.match(script, /<boolean name="dev_server_https" value="true" \/>/);
211+
assert.equal(script, 'mkdir -p shared_prefs && cat > shared_prefs/ReactNativeDevPrefs.xml');
212+
assert.match(stdinPayload, /<string name="keep">value<\/string>/);
213+
assert.match(stdinPayload, /<string name="debug_http_host">10\.0\.0\.10:8082<\/string>/);
214+
assert.match(stdinPayload, /<boolean name="dev_server_https" value="true" \/>/);
207215
});
208216
});
209217

@@ -278,7 +286,7 @@ test('applyRuntimeHintsToApp preserves write failures after a successful run-as
278286
});
279287

280288
test('clearRuntimeHintsFromApp removes managed Android runtime prefs but preserves unrelated entries', async () => {
281-
await withMockedAdb(async ({ device, readFilePath, scriptFilePath }) => {
289+
await withMockedAdb(async ({ device, readFilePath, stdinFilePath }) => {
282290
await fs.writeFile(
283291
readFilePath,
284292
[
@@ -298,10 +306,10 @@ test('clearRuntimeHintsFromApp removes managed Android runtime prefs but preserv
298306
appId: 'com.example.demo',
299307
});
300308

301-
const script = await fs.readFile(scriptFilePath, 'utf8');
302-
assert.match(script, /<string name="keep">value<\/string>/);
303-
assert.doesNotMatch(script, /debug_http_host/);
304-
assert.doesNotMatch(script, /dev_server_https/);
309+
const stdinPayload = await fs.readFile(stdinFilePath, 'utf8');
310+
assert.match(stdinPayload, /<string name="keep">value<\/string>/);
311+
assert.doesNotMatch(stdinPayload, /debug_http_host/);
312+
assert.doesNotMatch(stdinPayload, /dev_server_https/);
305313
});
306314
});
307315

src/daemon/runtime-hints.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,10 @@ async function writeAndroidDevPrefs(device: DeviceInfo, packageName: string, xml
144144
);
145145
}
146146

147-
const script = [
148-
'mkdir -p shared_prefs',
149-
`cat > ${ANDROID_DEV_PREFS_PATH} <<'EOF'`,
150-
xml.trimEnd(),
151-
'EOF',
152-
].join('\n');
147+
const script = `mkdir -p shared_prefs && cat > ${ANDROID_DEV_PREFS_PATH}`;
153148
const writeArgs = adbArgs(device, ['shell', 'run-as', packageName, 'sh', '-c', script]);
154149
try {
155-
await runCmd('adb', writeArgs);
150+
await runCmd('adb', writeArgs, { stdin: xml.trimEnd() });
156151
} catch (error) {
157152
const appErr = asAppError(error);
158153
if (appErr.code === 'TOOL_MISSING') throw appErr;

0 commit comments

Comments
 (0)