mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 19:02:17 +03:00
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)
`main` has been red since b342c1a361 on the vitest and integration gates:
✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos
Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).
release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.
This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.
The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.
* chore(scripts): carry the rm-maxretries codemod onto main alongside its output
The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
121 lines
4.4 KiB
TypeScript
121 lines
4.4 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
import {
|
|
resolveWritableDataDir,
|
|
getDefaultDataDir,
|
|
resolveDataDir,
|
|
} from "../../src/lib/dataPaths.ts";
|
|
|
|
// Running as root bypasses POSIX permission bits, so a chmod-based "unwritable"
|
|
// directory would still be writable and the EACCES/EPERM branch never triggers.
|
|
const IS_ROOT = typeof process.getuid === "function" && process.getuid() === 0;
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
async function withTempEnv(
|
|
fn: (paths: { root: string; home: string }) => void | Promise<void>
|
|
) {
|
|
const originalEnv = { ...process.env };
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "omni-datadir-"));
|
|
const home = path.join(root, "home");
|
|
fs.mkdirSync(home, { recursive: true });
|
|
|
|
delete process.env.DATA_DIR;
|
|
delete process.env.XDG_CONFIG_HOME;
|
|
delete process.env.APPDATA;
|
|
process.env.HOME = home;
|
|
process.env.USERPROFILE = home;
|
|
|
|
try {
|
|
await fn({ root, home });
|
|
} finally {
|
|
for (const key of Object.keys(process.env)) {
|
|
if (!(key in originalEnv)) delete process.env[key];
|
|
}
|
|
for (const [key, value] of Object.entries(originalEnv)) {
|
|
process.env[key] = value;
|
|
}
|
|
// Restore perms before cleanup so rmSync can delete read-only parents.
|
|
try {
|
|
fs.chmodSync(root, 0o755);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
fs.rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
}
|
|
}
|
|
|
|
test("resolveWritableDataDir returns the configured DATA_DIR when it is writable", async () => {
|
|
await withTempEnv(({ root }) => {
|
|
const configured = path.join(root, "writable-data");
|
|
process.env.DATA_DIR = configured;
|
|
|
|
const resolved = resolveWritableDataDir();
|
|
assert.equal(resolved, path.resolve(configured));
|
|
// The probe creates the directory as a side effect.
|
|
assert.ok(fs.existsSync(configured));
|
|
});
|
|
});
|
|
|
|
test("resolveWritableDataDir falls back to the default dir when DATA_DIR is not writable (EACCES/EPERM)", { skip: IS_ROOT || IS_WINDOWS }, async () => {
|
|
await withTempEnv(({ root, home }) => {
|
|
// A read-only parent makes mkdir of the child fail with EACCES/EPERM.
|
|
const lockedParent = path.join(root, "locked");
|
|
fs.mkdirSync(lockedParent, { recursive: true });
|
|
fs.chmodSync(lockedParent, 0o555);
|
|
|
|
const configured = path.join(lockedParent, "data");
|
|
process.env.DATA_DIR = configured;
|
|
|
|
const resolved = resolveWritableDataDir();
|
|
const expectedFallback = getDefaultDataDir();
|
|
|
|
// It must NOT return the unwritable configured dir...
|
|
assert.notEqual(resolved, path.resolve(configured));
|
|
// ...and instead fall back to the default user dir (~/.omniroute under HOME).
|
|
assert.equal(resolved, expectedFallback);
|
|
assert.ok(resolved.startsWith(path.resolve(home)));
|
|
});
|
|
});
|
|
|
|
test("resolveWritableDataDir returns the default dir (no probe) when DATA_DIR is unset", async () => {
|
|
await withTempEnv(() => {
|
|
delete process.env.DATA_DIR;
|
|
// #10428: this asserts the SERVER path. Since the test-context guard now redirects a
|
|
// DATA_DIR-less test process to a temp dir (so a test can never open the operator's
|
|
// real DB), opt back in explicitly here — otherwise this test would be asserting the
|
|
// guard's behavior instead of the server's.
|
|
process.env.OMNIROUTE_ALLOW_DEFAULT_DATA_DIR = "1";
|
|
const resolved = resolveWritableDataDir();
|
|
assert.equal(resolved, getDefaultDataDir());
|
|
// Matches the pure resolver when no override is present.
|
|
assert.equal(resolved, resolveDataDir());
|
|
});
|
|
});
|
|
|
|
test("resolveWritableDataDir rethrows non-permission errors", { skip: IS_WINDOWS }, async () => {
|
|
await withTempEnv(({ root }) => {
|
|
// Point DATA_DIR at a path whose parent is a regular file → ENOTDIR, not EACCES.
|
|
const fileParent = path.join(root, "iam-a-file");
|
|
fs.writeFileSync(fileParent, "x");
|
|
|
|
const configured = path.join(fileParent, "data");
|
|
process.env.DATA_DIR = configured;
|
|
|
|
assert.throws(() => resolveWritableDataDir(), (err: NodeJS.ErrnoException) => {
|
|
return err.code !== "EACCES" && err.code !== "EPERM";
|
|
});
|
|
});
|
|
});
|
|
|
|
test("resolveWritableDataDir leaves the cloud sentinel untouched", async () => {
|
|
await withTempEnv(() => {
|
|
process.env.DATA_DIR = "/some/configured/path";
|
|
const resolved = resolveWritableDataDir({ isCloud: true });
|
|
assert.equal(resolved, "/tmp");
|
|
});
|
|
});
|