diff --git a/packages/dms/e2e/mongo-server.test.mjs b/packages/dms/e2e/mongo-server.test.mjs index 86cbf9d1f..d310d4d0a 100644 --- a/packages/dms/e2e/mongo-server.test.mjs +++ b/packages/dms/e2e/mongo-server.test.mjs @@ -12,7 +12,7 @@ import test from "node:test"; import { fileURLToPath } from "node:url"; import { MongoClient } from "mongodb"; import { pidIn } from "./helpers/pids.mjs"; -import { isAlive } from "./identity.mjs"; +import { isRunning } from "./identity.mjs"; import { mongodBinary, startMongod } from "./mongo.mjs"; import { stopRecorded } from "./processes.mjs"; @@ -107,7 +107,7 @@ await test("a mongod that cannot join the replica set is stopped", async (t) => await assert.rejects(startMongod(paths, [PACKAGE_ROOT]), { message: "This node was not started with replication enabled.", }); - assert.equal(isAlive(pidIn(spawned)), false); + assert.equal(isRunning(pidIn(spawned)), false); }); await test("a mongod restarted on its port and data folder serves a change stream again", async (t) => { diff --git a/packages/dms/e2e/run-state.mjs b/packages/dms/e2e/run-state.mjs index a0f0db22f..63a7090d2 100644 --- a/packages/dms/e2e/run-state.mjs +++ b/packages/dms/e2e/run-state.mjs @@ -8,7 +8,7 @@ import { rmSync, writeFileSync, } from "node:fs"; -import { tmpdir } from "node:os"; +import { platform, tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { describeFailure } from "./failure.mjs"; import { assertSameNamespaces } from "./identity.mjs"; @@ -26,6 +26,9 @@ import { } from "./json.mjs"; const RUN_FILE = "run.json"; +const REPLACE_RETRY_MS = 2_000; +const REPLACE_POLL_MS = 5; +const REPLACE_PAUSE = new Int32Array(new SharedArrayBuffer(4)); const ID_BYTES = 3; const TIMESTAMP_LENGTH = 15; @@ -275,7 +278,42 @@ export function saveRun(state) { writeFileSync(`${file}.next`, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600, }); - renameSync(`${file}.next`, file); + replaceFile(`${file}.next`, file); +} + +/** + * Renames `from` over `to`. Windows refuses with EPERM to replace a file + * another process holds open, as a service loading the run does for a + * moment, so there the rename is tried again until that read is done. + * + * @param {string} from The new file + * @param {string} to The file it replaces + * @throws When the rename fails for another reason, or Windows still refuses it after {@link REPLACE_RETRY_MS} + */ +function replaceFile(from, to) { + const deadline = Date.now() + REPLACE_RETRY_MS; + for (;;) { + try { + renameSync(from, to); + return; + } catch (error) { + if (!isHeldOpen(error) || Date.now() >= deadline) throw error; + Atomics.wait(REPLACE_PAUSE, 0, 0, REPLACE_POLL_MS); + } + } +} + +/** + * @param {unknown} error + * @returns {boolean} + */ +function isHeldOpen(error) { + return ( + platform() === "win32" && + error instanceof Error && + "code" in error && + error.code === "EPERM" + ); } /** diff --git a/packages/dms/e2e/services.test.mjs b/packages/dms/e2e/services.test.mjs index e008766ad..909fa0ac0 100644 --- a/packages/dms/e2e/services.test.mjs +++ b/packages/dms/e2e/services.test.mjs @@ -6,6 +6,7 @@ import { join } from "node:path"; import test from "node:test"; import { pathToFileURL } from "node:url"; import { newRun } from "./helpers/fake-run.mjs"; +import { hasProc } from "./identity.mjs"; import { listenersOf, restartablePort } from "./processes.mjs"; import { readRun, saveRun } from "./run-state.mjs"; @@ -44,7 +45,8 @@ await ensureMail(loadRun(${JSON.stringify(state.dir)}));`, const mail = readRun(state.dir).processes.find( (entry) => entry.name === "mail", ); - assert.deepEqual(listenersOf(state.smtpPort), [mail?.pid]); + assert.ok(mail, "the run records no mail sink"); + if (hasProc()) assert.deepEqual(listenersOf(state.smtpPort), [mail.pid]); }); await test("the service entry refuses a name it does not serve and names those it does (catches an entry that exits 0 on a typo, leaving the stack to wait on a port nothing listens on)", () => { diff --git a/packages/dms/e2e/suite.test.mjs b/packages/dms/e2e/suite.test.mjs index cf053afdf..482700035 100644 --- a/packages/dms/e2e/suite.test.mjs +++ b/packages/dms/e2e/suite.test.mjs @@ -27,7 +27,9 @@ const CONFIG = `export default ${JSON.stringify({ * @returns {string} The checkout root */ function twoSuiteCheckout(t) { - const root = realpathSync(mkdtempSync(join(tmpdir(), "dms-e2e-suite-"))); + const root = realpathSync.native( + mkdtempSync(join(tmpdir(), "dms-e2e-suite-")), + ); const cwd = process.cwd(); const recorded = process.env.DMS_E2E_SUITE; const prebuilt = process.env.DMS_E2E_PREBUILT; diff --git a/packages/dms/src/test/unit/builder/declaration-refresh.test.ts b/packages/dms/src/test/unit/builder/declaration-refresh.test.ts index 22705e886..dcd96483c 100644 --- a/packages/dms/src/test/unit/builder/declaration-refresh.test.ts +++ b/packages/dms/src/test/unit/builder/declaration-refresh.test.ts @@ -84,9 +84,9 @@ describe("[unit] a declaration file edited between two builder operations", () = !result.ok && result.error.code === "typecheck_failed" ? result.error.diagnostics : []; - expect(diagnostics.map((diagnostic) => diagnostic.file)).to.deep.equal([ - path.join(app.root, "src", "labels.ts"), - ]); + expect( + diagnostics.map((diagnostic) => path.normalize(diagnostic.file)), + ).to.deep.equal([path.join(app.root, "src", "labels.ts")]); }); /** diff --git a/packages/dms/src/test/unit/builder/environment-refresh.test.ts b/packages/dms/src/test/unit/builder/environment-refresh.test.ts index 526691410..e8d9214a0 100644 --- a/packages/dms/src/test/unit/builder/environment-refresh.test.ts +++ b/packages/dms/src/test/unit/builder/environment-refresh.test.ts @@ -96,7 +96,9 @@ async function addBanner() { function failedFiles(result: Awaited>): string[] { return !result.ok && result.error.code === "typecheck_failed" - ? result.error.diagnostics.map((diagnostic) => diagnostic.file) + ? result.error.diagnostics.map((diagnostic) => + path.normalize(diagnostic.file), + ) : []; } diff --git a/packages/dms/src/test/unit/builder/importer-typecheck.test.ts b/packages/dms/src/test/unit/builder/importer-typecheck.test.ts index 9067c1a7c..fb778d91d 100644 --- a/packages/dms/src/test/unit/builder/importer-typecheck.test.ts +++ b/packages/dms/src/test/unit/builder/importer-typecheck.test.ts @@ -59,7 +59,9 @@ function installTone(tone: string): void { function failedFiles(result: OpResult): string[] { return !result.ok && result.error.code === "typecheck_failed" - ? result.error.diagnostics.map((diagnostic) => diagnostic.file) + ? result.error.diagnostics.map((diagnostic) => + path.normalize(diagnostic.file), + ) : []; }