Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/__tests__/test-utils/mocked-binaries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { DeviceInfo } from '../../kernel/device.ts';
import { ANDROID_EMULATOR } from './device-fixtures.ts';

/**
* Creates a temporary stub `adb` binary that logs all args to a file,
Expand Down Expand Up @@ -67,3 +69,36 @@ export async function withMockedXcrun(
await fs.rm(tmpDir, { recursive: true, force: true });
}
}

/**
* Like {@link withMockedAdb}, but with a caller-provided stub `adb` script so
* tests can shape per-subcommand responses instead of only recording args.
* The callback also receives the canonical Android emulator device fixture.
*/
export async function withScriptedAdb(
tempPrefix: string,
script: string,
run: (ctx: { argsLogPath: string; device: DeviceInfo }) => Promise<void>,
): Promise<void> {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), tempPrefix));
const adbPath = path.join(tmpDir, 'adb');
const argsLogPath = path.join(tmpDir, 'args.log');
await fs.writeFile(adbPath, script, 'utf8');
await fs.chmod(adbPath, 0o755);

const previousPath = process.env.PATH;
const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;

try {
// Fresh copy per call: tests may tailor the device without leaking
// mutations into the shared fixture.
await run({ argsLogPath, device: { ...ANDROID_EMULATOR } });
} finally {
process.env.PATH = previousPath;
if (previousArgsFile === undefined) delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
else process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
await fs.rm(tmpDir, { recursive: true, force: true });
}
}
Loading
Loading