Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Report gates known-reds cannot read instead of mislabelling them (#5552)

Nick Gomeza839ae5d7714
diff --git a/scripts/check-known-reds.uncached.test.mjs b/scripts/check-known-reds.uncached.test.mjs
index 88b464f8e..e09c29ca9 100644
--- a/scripts/check-known-reds.uncached.test.mjs
+++ b/scripts/check-known-reds.uncached.test.mjs
@@ -330,6 +330,21 @@ describe('known-reds scanner: CI-keyed skips', () => {
"const TIMEOUT = process.env.CI ? 60_000 : 5_000;\ntest.skipIf(TIMEOUT > 10_000)('x', () => {});",
'test',
],
+ ["const e = process.env;\ntest.skipIf(e.CI)('x', () => {});", 'test'],
+ ["const { env: e } = process;\ntest.skipIf(e.CI)('x', () => {});", 'test'],
+ ["const { CI } = process.env;\ntest.skipIf(CI)('x', () => {});", 'test'],
+ ["const { GITHUB_ACTIONS: gha } = process.env;\ntest.skipIf(gha)('x', () => {});", 'test'],
+ ["const key = 'CI';\ntest.skipIf(process.env[key])('x', () => {});", 'test'],
+ ["const onCi = () => Boolean(process.env.CI);\ntest.skipIf(onCi())('x', () => {});", 'test'],
+ [
+ "function onCi() { return process.env.CI === 'true'; }\ntest.skipIf(onCi())('x', () => {});",
+ 'test',
+ ],
+ ["import ci from 'ci-info';\ntest.skipIf(ci.isCI)('x', () => {});", 'test'],
+ ["import * as ci from 'ci-info';\ntest.skipIf(ci.isCI)('x', () => {});", 'test'],
+ ["import { isCI } from 'ci-info';\ntest.skipIf(isCI)('x', () => {});", 'test'],
+ ["test.skipIf('CI' in process.env)('x', () => {});", 'test'],
+ ["const key = 'GITHUB_ACTIONS';\ntest.skipIf(key in process.env)('x', () => {});", 'test'],
])('finds the Vitest skip in %s', (source, scope) => {
const gates = scanSource('packages/server/src/a.test.ts', source).gates;
expect(gates.filter((gate) => gate.ci === 'skips-on-ci').map((gate) => gate.scope)).toEqual([
@@ -353,6 +368,22 @@ describe('known-reds scanner: CI-keyed skips', () => {
"test.skipIf(process.env.CIRCLE)('x', () => {});",
"test.skipIf({ CI: true }.CI_FLAG)('x', () => {});",
"test.skipIf(process.platform === 'win32')('x', () => {});",
+ "const e = process.env;\ntest.skipIf(e.OK_LIVE_API)('x', () => {});",
+ "const { HOME } = process.env;\ntest.skipIf(HOME)('x', () => {});",
+ "const key = 'OK_LIVE_API';\ntest.skipIf(process.env[key])('x', () => {});",
+ "const hasDocker = () => process.env.DOCKER_HOST !== undefined;\ntest.skipIf(hasDocker())('x', () => {});",
+ "import ci from 'ci-info';\ntest.skipIf(ci.isPR)('x', () => {});",
+ "import { isCI } from './ci';\ntest.skipIf(isCI)('x', () => {});",
+ "import { isCi } from './env';\ntest.skipIf(isCi())('x', () => {});",
+ "const { ...CI } = process.env;\ntest.skipIf(CI)('x', () => {});",
+ "const { CI } = config;\ntest.skipIf(CI)('x', () => {});",
+ "const { env: e } = config;\ntest.skipIf(e.CI)('x', () => {});",
+ "import ci from './ci';\ntest.skipIf(ci.isCI)('x', () => {});",
+ "import * as ci from './ci';\ntest.skipIf(ci.isCI)('x', () => {});",
+ "const onCi = (flag) => Boolean(process.env.CI);\ntest.skipIf(onCi())('x', () => {});",
+ "function onCi() { return Boolean(process.env.CI); console.log('after'); }\ntest.skipIf(onCi())('x', () => {});",
+ "test.skipIf('OK_LIVE_API' in process.env)('x', () => {});",
+ "test.skipIf('CI' in flags)('x', () => {});",
])('lists %s as an environment gate', (source) => {
const gates = scanSource('packages/server/src/a.test.ts', source).gates;
expect(gates.map((gate) => gate.ci)).toEqual([null]);
@@ -432,7 +463,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
],
[
"test.skipIf(hasDocker())('x', () => {});",
- [{ kind: 'runtime', name: 'hasDocker()', text: 'hasDocker()' }],
+ [{ kind: 'unknown', name: 'hasDocker()', text: 'hasDocker()' }],
],
[
"import * as os from 'node:os';\ntest.skipIf(os.type() === 'Windows_NT')('x', () => {});",
@@ -482,6 +513,92 @@ describe('known-reds feed: gate atoms and title chains', () => {
[{ kind: 'env', name: 'OK_FLAG', text: "env.OK_FLAG === '1'" }],
],
["test.skipIf(IS_CI)('x', () => {});", [{ kind: 'ci', name: 'IS_CI', text: 'IS_CI' }]],
+ [
+ "test.skipIf(process.geteuid?.() === 0)('x', () => {});",
+ [{ kind: 'uid', name: 'process.geteuid', text: 'process.geteuid?.() === 0' }],
+ ],
+ [
+ "import { isTerminalPlatform } from './p';\ntest.skipIf(isTerminalPlatform(process.platform))('x', () => {});",
+ [
+ {
+ kind: 'unknown',
+ name: 'isTerminalPlatform(process.platform)',
+ text: 'isTerminalPlatform(process.platform)',
+ },
+ { kind: 'platform', name: 'process.platform', text: 'process.platform' },
+ ],
+ ],
+ [
+ "const { CI } = process.env;\ntest.skipIf(CI)('x', () => {});",
+ [{ kind: 'ci', name: 'CI', text: 'CI' }],
+ ],
+ [
+ "const e = process.env;\ntest.skipIf(e.OK_LIVE_API)('x', () => {});",
+ [{ kind: 'env', name: 'OK_LIVE_API', text: 'e.OK_LIVE_API' }],
+ ],
+ [
+ "const { platform } = process;\ntest.skipIf(platform === 'win32')('x', () => {});",
+ [{ kind: 'platform', name: 'process.platform', text: "platform === 'win32'" }],
+ ],
+ [
+ "import ci from 'ci-info';\ntest.skipIf(ci.isCI)('x', () => {});",
+ [{ kind: 'ci', name: 'ci-info', text: 'ci.isCI' }],
+ ],
+ [
+ "const onCi = () => Boolean(process.env.CI);\ntest.skipIf(onCi())('x', () => {});",
+ [{ kind: 'ci', name: 'CI', text: 'process.env.CI' }],
+ ],
+ [
+ "import { isCi } from './env';\ntest.skipIf(isCi())('x', () => {});",
+ [{ kind: 'unknown', name: 'isCi()', text: 'isCi()' }],
+ ],
+ [
+ "const onCi = (name) => Boolean(process.env[name]);\ntest.skipIf(onCi('CI'))('x', () => {});",
+ [{ kind: 'unknown', name: "onCi('CI')", text: "onCi('CI')" }],
+ ],
+ [
+ "test.skipIf(!process.env.OK_PACKAGE_DIR?.trim())('x', () => {});",
+ [
+ {
+ kind: 'unknown',
+ name: 'process.env.OK_PACKAGE_DIR?.trim()',
+ text: 'process.env.OK_PACKAGE_DIR?.trim()',
+ },
+ { kind: 'env', name: 'OK_PACKAGE_DIR', text: 'process.env.OK_PACKAGE_DIR' },
+ ],
+ ],
+ [
+ "test.skipIf(process.getegid?.() === 0)('x', () => {});",
+ [{ kind: 'uid', name: 'process.getegid', text: 'process.getegid?.() === 0' }],
+ ],
+ [
+ "const key = 'OK_LIVE_API';\ntest.skipIf(process.env[key] === '1')('x', () => {});",
+ [{ kind: 'env', name: 'OK_LIVE_API', text: "process.env[key] === '1'" }],
+ ],
+ [
+ "import { isCI } from 'ci-info';\ntest.skipIf(isCI)('x', () => {});",
+ [{ kind: 'ci', name: 'ci-info', text: 'isCI' }],
+ ],
+ [
+ "const key = 'CI';\ntest.skipIf(key in process.env)('x', () => {});",
+ [{ kind: 'ci', name: 'CI', text: 'key in process.env' }],
+ ],
+ [
+ "const { ...rest } = process.env;\ntest.skipIf(rest)('x', () => {});",
+ [{ kind: 'unknown', name: 'rest', text: 'rest' }],
+ ],
+ [
+ "const { ...platform } = process;\ntest.skipIf(platform)('x', () => {});",
+ [{ kind: 'unknown', name: 'platform', text: 'platform' }],
+ ],
+ [
+ "const { env: { CI } } = process;\ntest.skipIf(CI)('x', () => {});",
+ [{ kind: 'unknown', name: 'CI', text: 'CI' }],
+ ],
+ [
+ "function onCi() { const v = process.env.CI; return Boolean(v); }\ntest.skipIf(onCi())('x', () => {});",
+ [{ kind: 'unknown', name: 'onCi()', text: 'onCi()' }],
+ ],
])('reads the atoms of %s', (source, atoms) => {
expect(atomsOf(source)).toEqual([atoms]);
});
@@ -510,7 +627,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
'});',
].join('\n');
expect(atomsOf(source)).toEqual([
- [{ kind: 'runtime', name: 'localProbe()', text: 'localProbe()' }],
+ [{ kind: 'unknown', name: 'localProbe()', text: 'localProbe()' }],
]);
});
@@ -552,7 +669,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
'});',
].join('\n');
expect(atomsOf(source)).toEqual([
- [{ kind: 'runtime', name: 'platform', text: "platform === 'win32'" }],
+ [{ kind: 'unknown', name: 'platform', text: "platform === 'win32'" }],
]);
});
@@ -615,7 +732,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
].join('\n');
const { gates, earlyReturns } = scanSource('packages/server/src/a.test.ts', source);
expect(gates.map(({ ci, atoms }) => ({ ci, atoms }))).toEqual([
- { ci: null, atoms: [{ kind: 'runtime', name: 'capability()', text: 'capability()' }] },
+ { ci: null, atoms: [{ kind: 'unknown', name: 'capability()', text: 'capability()' }] },
]);
expect(earlyReturns.map(({ line, condition }) => ({ line, condition }))).toEqual([
{ line: 8, condition: 'FLAG' },
@@ -635,7 +752,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
].join('\n');
const { gates } = scanSource('packages/server/src/a.test.ts', source);
expect(gates.map(({ ci, atoms }) => ({ ci, atoms }))).toEqual([
- { ci: null, atoms: [{ kind: 'runtime', name: 'FLAG', text: 'FLAG' }] },
+ { ci: null, atoms: [{ kind: 'unknown', name: 'FLAG', text: 'FLAG' }] },
]);
},
);
@@ -659,7 +776,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
const source = `const FLAG = process.env.CI;\n${body}`;
const { gates } = scanSource('packages/server/src/a.test.ts', source);
expect(gates.map(({ ci, atoms }) => ({ ci, atoms }))).toEqual([
- { ci: null, atoms: [{ kind: 'runtime', name: 'FLAG', text: 'FLAG' }] },
+ { ci: null, atoms: [{ kind: 'unknown', name: 'FLAG', text: 'FLAG' }] },
]);
});
@@ -694,7 +811,8 @@ describe('known-reds feed: gate atoms and title chains', () => {
].join('\n');
const { gates, earlyReturns } = scanSource('packages/server/src/a.test.ts', source);
expect(gates[0].ci).toBeNull();
- expect(gates[0].atoms).toEqual([{ kind: 'runtime', name: condition, text: condition }]);
+ expect(gates[0].atoms).toContainEqual({ kind: 'unknown', name: condition, text: condition });
+ expect(gates[0].atoms.every((atom) => atom.kind === 'unknown')).toBe(true);
expect(earlyReturns).toEqual([]);
});
@@ -714,7 +832,7 @@ describe('known-reds feed: gate atoms and title chains', () => {
].join('\n');
const { gates, earlyReturns } = scanSource('packages/server/src/a.test.ts', source);
expect(gates.map(({ ci, atoms }) => ({ ci, atoms }))).toEqual([
- { ci: null, atoms: [{ kind: 'runtime', name: 'FIRST', text: 'FIRST' }] },
+ { ci: null, atoms: [{ kind: 'unknown', name: 'FIRST', text: 'FIRST' }] },
{ ci: 'skips-on-ci', atoms: [{ kind: 'ci', name: 'CI', text: 'process.env.CI' }] },
]);
expect(earlyReturns.map(({ condition }) => condition)).toEqual(['A']);
@@ -784,6 +902,8 @@ describe('known-reds scanner: early returns and not-run tests', () => {
"import os from 'node:os';\ntest('x', () => { if (os.platform() !== 'darwin') return; expect(1).toBe(1); });",
"test('x', () => { if (process.env.OK_AUDIT === '1') { report(); return; } expect(1).toBe(1); });",
"describe('x', () => { const isWin = process.platform === 'win32'; test('y', () => { if (isWin) return; expect(1).toBe(1); }); });",
+ "const { OK_LIVE_API } = process.env;\ntest('x', () => { if (!OK_LIVE_API) return; expect(1).toBe(1); });",
+ "const { platform } = process;\ntest('x', () => { if (platform === 'win32') return; expect(1).toBe(1); });",
])('flags the environment early return in %s', (source) => {
expect(scanSource('packages/server/src/a.test.ts', source).earlyReturns).toHaveLength(1);
});
@@ -796,10 +916,24 @@ describe('known-reds scanner: early returns and not-run tests', () => {
"test('x', () => { if (process.platform === 'win32') return promise; expect(1).toBe(1); });",
"test('x', (ctx) => { if (process.platform === 'win32') { ctx.skip(); return; } expect(1).toBe(1); });",
"describe('x', () => { if (process.platform === 'win32') return; test('y', () => { expect(1).toBe(1); }); });",
+ "const { value } = config;\ntest('x', () => { if (!value) return; expect(1).toBe(1); });",
+ "const { platform } = opts;\ntest('x', () => { if (platform === 'win32') return; expect(1).toBe(1); });",
+ "const { argv } = process;\ntest('x', () => { if (!argv) return; expect(1).toBe(1); });",
])('leaves %s alone', (source) => {
expect(scanSource('packages/server/src/a.test.ts', source).earlyReturns).toEqual([]);
});
+ test.each([
+ ["function f() { return f(); }\ntest.skipIf(f())('x', () => {});", 'f()'],
+ ["const a = b;\nconst b = a;\ntest.skipIf(process.env[a])('x', () => {});", null],
+ ["const e = f;\nconst f = e;\ntest.skipIf(e.CI)('x', () => {});", null],
+ ])('terminates on the cyclic declarations in %s', (source, unknownName) => {
+ const [gate] = scanSource('packages/server/src/a.test.ts', source).gates;
+ expect(gate.ci).toBeNull();
+ if (unknownName !== null)
+ expect(gate.atoms).toContainEqual({ kind: 'unknown', name: unknownName, text: unknownName });
+ });
+
test('flags a Playwright early return too', () => {
const source = "test('x', async () => { if (process.platform === 'win32') return; });";
expect(scanSource('packages/app/tests/stress/a.e2e.ts', source).earlyReturns).toHaveLength(1);
@@ -956,6 +1090,60 @@ describe('known-reds validation', () => {
);
});
+ test('the listing counts gates it cannot read, and lists them with --all', () => {
+ const unread = { kind: 'unknown', name: 'isCi()', text: 'isCi()' };
+ const report = {
+ files: 1,
+ problems: [],
+ pins: [],
+ quarantines: [],
+ earlyReturns: [],
+ ciSkips: [],
+ notRun: [],
+ envGates: [
+ {
+ path: 'a.test.ts',
+ line: 3,
+ form: 'skipIf',
+ condition: 'isCi()',
+ ci: null,
+ atoms: [unread],
+ },
+ {
+ path: 'b.test.ts',
+ line: 5,
+ form: 'skipIf',
+ condition: "process.platform === 'win32'",
+ ci: null,
+ atoms: [{ kind: 'platform', name: 'process.platform', text: 'process.platform' }],
+ },
+ ],
+ };
+ report.ciSkips = [
+ {
+ path: 'c.test.ts',
+ line: 7,
+ form: 'skipIf',
+ scope: 'test',
+ condition: 'process.env.CI && isFoo()',
+ ci: 'skips-on-ci',
+ atoms: [
+ { kind: 'ci', name: 'CI', text: 'process.env.CI' },
+ { kind: 'unknown', name: 'isFoo()', text: 'isFoo()' },
+ ],
+ },
+ ];
+ const heading = 'Gates with a condition the scanner cannot read (2)';
+ const rows = [
+ ' c.test.ts:7 skipIf cannot read: isFoo()',
+ ' a.test.ts:3 skipIf cannot read: isCi()',
+ ];
+ const brief = render(report, [], [], TODAY, { all: false });
+ expect(brief).toContain(heading);
+ for (const row of rows) expect(brief).not.toContain(row);
+ expect(render(report, [], [], TODAY, { all: true })).toContain([heading, ...rows].join('\n'));
+ });
+
test('the date boundaries are inclusive', () => {
expect(messages(pinReport({ until: TODAY }))).toEqual([]);
expect(messages(pinReport({ until: '2026-12-30' }))).toEqual([]);
@@ -1135,7 +1323,7 @@ describe('known-reds through its real invocation', () => {
});
expect(run.status).toBe(1);
const feed = JSON.parse(run.stdout);
- expect(feed.schemaVersion).toBe(2);
+ expect(feed.schemaVersion).toBe(3);
expect(feed.files).toBe(4);
expect(feed.violations.map((violation) => violation.rule)).toEqual(
expect.arrayContaining(['pin-retries', 'ci-skip', 'early-return']),
diff --git a/scripts/known-reds.mjs b/scripts/known-reds.mjs
index 47976cedc..4bb530dcb 100644
--- a/scripts/known-reds.mjs
+++ b/scripts/known-reds.mjs
@@ -11,7 +11,7 @@ export const OK_ROOT = fileURLToPath(new URL('..', import.meta.url));
export const ALLOWLIST_PATH = fileURLToPath(
new URL('./known-reds-allowlist.json', import.meta.url),
);
-export const SCHEMA_VERSION = 2;
+export const SCHEMA_VERSION = 3;
export const KNOWN_BUG_TAG = 'known-bug';
export const QUARANTINE_TAG = 'quarantine';
export const OWNERS = Object.freeze(['get-main-green', 'known-reds']);
@@ -55,6 +55,8 @@ const CI_ENV_NAMES = new Set([
'RUNNER_OS',
]);
const CI_IDENTIFIERS = new Set(['CI', 'IS_CI', 'isCI', 'isCi', 'ON_CI', 'onCI', 'IN_CI', 'inCI']);
+const CI_INFO_MODULE = 'ci-info';
+const CI_INFO_FLAG = 'isCI';
const FIELDS = ['issue', 'owner', 'until'];
function scriptKind(path) {
@@ -248,10 +250,64 @@ function isExpressionIdentifier(node) {
return true;
}
-function isEnvObject(node) {
+function isProcessObject(node) {
const value = unwrap(node);
- if (ts.isIdentifier(value)) return value.text === 'env';
- return ts.isPropertyAccessExpression(value) && value.name.text === 'env';
+ if (ts.isIdentifier(value)) return value.text === 'process';
+ return ts.isPropertyAccessExpression(value) && value.name.text === 'process';
+}
+
+function isEnvObject(node, seen = new Set()) {
+ const value = unwrap(node);
+ if (!value) return false;
+ if (ts.isPropertyAccessExpression(value)) return value.name.text === 'env';
+ if (!ts.isIdentifier(value)) return false;
+ if (value.text === 'env') return true;
+ const binding = visibleBinding(value, value.text);
+ if (binding?.destructured)
+ return binding.destructured.key === 'env' && isProcessObject(binding.destructured.from);
+ const initializer = binding?.initializer;
+ if (!initializer || seen.has(initializer)) return false;
+ seen.add(initializer);
+ return isEnvObject(initializer, seen);
+}
+
+function constantString(node, seen = new Set()) {
+ const direct = stringValue(node);
+ if (direct !== null) return direct;
+ const value = unwrap(node);
+ if (!value || !ts.isIdentifier(value)) return null;
+ const initializer = visibleBinding(value, value.text)?.initializer;
+ if (!initializer || seen.has(initializer)) return null;
+ seen.add(initializer);
+ return constantString(initializer, seen);
+}
+
+function localHelperResult(call) {
+ if (!ts.isCallExpression(call) || call.arguments.length > 0) return null;
+ const callee = unwrap(call.expression);
+ if (!ts.isIdentifier(callee)) return null;
+ const binding = visibleBinding(callee, callee.text);
+ const initializer = binding?.initializer ? unwrap(binding.initializer) : null;
+ const fn = binding?.declaration ?? (isFunction(initializer) ? initializer : null);
+ if (!fn?.body || fn.parameters.length > 0) return null;
+ if (!ts.isBlock(fn.body)) return fn.body;
+ const [only] = fn.body.statements;
+ return fn.body.statements.length === 1 && ts.isReturnStatement(only)
+ ? (only.expression ?? null)
+ : null;
+}
+
+function isCiInfoNamespace(node) {
+ const value = unwrap(node);
+ if (!value || !ts.isIdentifier(value)) return false;
+ const imported = visibleBinding(value, value.text)?.imported;
+ return Boolean(imported?.namespace) && imported.module === CI_INFO_MODULE;
+}
+
+function isCiInfoFlag(imported) {
+ return (
+ imported?.module === CI_INFO_MODULE && !imported.namespace && imported.name === CI_INFO_FLAG
+ );
}
function ciAtom(node, aliases) {
@@ -259,18 +315,25 @@ function ciAtom(node, aliases) {
if (!isExpressionIdentifier(node)) return null;
return aliases(node);
}
- if (
- ts.isPropertyAccessExpression(node) &&
- CI_ENV_NAMES.has(node.name.text) &&
- isEnvObject(node.expression)
- ) {
- return 'positive';
+ if (ts.isPropertyAccessExpression(node)) {
+ if (CI_ENV_NAMES.has(node.name.text) && isEnvObject(node.expression)) return 'positive';
+ if (node.name.text === CI_INFO_FLAG && isCiInfoNamespace(node.expression)) return 'positive';
+ return null;
}
if (ts.isElementAccessExpression(node) && isEnvObject(node.expression)) {
- const key = stringValue(node.argumentExpression);
+ const key = constantString(node.argumentExpression);
return key !== null && CI_ENV_NAMES.has(key) ? 'positive' : null;
}
- return null;
+ if (
+ ts.isBinaryExpression(node) &&
+ node.operatorToken.kind === ts.SyntaxKind.InKeyword &&
+ isEnvObject(node.right)
+ ) {
+ const key = constantString(node.left);
+ return key !== null && CI_ENV_NAMES.has(key) ? 'positive' : null;
+ }
+ const result = ts.isCallExpression(node) ? localHelperResult(node) : null;
+ return result ? aliases(result) : null;
}
function containsCi(node, aliases) {
@@ -358,17 +421,14 @@ function ciValue(node, aliases, onCi) {
function ciAliases() {
const aliases = new Map();
const following = new Set();
- const resolve = (identifier) => {
- const binding = visibleBinding(identifier, identifier.text);
- if (!binding) return CI_IDENTIFIERS.has(identifier.text) ? 'positive' : null;
- const { initializer } = binding;
- if (!initializer || following.has(initializer)) return null;
- if (aliases.has(initializer)) return aliases.get(initializer);
- following.add(initializer);
+ const polarityOf = (expression) => {
+ if (following.has(expression)) return null;
+ if (aliases.has(expression)) return aliases.get(expression);
+ following.add(expression);
let polarity = null;
- if (containsCi(initializer, resolve)) {
- const onCi = ciValue(initializer, resolve, true);
- const offCi = ciValue(initializer, resolve, false);
+ if (containsCi(expression, resolve)) {
+ const onCi = ciValue(expression, resolve, true);
+ const offCi = ciValue(expression, resolve, false);
polarity =
onCi === true && offCi === false
? 'positive'
@@ -376,10 +436,21 @@ function ciAliases() {
? 'negative'
: 'unknown';
}
- following.delete(initializer);
- aliases.set(initializer, polarity);
+ following.delete(expression);
+ aliases.set(expression, polarity);
return polarity;
};
+ const resolve = (node) => {
+ if (!ts.isIdentifier(node)) return polarityOf(node);
+ const binding = visibleBinding(node, node.text);
+ if (!binding) return CI_IDENTIFIERS.has(node.text) ? 'positive' : null;
+ if (binding.destructured) {
+ const { key, from } = binding.destructured;
+ return CI_ENV_NAMES.has(key) && isEnvObject(from) ? 'positive' : null;
+ }
+ if (binding.imported) return isCiInfoFlag(binding.imported) ? 'positive' : null;
+ return binding.initializer ? polarityOf(binding.initializer) : null;
+ };
return resolve;
}
@@ -388,6 +459,8 @@ const PROCESS_ATOMS = new Map([
['arch', 'arch'],
['getuid', 'uid'],
['getgid', 'uid'],
+ ['geteuid', 'uid'],
+ ['getegid', 'uid'],
['versions', 'runtime'],
['release', 'runtime'],
]);
@@ -450,7 +523,12 @@ function environmentBindings(ciAliasMap) {
const environment = {
ciAliases: ciAliasMap,
aliases: (identifier) => {
- const initializer = visibleBinding(identifier, identifier.text)?.initializer;
+ const binding = visibleBinding(identifier, identifier.text);
+ if (binding?.destructured) {
+ const { key, from } = binding.destructured;
+ return isEnvObject(from) || (isProcessObject(from) && PROCESS_ENVIRONMENT.has(key));
+ }
+ const initializer = binding?.initializer;
if (!initializer || following.has(initializer)) return false;
if (aliases.has(initializer)) return aliases.get(initializer);
following.add(initializer);
@@ -517,7 +595,7 @@ function envKey(node) {
const value = unwrap(node);
if (ts.isPropertyAccessExpression(value) && isEnvObject(value.expression)) return value.name.text;
if (ts.isElementAccessExpression(value) && isEnvObject(value.expression))
- return stringValue(value.argumentExpression);
+ return constantString(value.argumentExpression);
return undefined;
}
@@ -571,18 +649,31 @@ function bindsName(name, target) {
function variableBinding(list, target) {
const declaration = list.declarations.find((entry) => bindsName(entry.name, target));
- return declaration
- ? {
- initializer: ts.isIdentifier(declaration.name) ? (declaration.initializer ?? null) : null,
- }
- : null;
+ if (!declaration) return null;
+ if (ts.isIdentifier(declaration.name)) return { initializer: declaration.initializer ?? null };
+ const element = ts.isObjectBindingPattern(declaration.name)
+ ? declaration.name.elements.find(
+ (entry) => ts.isIdentifier(entry.name) && entry.name.text === target,
+ )
+ : undefined;
+ const key =
+ element && !element.dotDotDotToken
+ ? propertyName({ name: element.propertyName ?? element.name })
+ : null;
+ return {
+ initializer: null,
+ destructured:
+ key !== null && declaration.initializer ? { from: declaration.initializer, key } : null,
+ };
}
function statementBinding(statement, target) {
if (ts.isVariableStatement(statement)) return variableBinding(statement.declarationList, target);
+ if (ts.isFunctionDeclaration(statement) && statement.name && bindsName(statement.name, target)) {
+ return { initializer: null, declaration: statement };
+ }
if (
- (ts.isFunctionDeclaration(statement) ||
- ts.isClassDeclaration(statement) ||
+ (ts.isClassDeclaration(statement) ||
ts.isEnumDeclaration(statement) ||
ts.isModuleDeclaration(statement) ||
(ts.isImportEqualsDeclaration(statement) && !statement.isTypeOnly)) &&
@@ -721,6 +812,21 @@ function conditionAtoms(condition, context) {
add('fs', first ? oneLine(first, sourceFile) : name, textNode);
return;
}
+ const result = localHelperResult(node);
+ if (result && !following.has(result)) {
+ following.add(result);
+ visit(result, null);
+ following.delete(result);
+ return;
+ }
+ }
+ if (
+ ts.isPropertyAccessExpression(node) &&
+ node.name.text === CI_INFO_FLAG &&
+ isCiInfoNamespace(node.expression)
+ ) {
+ add('ci', CI_INFO_MODULE, textNode);
+ return;
}
const { root, members } = memberPath(node);
if (root?.text === 'process' && PROCESS_ATOMS.has(members[0])) {
@@ -745,9 +851,20 @@ function conditionAtoms(condition, context) {
if (ts.isIdentifier(node)) {
const binding = visibleBinding(node, node.text);
if (binding?.imported) {
- add('import', binding.imported.module, textNode);
+ add(isCiInfoFlag(binding.imported) ? 'ci' : 'import', binding.imported.module, textNode);
return;
}
+ if (binding?.destructured) {
+ const { key, from } = binding.destructured;
+ if (isEnvObject(from)) {
+ envAtom(key, textNode);
+ return;
+ }
+ if (isProcessObject(from) && PROCESS_ATOMS.has(key)) {
+ add(PROCESS_ATOMS.get(key), `process.${key}`, textNode);
+ return;
+ }
+ }
const initializer = binding?.initializer ?? null;
if (initializer && !following.has(initializer)) {
const specifier = importSpecifier(initializer, sourceFile);
@@ -765,7 +882,24 @@ function conditionAtoms(condition, context) {
return;
}
}
- add(probesFilesystem(node) ? 'fs' : 'runtime', oneLine(node, sourceFile), textNode);
+ if (probesFilesystem(node)) {
+ add('fs', oneLine(node, sourceFile), textNode);
+ return;
+ }
+ add('unknown', oneLine(node, sourceFile), textNode);
+ if (ts.isCallExpression(node)) {
+ const callee = unwrap(node.expression);
+ if (ts.isPropertyAccessExpression(callee)) visit(callee.expression, null);
+ for (const argument of node.arguments) {
+ const value = unwrap(argument);
+ if (
+ !isFunction(value) &&
+ !ts.isArrayLiteralExpression(value) &&
+ !ts.isObjectLiteralExpression(value)
+ )
+ visit(argument, null);
+ }
+ }
};
const visit = (raw, textNode) => {
const node = unwrap(raw);
@@ -795,7 +929,7 @@ function conditionAtoms(condition, context) {
return;
}
if (operator === ts.SyntaxKind.InKeyword && isEnvObject(node.right)) {
- envAtom(stringValue(node.left), textNode ?? node);
+ envAtom(constantString(node.left), textNode ?? node);
return;
}
}
@@ -1601,6 +1735,18 @@ export function render(report, violations, upcoming, today, { all }) {
`${row.path}:${row.line} ${row.form} ${row.condition}${row.ci ? ' (runs only on CI)' : ''}`,
all,
);
+ section(
+ 'Gates with a condition the scanner cannot read',
+ [...report.ciSkips, ...report.envGates].filter((row) =>
+ row.atoms.some((atom) => atom.kind === 'unknown'),
+ ),
+ (row) =>
+ `${row.path}:${row.line} ${row.form} cannot read: ${row.atoms
+ .filter((atom) => atom.kind === 'unknown')
+ .map((atom) => atom.name)
+ .join(', ')}`,
+ all,
+ );
section(
`Expiring within ${EXPIRY_WARNING_DAYS} days`,
upcoming,
@@ -1614,7 +1760,7 @@ export function render(report, violations, upcoming, today, { all }) {
);
if (!all)
lines.push(
- 'Pass --all to list every not-run test and environment gate, or --json for the full feed.',
+ 'Pass --all to list every not-run test, environment gate and gate the scanner cannot read, or --json for the full feed.',
);
lines.push(`The convention: ${CONVENTION_DOC}.`);
return `${lines.join('\n')}\n`;
diff --git a/test-support/KNOWN-REDS.md b/test-support/KNOWN-REDS.md
index d99040c0c..2756effe4 100644
--- a/test-support/KNOWN-REDS.md
+++ b/test-support/KNOWN-REDS.md
@@ -77,6 +77,8 @@ A test in the uncached tier (`*.uncached.test.*`) may not skip at all. That tier
A skip keyed on CI (`process.env.CI`, `IS_CI` and the like) hides the test from the run that gates merges. It fails the guard unless `scripts/known-reds-allowlist.json` lists it with an owner, a date and a reason. Prefer to key the skip on the missing capability instead.
+The guard follows a condition through names declared in the same file: a renamed or destructured `process.env`, a destructured `CI`, a constant key, a helper that takes no arguments and whose body is a single return expression, and `isCI` from `ci-info`. Anything else, such as a helper imported from another file, a longer helper body, a rest element or a nested destructuring, is labelled `unknown` and listed under "Gates with a condition the scanner cannot read", where a reviewer has to check it.
+
## A test not written yet
Use `test.todo('title')`.
@@ -84,7 +86,7 @@ Use `test.todo('title')`.
## The listing
- `pnpm known-reds` prints the pins, the quarantines and the CI-keyed skips.
-- `--all` adds every test that does not run and every environment gate.
-- `--json` prints the full feed (`schemaVersion` 2). Beyond the listing, it carries:
- - `atoms` on each CI skip and environment gate: the facts its condition reads, following each name to the declaration visible where the gate sits. Each is `{ kind, name, text }`, and `kind` is `platform`, `arch`, `uid`, `env`, `ci`, `fs`, `import` or `runtime`. `runtime` covers what the scanner cannot resolve, such as a call to a helper, and also resolved reads of `process.versions` or `process.release`, which keep their full name.
+- `--all` adds every test that does not run, every environment gate, and each gate whose condition the scanner cannot read. Without it, those groups show only their counts.
+- `--json` prints the full feed (`schemaVersion` 3). Beyond the listing, it carries:
+ - `atoms` on each CI skip and environment gate: the facts its condition reads, following each name to the declaration visible where the gate sits. Each is `{ kind, name, text }`, and `kind` is `platform`, `arch`, `uid`, `env`, `ci`, `fs`, `import`, `runtime` or `unknown`. `runtime` is a resolved read of `process.versions` or `process.release`, which keeps its full name, or of an `os` function with no narrower kind. `unknown` is anything the scanner cannot resolve, such as a helper imported from another file; the receiver and arguments of an unresolved call are still read, so `isTerminalPlatform(process.platform)` yields `unknown` and `platform`, and `process.env.DIR?.trim()` yields `unknown` and `env`.
- `titles` on every pin and quarantine, and on each other entry scoped to a test or describe: the enclosing describe titles, ending with the entry's own declaration title, so a describe-scoped gate ends with its describe's title. A title that is not a string literal appears as `{ nonLiteral: <source> }`.