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
4 changes: 2 additions & 2 deletions src/drivers/android/adb-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export class AdbTools {
/** why: a copy's AVD sits in its own folder under the root; a device made before copies sat at `<root>/<avdName>.avd`. */
avdFolder(data: AndroidDriverData): string {
return data.avdHome === undefined
? `${this.#options.deviceRoot}/${data.avdName}.avd`
: `${data.avdHome}/${SEED_AVD_NAME}.avd`;
? join(this.#options.deviceRoot, `${data.avdName}.avd`)
: join(data.avdHome, `${SEED_AVD_NAME}.avd`);
}

dataFor(device: DriverDevice): AndroidDriverData {
Expand Down
10 changes: 6 additions & 4 deletions src/drivers/android/avd-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { join } from "node:path";

import { DriverCrashError } from "../../core/index.js";
import { isMissingPathError, type Filesystem, type IdGenerator } from "../../ports/index.js";
import type { AdbTools } from "./adb-tools.js";
Expand Down Expand Up @@ -38,7 +40,7 @@ export class AvdConfig {
},
(line) => !isSeedHardwareLine(line),
);
const bootPropPath = `${folder}/boot.prop`;
const bootPropPath = join(folder, "boot.prop");
const properties = Object.entries(profile.bootProperties);
if (properties.length > 0) {
await this.#options.filesystem.writeFileAtomic(
Expand Down Expand Up @@ -76,7 +78,7 @@ export class AvdConfig {

async #avdConfig(folder: string): Promise<string> {
try {
const contents = await this.#options.filesystem.readFile(`${folder}/config.ini`);
const contents = await this.#options.filesystem.readFile(join(folder, "config.ini"));
return (
contents
.split(/\r?\n/)
Expand Down Expand Up @@ -122,7 +124,7 @@ export class AvdConfig {
);
}
}
const path = `${folder}/config.ini`;
const path = join(folder, "config.ini");
let contents: string;
try {
contents = await this.#options.filesystem.readFile(path);
Expand Down Expand Up @@ -159,7 +161,7 @@ export class AvdConfig {

async readDurableMark(folder: string): Promise<string | undefined> {
try {
const contents = await this.#options.filesystem.readFile(`${folder}/config.ini`);
const contents = await this.#options.filesystem.readFile(join(folder, "config.ini"));
const line = contents
.split(/\r?\n/)
.find((entry) => entry.startsWith(`${DURABLE_MARK_KEY}=`));
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/android/device-table.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from "node:path";
import { describe, expect, it } from "vitest";

import {
Expand Down Expand Up @@ -545,7 +546,7 @@ describe("DeviceTable", () => {

await expect(table.findLegacy("simlock_old")).resolves.toMatchObject({
device: { deviceId: "simlock_old" },
path: `${home}/.android/avd/simlock_old.avd`,
path: join(home, ".android", "avd", "simlock_old.avd"),
});
await expect(table.findLegacy("simlock_never_existed")).resolves.toBeUndefined();
});
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/android/device-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ export class DeviceTable {
for (const entry of await filesystem.readdir(deviceRoot)) {
const old = /^(.+)\.avd$/.exec(entry);
if (old?.[1] !== undefined) {
entries.push({ avdName: old[1], deviceId: old[1], folder: `${deviceRoot}/${entry}` });
entries.push({ avdName: old[1], deviceId: old[1], folder: join(deviceRoot, entry) });
continue;
}
const folder = `${deviceRoot}/${entry}/${SEED_AVD_NAME}.avd`;
const folder = join(deviceRoot, entry, `${SEED_AVD_NAME}.avd`);
if (await this.#holdsAvd(folder)) {
entries.push({ avdName: SEED_AVD_NAME, deviceId: entry, folder });
}
Expand Down Expand Up @@ -319,7 +319,7 @@ export class DeviceTable {
deviceId: owner.deviceId,
driverData: {
...(owner.avdName === SEED_AVD_NAME
? { avdHome: `${this.#options.deviceRoot}/${owner.deviceId}` }
? { avdHome: join(this.#options.deviceRoot, owner.deviceId) }
: {}),
avdName: owner.avdName,
configHash: "recovered",
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/android/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class Emulator {
}

#logPath(data: AndroidDriverData): string {
return `${this.#options.adbTools.avdFolder(data)}/emulator.log`;
return join(this.#options.adbTools.avdFolder(data), "emulator.log");
}

/** why: bounded, since an emulator that ran a while may have written a lot before it exited. */
Expand Down
34 changes: 25 additions & 9 deletions src/drivers/android/index-boot.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { join } from "node:path";
import { promiseState } from "../../test-support/promise-state.js";
import {
FakeClock,
Expand Down Expand Up @@ -32,6 +33,7 @@ import {
RecordingRunner,
consolePortOf,
copyPathExpectation,
withWindowsExtension,
} from "./testing.js";
import { sdk, home, buildDriver } from "./test-index.js";

Expand Down Expand Up @@ -95,16 +97,30 @@ describe("AndroidDriver", () => {
const versionedTools = `${sdk}/cmdline-tools/19.0/bin`;
await filesystem.rm(`${sdk}/cmdline-tools/latest`);
await filesystem.mkdirp(versionedTools);
await filesystem.writeFileAtomic(`${versionedTools}/avdmanager`, "binary");
await filesystem.writeFileAtomic(`${versionedTools}/sdkmanager`, "binary");
const versionedAvdmanager = withWindowsExtension(
`${versionedTools}/avdmanager`,
process.platform,
".bat",
);
await filesystem.writeFileAtomic(versionedAvdmanager, "binary");
await filesystem.writeFileAtomic(
withWindowsExtension(`${versionedTools}/sdkmanager`, process.platform, ".bat"),
"binary",
);

const legacyTools = `${sdk}/tools/bin`;
await filesystem.mkdirp(legacyTools);
await filesystem.writeFileAtomic(`${legacyTools}/avdmanager`, "binary");
await filesystem.writeFileAtomic(`${legacyTools}/sdkmanager`, "binary");
await filesystem.writeFileAtomic(
withWindowsExtension(`${legacyTools}/avdmanager`, process.platform, ".bat"),
"binary",
);
await filesystem.writeFileAtomic(
withWindowsExtension(`${legacyTools}/sdkmanager`, process.platform, ".bat"),
"binary",
);

const runner = new EmulatorExitingRunner([
processResult(`${versionedTools}/avdmanager`, ["list", "device"], pixelDevices),
processResult(versionedAvdmanager, ["list", "device"], pixelDevices),
]);
const driver = await createDriver(filesystem, runner);

Expand Down Expand Up @@ -317,9 +333,9 @@ describe("AndroidDriver", () => {
await driver.makeReady(await driver.provision(spec));

expect(crashDatabases).toEqual([
`${avdDirectory}/emu-crash.db`,
`${avdDirectory}/emu-crash.db`,
`${avdDirectory}/emu-crash.db`,
join(avdDirectory, "emu-crash.db"),
join(avdDirectory, "emu-crash.db"),
join(avdDirectory, "emu-crash.db"),
]);
const crashReportMode = (args: readonly string[]) => {
const flag = args.indexOf("-crash-report-mode");
Expand Down Expand Up @@ -562,7 +578,7 @@ describe("AndroidDriver", () => {

it("sends the emulator's output to <avd folder>/emulator.log, mode 0600, emptied in place at each start", async () => {
const harness = await provisionedHarness();
const logPath = `${avdDirectory}/simlock_one/simlock.avd/emulator.log`;
const logPath = join(avdDirectory, "simlock_one", "simlock.avd", "emulator.log");
await harness.filesystem.writeFileAtomic(logPath, "left by an earlier start\n", {
mode: 0o644,
});
Expand Down
21 changes: 11 additions & 10 deletions src/drivers/android/index-copies-recovery.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { join } from "node:path";
import { FakeClock, MemoryFilesystem, type ScriptedProcessExpectation } from "../../ports/index.js";
import { type AndroidDriverDiagnostic, type AndroidEmulatorLaunchOptions } from "./index.js";
import {
Expand All @@ -24,9 +25,9 @@ import { sdk } from "./test-index.js";

describe("AndroidDriver image fit", () => {
describe("seeds and copies", () => {
const sources = `${avdDirectory}/sources`;
const copyHome = `${avdDirectory}/simlock_one`;
const seedHome = `${sources}/seed_two`;
const sources = join(avdDirectory, "sources");
const copyHome = join(avdDirectory, "simlock_one");
const seedHome = join(sources, "seed_two");
async function seedHarness(
expectations: readonly ScriptedProcessExpectation[],
options: {
Expand Down Expand Up @@ -160,7 +161,7 @@ describe("AndroidDriver image fit", () => {
expect(await destroyed.filesystem.exists(seedHome)).toBe(false);
await expect(destroyed.driver.listSources()).resolves.toEqual([]);

const otherHome = `${sources}/seed_device-4`;
const otherHome = join(sources, "seed_device-4");
const other = await seedHarness([
...makingTheSeed(),
processResult(binaries.avdmanager, ["list", "device"], pixelDevices),
Expand Down Expand Up @@ -256,9 +257,9 @@ describe("AndroidDriver image fit", () => {
processResult(binaries.adb, ["-s", serial, "emu", "kill"]),
processResult(binaries.avdmanager, ["delete", "avd", "-n", "simlock_old"]),
]);
await harness.filesystem.mkdirp(`${avdDirectory}/simlock_old.avd`);
await harness.filesystem.mkdirp(join(avdDirectory, "simlock_old.avd"));
await harness.filesystem.writeFileAtomic(
`${avdDirectory}/simlock_old.avd/config.ini`,
join(avdDirectory, "simlock_old.avd", "config.ini"),
"hw.ramSize=2048\n",
);
const legacy = {
Expand Down Expand Up @@ -305,7 +306,7 @@ describe("AndroidDriver image fit", () => {
};
const harness = async (expectations: readonly ScriptedProcessExpectation[]) => {
const filesystem = await androidFilesystem();
for (const home of [copyHome, `${avdDirectory}/simlock_two`]) {
for (const home of [copyHome, join(avdDirectory, "simlock_two")]) {
await filesystem.mkdirp(`${home}/simlock.avd`);
await filesystem.writeFileAtomic(`${home}/simlock.avd/config.ini`, "hw.ramSize=2048\n");
}
Expand All @@ -327,7 +328,7 @@ describe("AndroidDriver image fit", () => {
]);
await passes.driver.destroy(own);
expect(await passes.filesystem.exists(copyHome)).toBe(false);
expect(await passes.filesystem.exists(`${avdDirectory}/simlock_two`)).toBe(true);
expect(await passes.filesystem.exists(join(avdDirectory, "simlock_two"))).toBe(true);

const neighbour = await harness([
processResult(
Expand Down Expand Up @@ -363,7 +364,7 @@ describe("AndroidDriver image fit", () => {
]);
failingDelete.filesystem.defineFailure(copyHome, "EACCES");
await expect(failingDelete.driver.destroy(own)).rejects.toMatchObject({ code: "EACCES" });
expect(await failingDelete.filesystem.exists(`${avdDirectory}/simlock_two`)).toBe(true);
expect(await failingDelete.filesystem.exists(join(avdDirectory, "simlock_two"))).toBe(true);

const outside = await harness([]);
await outside.filesystem.mkdirp("/elsewhere/simlock_two");
Expand All @@ -375,7 +376,7 @@ describe("AndroidDriver image fit", () => {
}),
).rejects.toThrow("Android device simlock_two names a folder outside the root");
expect(await outside.filesystem.exists("/elsewhere/simlock_two")).toBe(true);
expect(await outside.filesystem.exists(`${avdDirectory}/simlock_two`)).toBe(true);
expect(await outside.filesystem.exists(join(avdDirectory, "simlock_two"))).toBe(true);

const malformed = await harness([]);
await expect(
Expand Down
49 changes: 28 additions & 21 deletions src/drivers/android/index-image-fit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ live(

describe("AndroidDriver image fit", () => {
describe("seeds and copies", () => {
const sources = `${avdDirectory}/sources`;
const copyHome = `${avdDirectory}/simlock_one`;
const seedHome = `${sources}/seed_two`;
const sources = join(avdDirectory, "sources");
const copyHome = join(avdDirectory, "simlock_one");
const seedHome = join(sources, "seed_two");
async function seedHarness(
expectations: readonly ScriptedProcessExpectation[],
options: {
Expand Down Expand Up @@ -194,11 +194,11 @@ describe("AndroidDriver image fit", () => {
deviceId: "simlock_one",
driverData: { avdHome: copyHome, avdName: "simlock" },
});
await expect(harness.filesystem.readFile(`${copyHome}/simlock.avd/config.ini`)).resolves.toBe(
"hw.ramSize=2048\nsimlock.mark=three\n",
);
await expect(harness.filesystem.readFile(`${copyHome}/simlock.ini`)).resolves.toBe(
`avd.ini.encoding=UTF-8\npath=${copyHome}/simlock.avd\ntarget=android-34\n`,
await expect(
harness.filesystem.readFile(join(copyHome, "simlock.avd", "config.ini")),
).resolves.toBe("hw.ramSize=2048\nsimlock.mark=three\n");
await expect(harness.filesystem.readFile(join(copyHome, "simlock.ini"))).resolves.toBe(
`avd.ini.encoding=UTF-8\npath=${join(copyHome, "simlock.avd")}\ntarget=android-34\n`,
);
await expect(harness.driver.listSources()).resolves.toMatchObject([
{
Expand Down Expand Up @@ -264,7 +264,7 @@ describe("AndroidDriver image fit", () => {
});

describe("saveSource", () => {
const savedHome = `${sources}/baseline_four`;
const savedHome = join(sources, "baseline_four");
const serial = "emulator-5586";
const config =
"hw.device.name=pixel_5\nimage.sysdir.1=system-images/android-34/google_apis/arm64-v8a/\nhw.ramSize=2048\n";
Expand Down Expand Up @@ -292,7 +292,7 @@ describe("AndroidDriver image fit", () => {
const probe = processResult(
binaries.adb,
["-s", serial, "emu", "avd", "path"],
`${copyHome}/simlock.avd\r\nOK\r\n`,
`${join(copyHome, "simlock.avd")}\r\nOK\r\n`,
);
const savingSteps = [
processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"),
Expand All @@ -313,14 +313,19 @@ describe("AndroidDriver image fit", () => {
);
const ready = await harness.driver.makeReady(await harness.driver.provision(harness.spec));
await harness.driver.saveSource({ ...ready, physical: false as const });
await harness.filesystem.writeFileAtomic(`${savedHome}/simlock.avd/marker.txt`, "saved");
await harness.filesystem.writeFileAtomic(
join(savedHome, "simlock.avd", "marker.txt"),
"saved",
);
const callsBefore = harness.runner.calls.length;

const copy = await harness.driver.provision(harness.spec, { source: "baseline_four" });

expect(copy.deviceId).toBe("simlock_five");
await expect(
harness.filesystem.readFile(`${avdDirectory}/simlock_five/simlock.avd/marker.txt`),
harness.filesystem.readFile(
join(avdDirectory, "simlock_five", "simlock.avd", "marker.txt"),
),
).resolves.toBe("saved");
expect(harness.runner.calls).toHaveLength(callsBefore + 1);

Expand All @@ -330,15 +335,17 @@ describe("AndroidDriver image fit", () => {
).rejects.toThrow(
"source baseline_four is stale: system image system-images;android-34;google_apis;arm64-v8a is not installed",
);
expect(await harness.filesystem.exists(`${avdDirectory}/simlock_six`)).toBe(false);
expect(await harness.filesystem.exists(join(avdDirectory, "simlock_six"))).toBe(false);
});

it("keeps a powered-off copy of the device as a ready baseline source with one boot snapshot, powering the emulator off first, or killing it when it will not leave in 60 s, or polling adb with no process handle", async () => {
const kept = async (harness: Awaited<ReturnType<typeof seedHarness>>) => {
expect(await harness.filesystem.exists(`${savedHome}/simlock.avd/config.ini`)).toBe(true);
await expect(harness.filesystem.readFile(`${savedHome}/simlock.ini`)).resolves.toContain(
`path=${savedHome}/simlock.avd`,
);
expect(
await harness.filesystem.exists(join(savedHome, "simlock.avd", "config.ini")),
).toBe(true);
await expect(
harness.filesystem.readFile(join(savedHome, "simlock.ini")),
).resolves.toContain(`path=${join(savedHome, "simlock.avd")}`);
expect(await harness.filesystem.exists(copyHome)).toBe(true);
const listed = await harness.driver.listSources();
expect(listed.find((source) => source.id === "baseline_four")).toMatchObject({
Expand Down Expand Up @@ -399,7 +406,7 @@ describe("AndroidDriver image fit", () => {
processResult(
binaries.adb,
["-s", serial, "emu", "avd", "path"],
`${copyHome}/simlock.avd\r\nOK\r\n`,
`${join(copyHome, "simlock.avd")}\r\nOK\r\n`,
),
processResult(binaries.adb, ["-s", serial, "emu", "kill"]),
...sourceBoot(),
Expand Down Expand Up @@ -489,7 +496,7 @@ describe("AndroidDriver image fit", () => {
});

describe("saveSource failures", () => {
const savedHome = `${sources}/baseline_four`;
const savedHome = join(sources, "baseline_four");
const serial = "emulator-5586";
const config =
"hw.device.name=pixel_5\nimage.sysdir.1=system-images/android-34/google_apis/arm64-v8a/\nhw.ramSize=2048\n";
Expand All @@ -498,7 +505,7 @@ describe("AndroidDriver image fit", () => {
["-version"],
"Android emulator version 36.1.9",
);
const probe = (path = `${copyHome}/simlock.avd`) =>
const probe = (path = join(copyHome, "simlock.avd")) =>
processResult(binaries.adb, ["-s", serial, "emu", "avd", "path"], `${path}\r\nOK\r\n`);
const poweredOff = [
version,
Expand All @@ -511,7 +518,7 @@ describe("AndroidDriver image fit", () => {
it("deletes the source's folder and row and throws when the ownership check fails, the copy fails or the boot snapshot is not saved, leaving the device as it was", async () => {
const cases = [
{
expectations: [version, probe(`${avdDirectory}/other.avd`)],
expectations: [version, probe(join(avdDirectory, "other.avd"))],
message: "emulator-5586 is not its emulator",
sent: false,
},
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/android/index-launch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import { join } from "node:path";
import { type Driver } from "../../core/index.js";
import { MemoryFilesystem } from "../../ports/index.js";
import { type AndroidDriverDiagnostic, type AndroidEmulatorLaunchOptions } from "./index.js";
Expand Down Expand Up @@ -271,7 +272,7 @@ describe("AndroidDriver", () => {
});

it("rethrows a non-missing-file config.ini read error from the mark write instead of clobbering it as empty", async () => {
const configPath = `${avdDirectory}/simlock_one/simlock.avd/config.ini`;
const configPath = join(avdDirectory, "simlock_one", "simlock.avd", "config.ini");
const permissionError = Object.assign(new Error("EACCES: permission denied"), {
code: "EACCES",
});
Expand Down
Loading
Loading