Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

ci: guard that files Go tests read outside server/ select backend-tests (MUL-9262) (#10599)

Jiayuan Zhangbf8862f03fcc
diff --git a/scripts/ci-scope.test.mjs b/scripts/ci-scope.test.mjs
index 5e76c38452..5df0b8600d 100644
--- a/scripts/ci-scope.test.mjs
+++ b/scripts/ci-scope.test.mjs
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
-import { mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
-import { join, matchesGlob } from "node:path";
+import { dirname, join, matchesGlob, posix, sep } from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import test from "node:test";
@@ -221,3 +221,192 @@ test("the CLI writes real Actions outputs and exits nonzero on a failed gate", (
assert.equal(gate.status, 1);
assert.match(gate.stderr, /backend-tests/);
});
+
+// A Go test that reads a repository file outside server/ only runs on a PR
+// touching that file if the backend filter names it; otherwise the PR skips
+// backend-tests and the broken test lands on main. The scan is heuristic: it
+// follows the shapes the suite uses (filepath.Join from "..", "../" literals,
+// package string constants, a repo-root constant joined with a path literal)
+// and keeps only paths that exist, which drops traversal fixtures like
+// "../../etc/passwd".
+function goTokens(source) {
+ const tokens = [];
+ for (const [text] of source.matchAll(/\/\/[^\n]*|\/\*[\s\S]*?\*\/|"(?:[^"\\\n]|\\.)*"|`[^`]*`|'(?:[^'\\\n]|\\.)*'|\w+|\S/g)) {
+ if (text[0] === '"') {
+ try { tokens.push({ value: JSON.parse(text) }); } catch { tokens.push({ value: text.slice(1, -1) }); }
+ } else if (text[0] === "`") {
+ tokens.push({ value: text.slice(1, -1) });
+ } else if (!/^(\/\/|\/\*|')/.test(text)) {
+ tokens.push(text);
+ }
+ }
+ return tokens;
+}
+
+const opening = new Set(["(", "[", "{"]);
+const closingBrackets = new Set([")", "]", "}"]);
+function closing(tokens, open) {
+ for (let depth = 0, i = open; i < tokens.length; i++) {
+ if (opening.has(tokens[i])) depth++;
+ else if (closingBrackets.has(tokens[i]) && --depth === 0) return i;
+ }
+ return tokens.length;
+}
+function splitTopLevel(tokens, separator) {
+ const parts = [[]];
+ let depth = 0;
+ for (const token of tokens) {
+ if (opening.has(token)) depth++;
+ if (closingBrackets.has(token)) depth--;
+ if (token === separator && depth === 0) parts.push([]);
+ else parts.at(-1).push(token);
+ }
+ return parts;
+}
+
+function goStringConstants(tokens, constants) {
+ for (let i = 0; i < tokens.length; i++) {
+ if (tokens[i] !== "const" && tokens[i] !== "var") continue;
+ const end = tokens[i + 1] === "(" ? closing(tokens, i + 1) : i + 4;
+ for (let j = i + 2; j < end; j++) {
+ const value = tokens[j + 1]?.value;
+ if (tokens[j] !== "=" || value === undefined || tokens[j + 2] === "+") continue;
+ constants.set(tokens[j - 1] === "string" ? tokens[j - 2] : tokens[j - 1], value);
+ }
+ }
+}
+
+const pathFunctions = new Set(["Join", "Clean", "FromSlash", "ToSlash"]);
+function pathCall(tokens, i) {
+ if ((tokens[i] !== "filepath" && tokens[i] !== "path") || tokens[i + 1] !== "."
+ || !pathFunctions.has(tokens[i + 2]) || tokens[i + 3] !== "(") return undefined;
+ const end = closing(tokens, i + 3);
+ return { name: tokens[i + 2], args: splitTopLevel(tokens.slice(i + 4, end), ","), end };
+}
+// The leading arguments that resolve statically; a Join stops being known at
+// its first dynamic argument, which still leaves the directory it reads under.
+function knownPrefix(args, constants) {
+ const known = [];
+ for (const arg of args) {
+ const value = evaluate(arg, constants);
+ if (value === undefined) break;
+ known.push(value);
+ }
+ return known;
+}
+function evaluate(tokens, constants) {
+ let result = "";
+ for (const part of splitTopLevel(tokens, "+")) {
+ const call = pathCall(part, 0);
+ let value = part.length === 1 ? part[0].value ?? constants.get(part[0]) : undefined;
+ if (call?.end === part.length - 1) {
+ const known = knownPrefix(call.args, constants);
+ if (known.length === call.args.length && known.length > 0) {
+ value = call.name === "Join" ? posix.join(...known) : posix.normalize(known[0]);
+ }
+ }
+ if (value === undefined) return undefined;
+ result += value;
+ }
+ return result;
+}
+
+function goTestRepoReads(root) {
+ const reads = new Map();
+ const goFiles = readdirSync(join(root, "server"), { recursive: true })
+ .map((file) => posix.join("server", file.split(sep).join("/")))
+ .filter((file) => file.endsWith(".go"));
+ for (const [dir, files] of Map.groupBy(goFiles, posix.dirname)) {
+ const sources = new Map(files.map((file) => [file, goTokens(readFileSync(join(root, file), "utf8"))]));
+ const constants = new Map();
+ for (const tokens of sources.values()) goStringConstants(tokens, constants);
+ for (const [file, tokens] of sources) {
+ if (!file.endsWith("_test.go")) continue;
+ const bases = new Set();
+ const resolve = (path, { directories = true } = {}) => {
+ if (path === ".." || path.startsWith("../") || path === "server" || path.startsWith("server/")) return;
+ const stat = statSync(join(root, path), { throwIfNoEntry: false });
+ if (!stat || (stat.isDirectory() && !directories)) return;
+ if (stat.isDirectory()) bases.add(path);
+ if (path === ".") return;
+ if (!reads.has(path)) reads.set(path, { directory: stat.isDirectory(), readers: new Set() });
+ reads.get(path).readers.add(file);
+ };
+ for (let i = 0; i < tokens.length; i++) {
+ const call = pathCall(tokens, i);
+ const relative = call?.name === "Join"
+ ? posix.join(...knownPrefix(call.args, constants), ".")
+ : tokens[i].value ?? constants.get(tokens[i]);
+ if (relative === ".." || relative?.startsWith("../")) resolve(posix.join(dir, relative));
+ }
+ // readRepoFile(t, ".env.example") style: a helper joins the literal onto
+ // a repository directory resolved above.
+ for (const base of [...bases]) {
+ for (const { value } of tokens) {
+ if (/^(?!\.\.)[\w.@+-]+(\/[\w.@+-]+)*$/.test(value ?? "")) resolve(posix.join(base, value), { directories: false });
+ }
+ }
+ }
+ }
+ return reads;
+}
+
+function uncoveredReads(root, reads, patterns) {
+ const covered = (file) => patterns.some((pattern) => matchesGlob(file, pattern));
+ return [...reads].filter(([path, { directory }]) => !(directory
+ ? readdirSync(join(root, path), { recursive: true }).some((file) => covered(posix.join(path, file.split(sep).join("/"))))
+ : covered(path)));
+}
+
+function missingFilterMessage(missing) {
+ return "Go tests read these files outside server/, but a PR changing them skips backend-tests.\n"
+ + 'Add each path to the "backend" group in .github/ci-paths.json:\n'
+ + missing.map(([path, { directory, readers }]) =>
+ ` "${directory ? `${path}/**` : path}" (read by ${[...readers].sort().join(", ")})`).join("\n");
+}
+
+test("every repository file a Go test reads outside server/ selects backend-tests", () => {
+ const root = fileURLToPath(new URL("..", import.meta.url));
+ const reads = goTestRepoReads(root);
+ assert.ok(reads.size > 0, "The scan found no reads outside server/; it no longer recognizes how Go tests load repository files.");
+ const missing = uncoveredReads(root, reads, filters.backend);
+ assert.deepEqual(missing.map(([path]) => path), [], missingFilterMessage(missing));
+});
+
+test("the Go test scan follows relative paths, constants and repo-root helpers", (t) => {
+ const root = mkdtempSync(join(tmpdir(), "ci-scope-go-"));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ const files = {
+ "server/pkg/a/config.go": 'package a\n\nconst ConfigPath = ".multica/environments.toml"\n',
+ "server/pkg/a/a_test.go": [
+ "package a",
+ 'const repoRoot = "../../.."',
+ "func readRepoFile(name string) { os.ReadFile(filepath.Join(repoRoot, name)) }",
+ "func TestA(t *testing.T) {",
+ '\treadRepoFile(".env.example")',
+ '\tos.ReadFile(filepath.Join("..", "..", "..", filepath.FromSlash(ConfigPath)))',
+ '\tos.ReadFile("../../../scripts/check.sh")',
+ '\tos.ReadDir(filepath.Join("..", "..", "..", "examples", "plugins", entry.Name()))',
+ '\tos.ReadFile("../../migrations/1.up.sql")',
+ '\tstore.Get("../../../etc/passwd")',
+ '\t// os.ReadFile("../../../docs/commented.md")',
+ "}",
+ ].join("\n"),
+ ".multica/environments.toml": "",
+ ".env.example": "",
+ "scripts/check.sh": "",
+ "examples/plugins/demo/multica.plugin.json": "",
+ "server/migrations/1.up.sql": "",
+ "docs/commented.md": "",
+ };
+ for (const [file, body] of Object.entries(files)) {
+ mkdirSync(dirname(join(root, file)), { recursive: true });
+ writeFileSync(join(root, file), body);
+ }
+ const reads = goTestRepoReads(root);
+ assert.deepEqual([...reads.keys()].sort(),
+ [".env.example", ".multica/environments.toml", "examples/plugins", "scripts/check.sh"]);
+ const missing = uncoveredReads(root, reads, [".env.example", "examples/plugins/**", "scripts/*.sh"]);
+ assert.equal(missingFilterMessage(missing).split("\n").at(-1),
+ ' ".multica/environments.toml" (read by server/pkg/a/a_test.go)');
+});