mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 08:31:44 +01:00
Changed-constant check stops flagging unrelated tests for one- and two-digit numbers (#4683)
Changing a small constant such as `MAX_ITEMS = 10` no longer flags every test that mentions its module and holds a `10` somewhere; only lines that use something taken from the module, or name the constant, count. Why: one-line raises of two-digit constants were blocked by dozens of false matches (#4312); on gstack itself, changing `RECEIPT_RUNS_KEPT` (30) flagged 20 test files and now flags 4. Nothing to do. Three-digit and longer values keep the old rule, and a test that imports the module and asserts the old value is still caught. Co-authored-by: Mahmoud ElNeMR <83190847+ElNeMeR70@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p1/red-team#2 reader=red-team
This commit is contained in:
4 files changed
+119
-25
No files matched your search
@@ -0,0 +1 @@
|
||||
- **The changed-constant check no longer floods on one- and two-digit numbers.** When a constant such as `MAX_ITEMS = 10` changes, a test that only mentions the constant's module counts as a consumer of the old `10` only when that line uses something the test imported from the module (or a variable it got from running it or from such a name), or names the constant. A comment that names the module beside an unrelated `10` no longer blocks the push; on gstack itself, changing `RECEIPT_RUNS_KEPT` (30) flagged 20 test files before and 4 now, and the most any two-digit constant flags fell from 29 files to 13 (#4312). Three-digit and longer values keep the old rule, and negative values such as `-10` follow the same rule as `10`.
|
||||
+12
-2
@@ -245,7 +245,8 @@ Two stages with separate clocks, both printed:
|
||||
literal (a named literal the branch only adds, such as `limit: 300` in a new
|
||||
object, is not a change), the whole test tree (nightly- and Heavy-only files included) is
|
||||
searched for the old value in files that name the constant or, when the old
|
||||
value is longer than one character, its module (the module's file name not
|
||||
value is longer than one character, its module (for a one- or two-digit
|
||||
integer, only on a line that uses a name taken from the module, below; the module's file name not
|
||||
followed by another extension, or a path ending
|
||||
in its stem followed by its own extension, for a JS/TS module also any JS/TS
|
||||
extension or `.d.ts`, a sentence-ending `.`, or anything but a word
|
||||
@@ -266,7 +267,16 @@ Two stages with separate clocks, both printed:
|
||||
line before ends with `(`, `[`, `{`, `,`, `.`, `=` or another operator, or the
|
||||
line starts with `.`, `)`, `]`, `}` or an operator), never on a comment line, plus the history rows
|
||||
above. A `$2` in a shell stub, a comment, or `export default 2` in a fixture
|
||||
elsewhere in the file is not a consumer. A call-site name such as `attempts`
|
||||
elsewhere in the file is not a consumer.
|
||||
A one- or two-digit integer (`12`, `-10`) found through the module counts only
|
||||
in a statement that names the constant or a binding the file takes from the
|
||||
module: a JS/TS import of it (type-only imports excluded) or a variable that a
|
||||
statement naming the module or another such binding declares or assigns, its
|
||||
continuation lines included (`const out = run('scripts/arms.ts')`,
|
||||
`const items = listItems()`). So `expect(shards()).toBe(12)` after importing
|
||||
`shards` still fails, while a comment naming the module beside an unrelated
|
||||
`10` does not (#4312). A local helper function that wraps an imported name is
|
||||
not followed, so its assertions need the constant's name to count. A call-site name such as `attempts`
|
||||
or `limit` is an ordinary word, so for it the file must also name the changed
|
||||
module, whatever the old value's length: a test that only uses the word is not
|
||||
a consumer, and a test that imports the module still is.
|
||||
|
||||
@@ -97,7 +97,13 @@ function unquote(v: string): string { return /^['"`]/.test(v) ? v.slice(1, -1) :
|
||||
* in a statement that names it: on the name's line, or on a line that
|
||||
* continues it (the line before ends open, `(` `,` `.` `=`, or the line
|
||||
* starts with `.` `)` `]`), comments excluded (#3492: `$2`, "from revision 2",
|
||||
* `export default 2`). A call-site name (`attempts`, `limit`) is an ordinary
|
||||
* `export default 2`). A one- or two-digit integer (`12`, `-10`) is nearly as
|
||||
* common, so through the module it counts only in a statement that names the
|
||||
* constant or a binding the file takes from the module: an import of it
|
||||
* (`import { shards } from '…/arms'`, type-only imports excluded) or a variable
|
||||
* a line naming it declares (`const out = run('…/arms.ts')`). A comment that
|
||||
* names the module beside an unrelated `10` is not a consumer (#4312), while
|
||||
* `expect(shards()).toBe(12)` still is (GST-820). A call-site name (`attempts`, `limit`) is an ordinary
|
||||
* word, so for it the file must also name the module whatever the value's
|
||||
* length (gstack-evals#97, #4478).
|
||||
*
|
||||
@@ -128,6 +134,7 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
const moduleRe = new RegExp(`(?:^|[/\\\\])${escape(stem)}(?=[^\\w.-]|$|\\.${dts}(?:${exts})(?![\\w.-])|\\.(?!\\w))|(?<![\\w-])${escape(base)}(?![\\w-]|\\.\\w)`, 'm');
|
||||
const nameRe = new RegExp(`\\b${escape(change.name)}\\b`);
|
||||
const nameRequired = value.length < 2;
|
||||
const shortNumber = !nameRequired && /^-?\d{1,2}$/.test(value);
|
||||
const callSite = !/^[A-Z]/.test(change.name);
|
||||
const comment = (l: string) => /^\s*(?:\/\/|#|\*|\/\*|<!--)/.test(l);
|
||||
const continues = (prev: string, cur: string) => /[([{,.=+\-*/&|?:\\]\s*$/.test(prev) || /^\s*[.)\]}?:&|+]/.test(cur);
|
||||
@@ -137,11 +144,14 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
try { text = fs.readFileSync(path.join(repoRoot, rel), 'utf8'); } catch { continue; }
|
||||
if (!((nameRequired ? nameRe.test(text) : nameRe.test(text) || moduleRe.test(text)) && (!callSite || moduleRe.test(text)))) continue;
|
||||
const lines = text.split('\n');
|
||||
const bindings = shortNumber ? moduleBindings(lines, moduleRe, comment, continues) : [];
|
||||
if (shortNumber && !bindings.length && !nameRe.test(text)) continue;
|
||||
const statementRe = bindings.length ? new RegExp(`(?<![\\w$])(?:${[change.name, ...bindings].map(escape).join('|')})(?![\\w$])`) : nameRe;
|
||||
const named = (i: number) => {
|
||||
if (comment(lines[i]!)) return false;
|
||||
for (let j = i; j >= 0 && i - j < 4; j--) {
|
||||
if (j < i && (comment(lines[j]!) || !continues(lines[j]!, lines[j + 1]!))) return false;
|
||||
if (nameRe.test(lines[j]!)) return true;
|
||||
if (statementRe.test(lines[j]!)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -168,11 +178,42 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
return true;
|
||||
};
|
||||
const carries = (i: number) => { const m = markerIn(lines[i]!); return valueRe.test(m ? lines[i]!.slice(0, lines[i]!.indexOf('pregate-literal:')) : lines[i]!); };
|
||||
for (let i = 0; i < lines.length; i++) if (carries(i) && (!nameRequired || oldRowRe.test(lines[i]!) || named(i)) && !skip(i) && !waiver(i)) { hits.push(`${rel}:${i + 1}`); break; }
|
||||
for (let i = 0; i < lines.length; i++) if (carries(i) && (!(nameRequired || shortNumber) || oldRowRe.test(lines[i]!) || named(i)) && !skip(i) && !waiver(i)) { hits.push(`${rel}:${i + 1}`); break; }
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
/** The names a file takes from the module: the bindings of a JS/TS import whose specifier is the module (type-only ones excluded), and the variables a non-comment statement (a line plus the lines that continue it) declares or assigns from the module or from such a name (`const items = listItems()`, `out = run('bin/x.sh')` in a hook). */
|
||||
function moduleBindings(lines: readonly string[], moduleRe: RegExp, comment: (l: string) => boolean, continues: (prev: string, cur: string) => boolean): string[] {
|
||||
const code = lines.filter(l => !comment(l)).join('\n');
|
||||
const out = new Set<string>();
|
||||
const names = (list: string) => list.split(',').map(s => s.trim()).filter(s => s && !/^type\s/.test(s)).map(s => s.split(/\s+as\s+|:/).pop()!.replace(/^\.\.\./, '').trim());
|
||||
for (const m of code.matchAll(/\bimport\s+(?!type\b)([^'"`;]*?)\s*\bfrom\s*['"]([^'"]+)['"]/g)) {
|
||||
if (!moduleRe.test(m[2]!)) continue;
|
||||
const braces = /\{([^}]*)\}/.exec(m[1]!);
|
||||
if (braces) for (const n of names(braces[1]!)) out.add(n);
|
||||
for (const n of m[1]!.replace(/\{[^}]*\}/, '').split(',').map(s => s.trim().replace(/^\*\s+as\s+/, ''))) out.add(n);
|
||||
}
|
||||
const codeLines = code.split('\n');
|
||||
const statements = codeLines.map((line, i) => {
|
||||
let s = line;
|
||||
for (let j = i + 1; j < codeLines.length && j - i < 4 && continues(codeLines[j - 1]!, codeLines[j]!); j++) s += '\n' + codeLines[j];
|
||||
return s;
|
||||
});
|
||||
const declRe = /(?:\b(?:const|let|var)\s+(\{[^}]*\}|[\w$]+)|(?<![\w$.])([\w$]+))\s*=(?![=>])/;
|
||||
for (let grew = true, round = 0; grew && round < 4; round++) {
|
||||
grew = false;
|
||||
const known = [...out].filter(n => /^[\w$]+$/.test(n));
|
||||
const knownRe = known.length ? new RegExp(`(?<![\\w$])(?:${known.map(escape).join('|')})(?![\\w$])`) : null;
|
||||
for (const s of statements) {
|
||||
const m = declRe.exec(s);
|
||||
if (!m || !(moduleRe.test(s) || knownRe?.test(s.slice(m.index + m[0].length)))) continue;
|
||||
for (const n of m[1]?.startsWith('{') ? names(m[1].slice(1, -1)) : [m[1] ?? m[2]!]) if (!out.has(n)) { out.add(n); grew = true; }
|
||||
}
|
||||
}
|
||||
return [...out].filter(n => /^[\w$]+$/.test(n));
|
||||
}
|
||||
|
||||
function run(ctx: PregateContext): CheckResult {
|
||||
const candidates = ctx.touched.filter(t => (t.status === 'M' || t.status === 'R') && !isTestFile(t.path) && /\.(?:[cm]?[jt]sx?|py|rb|sh|bash)$/.test(t.path));
|
||||
const changes: ChangedLiteral[] = [];
|
||||
|
||||
@@ -218,33 +218,75 @@ describe('literals (pure)', () => {
|
||||
// shell module is; fails_when=`/<stem>` again accepts any following `.<word>`, the file name accepts a further extension,
|
||||
// a non-JS module takes JS/TS extensions, or stops accepting a following space, bracket or sentence end
|
||||
// (GST-820: 17 false files in gbrain #8372 on ARM_SHARDS 12 -> 18); why_new=no test had a same-stem sibling; seam=none;
|
||||
// no_claim=name-gated hits (a file naming ARM_SHARDS still counts any 12)
|
||||
// no_claim=name-gated hits (a file naming ARM_SHARDS still counts any 12); the two-digit run pins which module matches still count (#4312)
|
||||
test('consumersOf: a sibling file sharing the module stem is not the module; real imports of it still are (GST-820)', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-literals-stem-'));
|
||||
const nl = String.fromCharCode(10);
|
||||
const files: Record<string, string> = {
|
||||
'test/a.test.ts': ['// listed in test/postgres-unit-arms.txt', 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/json.test.ts': ["const arms = read('fixtures/postgres-unit-arms.json');", 'expect(arms.length).toBe(12);', ''].join(nl),
|
||||
'test/longer.test.ts': ["run('scripts/postgres-unit-arms-v2.ts');", 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/import.test.ts': ["import { shards } from '../scripts/postgres-unit-arms';", 'expect(shards()).toBe(12);', ''].join(nl),
|
||||
'test/import-js.test.ts': ["import { shards } from '../scripts/postgres-unit-arms.js';", 'expect(shards()).toBe(12);', ''].join(nl),
|
||||
'test/own.test.ts': ["const out = run('scripts/postgres-unit-arms.ts');", 'expect(out).toBe(12);', ''].join(nl),
|
||||
'test/line-end.test.ts': ['// covers scripts/postgres-unit-arms', 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/prose.test.ts': ['// covers scripts/postgres-unit-arms and more', 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/paren.test.ts': ['// (scripts/postgres-unit-arms)', 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/sentence.test.ts': ['// see scripts/postgres-unit-arms.', 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/dts.test.ts': ["import type { Arms } from '../scripts/postgres-unit-arms.d.ts';", 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/sibling-test.test.ts': ["run('scripts/postgres-unit-arms.test.ts');", 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/snap.test.ts': ["read('scripts/__snapshots__/postgres-unit-arms.ts.snap');", 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/sh-twin.test.ts': ["import { arms } from '../lib/arms-tool.ts';", 'expect(n).toBe(12);', ''].join(nl),
|
||||
'test/sh-own.test.ts': ["run('scripts/arms-tool.sh');", 'expect(n).toBe(12);', ''].join(nl),
|
||||
};
|
||||
const armsFiles = (v: string): Record<string, string> => ({
|
||||
'test/a.test.ts': ['// listed in test/postgres-unit-arms.txt', `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/json.test.ts': ["const arms = read('fixtures/postgres-unit-arms.json');", `expect(arms.length).toBe(${v});`, ''].join(nl),
|
||||
'test/longer.test.ts': ["run('scripts/postgres-unit-arms-v2.ts');", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/import.test.ts': ["import { shards } from '../scripts/postgres-unit-arms';", `expect(shards()).toBe(${v});`, ''].join(nl),
|
||||
'test/import-js.test.ts': ["import { shards } from '../scripts/postgres-unit-arms.js';", `expect(shards()).toBe(${v});`, ''].join(nl),
|
||||
'test/own.test.ts': ["const out = run('scripts/postgres-unit-arms.ts');", `expect(out).toBe(${v});`, ''].join(nl),
|
||||
'test/line-end.test.ts': ['// covers scripts/postgres-unit-arms', `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/prose.test.ts': ['// covers scripts/postgres-unit-arms and more', `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/paren.test.ts': ['// (scripts/postgres-unit-arms)', `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/sentence.test.ts': ['// see scripts/postgres-unit-arms.', `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/dts.test.ts': ["import type { Arms } from '../scripts/postgres-unit-arms.d.ts';", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/sibling-test.test.ts': ["run('scripts/postgres-unit-arms.test.ts');", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/snap.test.ts': ["read('scripts/__snapshots__/postgres-unit-arms.ts.snap');", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/sh-twin.test.ts': ["import { arms } from '../lib/arms-tool.ts';", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
'test/sh-own.test.ts': ["run('scripts/arms-tool.sh');", `expect(n).toBe(${v});`, ''].join(nl),
|
||||
});
|
||||
const files = armsFiles('120');
|
||||
for (const [rel, text] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true }); fs.writeFileSync(path.join(root, rel), text); }
|
||||
expect(consumersOf(root, Object.keys(files), { name: 'ARM_SHARDS', old: '12', new: '18', file: 'scripts/postgres-unit-arms.ts' })).toEqual([
|
||||
expect(consumersOf(root, Object.keys(files), { name: 'ARM_SHARDS', old: '120', new: '180', file: 'scripts/postgres-unit-arms.ts' })).toEqual([
|
||||
'test/import.test.ts:2', 'test/import-js.test.ts:2', 'test/own.test.ts:2', 'test/line-end.test.ts:2',
|
||||
'test/prose.test.ts:2', 'test/paren.test.ts:2', 'test/sentence.test.ts:2', 'test/dts.test.ts:2',
|
||||
]);
|
||||
expect(consumersOf(root, ['test/sh-twin.test.ts', 'test/sh-own.test.ts'], { name: 'ARMS', old: '12', new: '18', file: 'scripts/arms-tool.sh' })).toEqual(['test/sh-own.test.ts:2']);
|
||||
expect(consumersOf(root, ['test/sh-twin.test.ts', 'test/sh-own.test.ts'], { name: 'ARMS', old: '120', new: '180', file: 'scripts/arms-tool.sh' })).toEqual(['test/sh-own.test.ts:2']);
|
||||
// The incident's own two-digit change: a test that imports the module, or keeps what running it returned, and asserts 12 is still
|
||||
// a consumer; a comment naming the module, a type-only import, or a script run with no binding beside an unrelated 12 is not (#4312).
|
||||
for (const [rel, text] of Object.entries(armsFiles('12'))) fs.writeFileSync(path.join(root, rel), text);
|
||||
expect(consumersOf(root, Object.keys(files), { name: 'ARM_SHARDS', old: '12', new: '18', file: 'scripts/postgres-unit-arms.ts' })).toEqual([
|
||||
'test/import.test.ts:2', 'test/import-js.test.ts:2', 'test/own.test.ts:2',
|
||||
]);
|
||||
expect(consumersOf(root, ['test/sh-twin.test.ts', 'test/sh-own.test.ts'], { name: 'ARMS', old: '12', new: '18', file: 'scripts/arms-tool.sh' })).toEqual([]);
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
// Value: protects=a one- or two-digit old value found only through the module counts in a statement that uses a binding the file
|
||||
// takes from it (an import, multi-line, namespace or require, a local declared from one, a hook assignment, a declaration whose
|
||||
// module path sits on a continuation line), never beside a comment naming the module or after a type-only import, and a negative
|
||||
// two-digit value gets the same rule; three digits keep the module rule; fails_when=short integers match any line in a file that
|
||||
// mentions the module again (#4312: 10 -> 11 flagged 194 lines in 11 files), or a derived local stops counting; why_new=only
|
||||
// one-character values were gated; seam=none; no_claim=Python or shell bindings, and a local helper function that wraps an
|
||||
// imported name (a short value there needs the constant's name or a binding on the asserting statement)
|
||||
test('consumersOf: a one- or two-digit value found through its module counts only beside a binding taken from it (#4312)', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-literals-short-'));
|
||||
const nl = String.fromCharCode(10);
|
||||
const files: Record<string, string> = {
|
||||
'test/unrelated.test.ts': ["import { other } from '../lib/retry';", 'expect(items.length).toBe(10);', 'expect(other()).toBe(11);', ''].join(nl),
|
||||
'test/prose.test.ts': ['// covers lib/retry', 'expect(rows).toHaveLength(10);', ''].join(nl),
|
||||
'test/type-only.test.ts': ["import type { Opts } from '../lib/retry';", 'expect(rows).toHaveLength(10);', ''].join(nl),
|
||||
'test/named.test.ts': ['expect(MAX_ITEMS).toBe(10);', ''].join(nl),
|
||||
'test/binding.test.ts': ['import {', ' maxItems as limit,', "} from '../lib/retry.js';", 'expect(n).toBe(10);', 'expect(', ' limit(),', ').toBe(10);', ''].join(nl),
|
||||
'test/namespace.test.ts': ["import * as retry from '../lib/retry';", 'expect(retry.MAX).toBe(10);', ''].join(nl),
|
||||
'test/require.test.ts': ["const { maxItems } = require('../lib/retry');", 'expect(maxItems()).toBe(10);', ''].join(nl),
|
||||
'test/derived.test.ts': ["import { listItems } from '../lib/retry';", 'const items = listItems();', 'expect(items).toHaveLength(10);', ''].join(nl),
|
||||
'test/hook.test.ts': ['let out;', "beforeAll(() => { out = run('lib/retry.ts'); });", 'expect(out.count).toBe(10);', ''].join(nl),
|
||||
'test/multiline.test.ts': ["const r = spawnSync('bun', [", " path.join(ROOT, 'lib/retry.ts'),", '], { timeout: 1000 });', 'expect(r.status).toBe(10);', ''].join(nl),
|
||||
};
|
||||
for (const [rel, text] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true }); fs.writeFileSync(path.join(root, rel), text); }
|
||||
expect(consumersOf(root, Object.keys(files), { name: 'MAX_ITEMS', old: '10', new: '11', file: 'lib/retry.ts' })).toEqual([
|
||||
'test/named.test.ts:1', 'test/binding.test.ts:7', 'test/namespace.test.ts:2', 'test/require.test.ts:2',
|
||||
'test/derived.test.ts:3', 'test/hook.test.ts:3', 'test/multiline.test.ts:4',
|
||||
]);
|
||||
fs.writeFileSync(path.join(root, 'test/negative.test.ts'), ['// covers lib/retry', 'expect(offset).toBe(-10);', ''].join(nl));
|
||||
fs.writeFileSync(path.join(root, 'test/negative-import.test.ts'), ["import { offset } from '../lib/retry';", 'expect(offset()).toBe(-10);', ''].join(nl));
|
||||
expect(consumersOf(root, ['test/negative.test.ts', 'test/negative-import.test.ts'], { name: 'OFFSET', old: '-10', new: '-11', file: 'lib/retry.ts' })).toEqual(['test/negative-import.test.ts:2']);
|
||||
fs.writeFileSync(path.join(root, 'test/wide.test.ts'), ['// covers lib/retry', 'expect(rows).toHaveLength(100);', ''].join(nl));
|
||||
expect(consumersOf(root, ['test/wide.test.ts'], { name: 'MAX_ITEMS', old: '100', new: '101', file: 'lib/retry.ts' })).toEqual(['test/wide.test.ts:2']);
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
test('consumersOf: a module stem is a path fragment, never a bare word, and a one-character old value needs the constant name (GSTA-114 flood: install.sh 2 -> 3 flagged 575 consumers)', () => {
|
||||
|
||||
Reference in new issue
Block a user