Skip to content
Merged
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
38 changes: 37 additions & 1 deletion src/ports/daemon-launcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { chmod, mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
Expand All @@ -9,6 +9,13 @@ import { FakeDaemonLauncher, NodeDaemonLauncher, startErrorFrom } from "./daemon
import { launcherFor } from "./service-launcher.js";
import { FakeServiceManager, type ServiceState } from "./service-manager.js";

// why: lets one test refuse a mkdir of one existing folder, as Windows does for a drive root; every
// other call goes to the real `mkdir`.
vi.mock("node:fs/promises", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs/promises")>();
return { ...actual, mkdir: vi.fn(actual.mkdir) };
});

describe("NodeDaemonLauncher", () => {
it("rejects asynchronous spawn failures", async () => {
const directory = await mkdtemp(join(tmpdir(), "simlock-launcher-"));
Expand Down Expand Up @@ -103,6 +110,35 @@ describe("NodeDaemonLauncher", () => {
}
});

it("starts the daemon when the folder holding SIMLOCK_HOME exists but refuses mkdir", async () => {
// why: #1663: on Windows `mkdir('D:\\')`, recursive or not, rejects EPERM though the root exists
// (Node 22.23.5, Windows 11). The temporary folder stands in for the drive root.
const driveRoot = await mkdtemp(join(tmpdir(), "simlock-launcher-"));
const home = join(driveRoot, "simlock");
const logPath = join(home, "daemon.log");
const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
vi.mocked(mkdir).mockImplementation(async (path, options) => {
if (path === driveRoot)
throw Object.assign(new Error(`EPERM: operation not permitted, mkdir '${driveRoot}'`), {
code: "EPERM",
});
return actual.mkdir(path, options);
});
try {
const launch = new NodeDaemonLauncher({
args: ["-e", "console.log('out')"],
command: process.execPath,
logPath,
simlockHome: home,
}).launch();
await expect(launch).resolves.toHaveProperty("exited");
await expect.poll(async () => (await readFile(logPath, "utf8")).trim()).toBe("out");
} finally {
vi.mocked(mkdir).mockImplementation(actual.mkdir);
await rm(driveRoot, { force: true, recursive: true });
}
});

it("combines stdout and stderr in an append-only log", async () => {
const directory = await mkdtemp(join(tmpdir(), "simlock-launcher-"));
const logPath = join(directory, "daemon.log");
Expand Down
11 changes: 10 additions & 1 deletion src/ports/daemon-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ export interface NodeDaemonLauncherOptions {
* import), their parents left to the default and an existing one left as it is. `chmod` because the umask can only take bits from `mkdir`'s mode.
*/
async function createDataDirectory(directory: string): Promise<void> {
await mkdir(dirname(directory), { recursive: true });
const parent = dirname(directory);
// why: Windows rejects even a recursive mkdir of a drive root (`D:\`) with EPERM though it
// exists, so a parent that is already a directory is left alone.
if (
!(await stat(parent).then(
(info) => info.isDirectory(),
() => false,
))
)
await mkdir(parent, { recursive: true });
try {
await mkdir(directory, { mode: 0o700 });
} catch (error: unknown) {
Expand Down
Loading