mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.
One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.
Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.
158 lines
4.6 KiB
TypeScript
158 lines
4.6 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { spawnSync } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
const BIN = path.join(ROOT, "bin", "omniroute.mjs");
|
|
const DATA_DIR_MODULE = path.join(ROOT, "bin", "cli", "data-dir.mjs");
|
|
|
|
async function withEnv<T>(
|
|
updates: Record<string, string | undefined>,
|
|
fn: () => Promise<T>
|
|
): Promise<T> {
|
|
const original = new Map<string, string | undefined>();
|
|
for (const key of Object.keys(updates)) {
|
|
original.set(key, process.env[key]);
|
|
if (updates[key] === undefined) {
|
|
delete process.env[key];
|
|
} else {
|
|
process.env[key] = updates[key];
|
|
}
|
|
}
|
|
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
for (const [key, value] of original) {
|
|
if (value === undefined) {
|
|
delete process.env[key];
|
|
} else {
|
|
process.env[key] = value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function readCliEnvShow(env: NodeJS.ProcessEnv, cwd: string): Record<string, string> {
|
|
const result = spawnSync("node", [BIN, "env", "show", "--json"], {
|
|
cwd,
|
|
env,
|
|
encoding: "utf-8",
|
|
timeout: 60_000,
|
|
});
|
|
|
|
assert.equal(
|
|
result.status,
|
|
0,
|
|
`omniroute env show failed\nstdout=${result.stdout}\nstderr=${result.stderr}`
|
|
);
|
|
|
|
const stdout = result.stdout ?? "";
|
|
const jsonStart = stdout.indexOf("{");
|
|
assert.ok(jsonStart >= 0, `expected JSON in stdout: ${stdout}`);
|
|
const parsed = JSON.parse(stdout.slice(jsonStart)) as { current: Record<string, string> };
|
|
return parsed.current;
|
|
}
|
|
|
|
test("CLI data-dir resolver preserves an existing legacy ~/.omniroute before XDG/app data", async () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cli-data-dir-"));
|
|
const home = path.join(tmp, "home");
|
|
const legacyDir = path.join(home, ".omniroute");
|
|
const appData = path.join(tmp, "appdata");
|
|
const xdgConfigHome = path.join(tmp, "xdg");
|
|
|
|
try {
|
|
fs.mkdirSync(legacyDir, { recursive: true });
|
|
|
|
await withEnv(
|
|
{
|
|
DATA_DIR: undefined,
|
|
HOME: home,
|
|
USERPROFILE: home,
|
|
APPDATA: appData,
|
|
XDG_CONFIG_HOME: xdgConfigHome,
|
|
},
|
|
async () => {
|
|
const { resolveDataDir: cliResolveDataDir } = await import(
|
|
`${pathToFileURL(DATA_DIR_MODULE).href}?t=${Date.now()}`
|
|
);
|
|
const { resolveDataDir: runtimeResolveDataDir } = await import(
|
|
`../../src/lib/dataPaths.ts?t=${Date.now()}`
|
|
);
|
|
|
|
assert.equal(cliResolveDataDir(), legacyDir);
|
|
assert.equal(cliResolveDataDir(), runtimeResolveDataDir());
|
|
}
|
|
);
|
|
} finally {
|
|
fs.rmSync(tmp, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
}
|
|
});
|
|
|
|
test("CLI startup loads later non-conflicting .env files without overriding earlier values", () => {
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cli-env-layers-"));
|
|
const home = path.join(tmp, "home");
|
|
const dataDir = path.join(tmp, "data");
|
|
const cwd = path.join(tmp, "cwd");
|
|
const appDataDir =
|
|
process.platform === "win32"
|
|
? path.join(tmp, "appdata", "omniroute")
|
|
: path.join(home, ".omniroute");
|
|
|
|
try {
|
|
fs.mkdirSync(dataDir, { recursive: true });
|
|
fs.mkdirSync(appDataDir, { recursive: true });
|
|
fs.mkdirSync(cwd, { recursive: true });
|
|
|
|
fs.writeFileSync(
|
|
path.join(dataDir, ".env"),
|
|
"OMNIROUTE_BASE_URL=https://data.example/v1\n",
|
|
"utf-8"
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(appDataDir, ".env"),
|
|
["OMNIROUTE_BASE_URL=https://appdata.example/v1", "OMNIROUTE_HTTP_TIMEOUT_MS=1234", ""].join(
|
|
"\n"
|
|
),
|
|
"utf-8"
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(cwd, ".env"),
|
|
["OMNIROUTE_BASE_URL=https://cwd.example/v1", "PORT=34567", ""].join("\n"),
|
|
"utf-8"
|
|
);
|
|
|
|
const cleanEnv = { ...process.env };
|
|
for (const key of [
|
|
"OMNIROUTE_BASE_URL",
|
|
"OMNIROUTE_HTTP_TIMEOUT_MS",
|
|
"PORT",
|
|
"STORAGE_ENCRYPTION_KEY",
|
|
]) {
|
|
delete cleanEnv[key];
|
|
}
|
|
|
|
const env = {
|
|
...cleanEnv,
|
|
DATA_DIR: dataDir,
|
|
HOME: home,
|
|
USERPROFILE: home,
|
|
APPDATA: path.join(tmp, "appdata"),
|
|
CI: "1",
|
|
OMNIROUTE_CLI_SKIP_REPO_ENV: "1",
|
|
OMNIROUTE_NO_UPDATE_NOTIFIER: "1",
|
|
};
|
|
|
|
const current = readCliEnvShow(env, cwd);
|
|
assert.equal(current.OMNIROUTE_BASE_URL, "https://data.example/v1");
|
|
assert.equal(current.OMNIROUTE_HTTP_TIMEOUT_MS, "1234");
|
|
assert.equal(current.PORT, "34567");
|
|
} finally {
|
|
fs.rmSync(tmp, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
}
|
|
});
|