Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
Run tests without shell globbing, and strip trailing slashes in linear time
anirudhkumar-nanonets57d3915f3782
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 93ad3791..201fe07b 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -38,10 +38,4 @@ jobs:run: npm ci- name: Test- # `npm test` is `node --test test/*.test.ts` — that glob is expanded by the- # shell, and PowerShell (the Windows runner default) passes it through- # literally while Node 20's `--test` does not glob. Pinning bash, which the- # Windows runners ship via Git for Windows, makes both legs run the same- # file set instead of the Windows one silently testing nothing.- shell: bashrun: npm testdiff --git a/package.json b/package.jsonindex b645390f..bbf87aee 100644--- a/package.json+++ b/package.json@@ -46,7 +46,7 @@"build": "tsc -p tsconfig.json && tsc -p viewer/tsconfig.json && node scripts/build-viewer.mjs","dev": "tsc -p tsconfig.json --watch","cli": "tsx src/cli.ts",- "test": "node --import tsx --test test/*.test.ts",+ "test": "node scripts/run-tests.mjs","prepare": "npm run build","prepublishOnly": "npm run build","postinstall": "node scripts/postinstall.mjs"diff --git a/scripts/run-tests.mjs b/scripts/run-tests.mjsnew file mode 100644index 00000000..f8418123--- /dev/null+++ b/scripts/run-tests.mjs@@ -0,0 +1,43 @@+/**+ * Runs the test suite without depending on shell glob expansion.+ *+ * `node --test test/*.test.ts` needs the *shell* to expand that glob, and on+ * Windows nothing does: npm runs scripts through `cmd.exe` regardless of the+ * shell that invoked npm, and Node's `--test` only accepts glob patterns itself+ * from Node 21 (this package supports >=20). The literal pattern reaches Node as+ * a filename and it exits with `Could not find '…\test\*.test.ts'` — so the+ * Windows CI leg reported failure while actually running zero tests.+ *+ * Enumerating the files here instead means `npm test` behaves identically in+ * cmd.exe, PowerShell, bash and CI, on any supported Node.+ */+import { readdirSync } from "node:fs";+import { spawnSync } from "node:child_process";+import { dirname, join, resolve } from "node:path";+import { fileURLToPath } from "node:url";++const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");+const testDir = join(repoRoot, "test");++// Sorted so a failure is reported in the same order on every platform and run.+const files = readdirSync(testDir)+ .filter((f) => f.endsWith(".test.ts"))+ .sort()+ .map((f) => join(testDir, f));++if (files.length === 0) {+ console.error(`✗ no test files found in ${testDir}`);+ process.exit(1);+}++const result = spawnSync(process.execPath, ["--import", "tsx", "--test", ...files], {+ cwd: repoRoot,+ stdio: "inherit",+});++if (result.error) {+ console.error(`✗ could not start the test runner: ${result.error.message}`);+ process.exit(1);+}+// A signal-killed run reports null status; treat anything non-zero as failure.+process.exit(result.status ?? 1);diff --git a/src/context/node-file.ts b/src/context/node-file.tsindex bad2aee9..a4e9d49f 100644--- a/src/context/node-file.ts+++ b/src/context/node-file.ts@@ -25,7 +25,7 @@ import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync, rmSyncimport { join } from "node:path";import matter from "gray-matter";import { contentHash, normalizeName } from "../util/id.js";-import { relPosix } from "../util/paths.js";+import { relPosix, stripTrailingSlashes } from "../util/paths.js";// Value-only import of a constant; `write.ts` pulls in nothing from here, so no cycle.import { GRAPH_DIR } from "../graph/write.js";@@ -105,21 +105,6 @@ export function contextDirFor(root: string, override?: string): string {return join(root, "graft");}-/**- * Trailing `/` off a repo-relative dir path, in linear time.- *- * The obvious `replace(/\/+$/, "")` is a polynomial-ReDoS shape — `\/+$` can begin- * matching at any point inside a run of slashes, so a path ending in many slashes- * and then anything else costs O(n²). CodeQL flags it (`js/polynomial-redos`), and- * rightly: the input here is a `--dir` value, so it is not attacker-controlled, but- * the ambiguity buys nothing and a loop is both faster and plainer.- */-function stripTrailingSlashes(path: string): string {- let end = path.length;- while (end > 0 && path[end - 1] === "/") end--;- return path.slice(0, end);-}-/*** Make sure the repo's root `.gitignore` ignores the graft output dir. The* graph is a local, regenerable cache (like `node_modules`), not a committeddiff --git a/src/util/paths.ts b/src/util/paths.tsindex 61a0a177..f426821d 100644--- a/src/util/paths.ts+++ b/src/util/paths.ts@@ -41,6 +41,22 @@ export function relPosix(from: string, to: string): string {return toPosixPath(relative(from, to));}+/**+ * Trailing `/` off a repo-relative path, in linear time.+ *+ * The obvious `replace(/\/+$/, "")` is a polynomial-ReDoS shape — `\/+$` can begin+ * matching at any point inside a run of slashes, so a path ending in many slashes+ * and then anything else costs O(n²). CodeQL flags it (`js/polynomial-redos`), and+ * rightly: these inputs are a `--dir` or `--in` value rather than anything+ * attacker-controlled, but the ambiguity buys nothing and a loop is both faster+ * and plainer.+ */+export function stripTrailingSlashes(path: string): string {+ let end = path.length;+ while (end > 0 && path[end - 1] === "/") end--;+ return path.slice(0, end);+}+/*** Normalize a user-supplied path prefix (`--in`) so it can be compared against* a stored `node.path`: posix separators, no leading `./`, no trailing@@ -52,5 +68,5 @@ export function normalizePathPrefix(p: string): string {while (out.startsWith("./")) out = out.slice(2);// Trailing separators only; a bare "/" normalizes to "" (match everything),// which is exactly how `pathUnderPrefix` reads an empty prefix.- return out.replace(/\/+$/, "");+ return stripTrailingSlashes(out);}diff --git a/test/paths.test.ts b/test/paths.test.tsindex e52ae9ff..4f01d04b 100644--- a/test/paths.test.ts+++ b/test/paths.test.ts@@ -19,7 +19,7 @@import { test } from "node:test";import assert from "node:assert/strict";import { join, sep } from "node:path";-import { normalizePathPrefix, relPosix, toPosixPath } from "../src/util/paths.js";+import { normalizePathPrefix, relPosix, stripTrailingSlashes, toPosixPath } from "../src/util/paths.js";import { pathUnderPrefix } from "../src/graph/scopes.js";test("toPosixPath: identity on an already-posix path, and idempotent", () => {@@ -65,6 +65,17 @@ test("normalizePathPrefix: a native-separator prefix normalizes to posix", () =>assert.equal(normalizePathPrefix(join("server", "src", "gpu") + sep), "server/src/gpu");});+test("stripTrailingSlashes: linear-time, and leaves interior slashes alone", () => {+ assert.equal(stripTrailingSlashes("src/util"), "src/util");+ assert.equal(stripTrailingSlashes("src/util/"), "src/util");+ assert.equal(stripTrailingSlashes("src/util///"), "src/util");+ assert.equal(stripTrailingSlashes("/"), "");+ assert.equal(stripTrailingSlashes(""), "");+ // The regex this replaces was a polynomial-ReDoS shape (js/polynomial-redos):+ // a long run of trailing slashes must stay cheap, not quadratic.+ assert.equal(stripTrailingSlashes("a" + "/".repeat(50_000)), "a");+});+test("pathUnderPrefix: segment-aware, so a prefix is not a substring", () => {assert.ok(pathUnderPrefix("src/gate.ts", "src"));assert.ok(pathUnderPrefix("src/a/b/gate.ts", "src/a"));