codehere 2.1.0-rc.2 → 2.1.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/package.json +2 -2
- package/src/audit.js +64 -21
- package/src/brain.js +172 -47
- package/src/commands.js +24 -2
- package/src/gitpath.js +47 -0
- package/src/hooks.js +489 -45
- package/src/noskip.js +590 -0
- package/src/signals.js +651 -23
package/src/noskip.js
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
// Changed expected values, replaced tests and dropped test cases, read from a diff of test files.
|
|
2
|
+
// Ported from noskip 1.2.0, src/rules/tests.js and src/util.js (github.com/DariusHQ/noskip), with the
|
|
3
|
+
// findings reworded for Codehere and the file model built from a unified diff plus the files as they are now.
|
|
4
|
+
// Only the finish check runs this: it is the one caller that can read the files.
|
|
5
|
+
//
|
|
6
|
+
// MIT License
|
|
7
|
+
//
|
|
8
|
+
// Copyright (c) 2026 DariusHQ
|
|
9
|
+
//
|
|
10
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
// in the Software without restriction, including without limitation the rights
|
|
13
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
// furnished to do so, subject to the following conditions:
|
|
16
|
+
//
|
|
17
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
// copies or substantial portions of the Software.
|
|
19
|
+
//
|
|
20
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
// SOFTWARE.
|
|
27
|
+
|
|
28
|
+
import { unquotePath } from './gitpath.js';
|
|
29
|
+
|
|
30
|
+
// ---------- helpers (noskip src/util.js) ----------
|
|
31
|
+
|
|
32
|
+
const normalizeLine = (text) => text.trim().replace(/\s+/g, ' ');
|
|
33
|
+
const isCommentLine = (text) => /^\s*(?:\/\/|\/\*|\*(?:\s|\/|$)|<!--|#(?![[!]|pragma\b|if\b|include\b|define\b|expect\b|require\b))/.test(text);
|
|
34
|
+
const truncate = (text, max = 120) => {
|
|
35
|
+
const t = text.trim();
|
|
36
|
+
return t.length > max ? t.slice(0, max - 1) + '…' : t;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function splitArgs(text) {
|
|
40
|
+
const out = [];
|
|
41
|
+
let depth = 0;
|
|
42
|
+
let quote = null;
|
|
43
|
+
let cur = '';
|
|
44
|
+
for (let i = 0; i < text.length; i++) {
|
|
45
|
+
const ch = text[i];
|
|
46
|
+
if (quote) {
|
|
47
|
+
cur += ch;
|
|
48
|
+
if (ch === '\\') cur += text[++i] ?? '';
|
|
49
|
+
else if (ch === quote) quote = null;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (ch === '"' || ch === "'" || ch === '`') (quote = ch), (cur += ch);
|
|
53
|
+
else if (ch === '(' || ch === '[' || ch === '{') depth++, (cur += ch);
|
|
54
|
+
else if (ch === ')' || ch === ']' || ch === '}') depth--, (cur += ch);
|
|
55
|
+
else if (ch === ',' && depth === 0) out.push(cur.trim()), (cur = '');
|
|
56
|
+
else cur += ch;
|
|
57
|
+
}
|
|
58
|
+
if (cur.trim()) out.push(cur.trim());
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function matchParen(text, open) {
|
|
63
|
+
let depth = 0;
|
|
64
|
+
let quote = null;
|
|
65
|
+
for (let i = open; i < text.length; i++) {
|
|
66
|
+
const ch = text[i];
|
|
67
|
+
if (quote) {
|
|
68
|
+
if (ch === '\\') i++;
|
|
69
|
+
else if (ch === quote) quote = null;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ch === '"' || ch === "'" || ch === '`') quote = ch;
|
|
73
|
+
else if (ch === '(') depth++;
|
|
74
|
+
else if (ch === ')' && --depth === 0) return i;
|
|
75
|
+
}
|
|
76
|
+
return -1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ---------- test names (noskip src/rules/tests.js) ----------
|
|
80
|
+
|
|
81
|
+
const EXT_LANG = {
|
|
82
|
+
js: 'js', jsx: 'js', mjs: 'js', cjs: 'js', ts: 'js', tsx: 'js', mts: 'js', cts: 'js', vue: 'js', svelte: 'js', astro: 'js',
|
|
83
|
+
py: 'py', pyi: 'py', go: 'go', rs: 'rs', java: 'java', kt: 'java', kts: 'java', groovy: 'java', scala: 'java', cs: 'cs',
|
|
84
|
+
rb: 'rb', php: 'php', swift: 'swift', dart: 'dart', c: 'c', cc: 'c', cpp: 'c', cxx: 'c', h: 'c', hh: 'c', hpp: 'c', hxx: 'c', ex: 'ex', exs: 'ex',
|
|
85
|
+
};
|
|
86
|
+
export const languageOf = (p) => EXT_LANG[(/\.([^./]+)$/.exec(p) || [])[1]?.toLowerCase()] ?? null;
|
|
87
|
+
|
|
88
|
+
const STR = String.raw`(['"\x60])((?:\\.|(?!\1)[^\\])*)\1`;
|
|
89
|
+
const NAME_PATTERNS = {
|
|
90
|
+
js: [
|
|
91
|
+
{
|
|
92
|
+
re: new RegExp(
|
|
93
|
+
String.raw`\b(?:it|test|specify)(?:\s*\.\s*(?:only|skip|concurrent|sequential|failing|fails|todo|fixme|skipIf\([^)]{0,1000}\)|runIf\([^)]{0,1000}\)|each\s*(?:\([^)]{0,1000}\)|\x60[^\x60]*\x60)))*\s*\(\s*` + STR,
|
|
94
|
+
'g',
|
|
95
|
+
),
|
|
96
|
+
pick: (m) => m[2],
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
py: [{ re: /^[ \t]*(?:async[ \t]+)?def[ \t]+(test\w*)[ \t]*\(/gm }],
|
|
100
|
+
go: [{ re: /^func\s+(?:\([^)]{0,1000}\)\s*)?((?:Test|Benchmark|Fuzz|Example)\w*)\s*\(/gm }, { re: /\bt\.Run\(\s*"((?:\\.|[^"\\])*)"/g }, { re: /\bname:\s*"((?:\\.|[^"\\])*)"/g }],
|
|
101
|
+
rs: [{ re: /#\[(?:[\w:]+::)?test\b[^\]]{0,1000}\]\s*(?:#\[[^\]]{0,1000}\]\s*){0,20}(?:pub(?:\([^)\n]{0,200}\))?\s+)?(?:async\s+)?fn\s+(\w+)/g }],
|
|
102
|
+
java: [
|
|
103
|
+
{ re: /@(?:Test|ParameterizedTest|RepeatedTest|TestFactory|TestTemplate)\b[^\n]{0,1000}\n(?:[ \t]*(?:@[^\n]{0,1000})?\n){0,20}[ \t]*(?:(?:public|private|protected|internal|open|override|suspend|static|final)\s+)*(?:void\s+|fun\s+)?`?([^`(\s][^`(\n]{0,199}?)`?[ \t]{0,40}\(/g },
|
|
104
|
+
{ re: /\bvoid\s+(test\w+)\s*\(/g },
|
|
105
|
+
],
|
|
106
|
+
cs: [{ re: /\[\s*(?:Fact|Theory|Test|TestMethod|TestCase|TestCaseSource|SkippableFact)\b[^\]]{0,1000}\]\s*(?:\[[^\]]{0,1000}\]\s*){0,20}(?:(?:public|private|protected|internal|async|static|virtual|override)\s+)*[\w<>[\],.?]+\s+(\w+)\s*\(/g }],
|
|
107
|
+
rb: [{ re: new RegExp(String.raw`^[ \t]*(?:it|specify|scenario|example|test)[ \t]*(?:\(\s*)?` + STR, 'gm'), pick: (m) => m[2] }, { re: /^[ \t]*def\s+(test_\w+)/gm }],
|
|
108
|
+
php: [{ re: /function\s+(test\w+)\s*\(/g }, { re: /(?:#\[Test\]|@test\b)[^]{0,200}?function\s+(\w+)\s*\(/g }, { re: new RegExp(String.raw`\b(?:it|test)\s*\(\s*` + STR, 'g'), pick: (m) => m[2] }],
|
|
109
|
+
swift: [{ re: /func\s+(test\w+)\s*\(/g }, { re: /@Test\b(?:\([^)]{0,1000}\))?\s*(?:@\w+(?:\([^)]{0,1000}\))?\s*){0,20}func\s+(\w+)\s*\(/g }],
|
|
110
|
+
dart: [{ re: new RegExp(String.raw`\b(?:test|testWidgets)\s*\(\s*` + STR, 'g'), pick: (m) => m[2] }],
|
|
111
|
+
c: [{ re: /\b(?:TEST|TEST_F|TEST_P|TYPED_TEST)\s*\(\s*(\w+)\s*,\s*(\w+)\s*\)/g, pick: (m) => `${m[1]}.${m[2]}` }, { re: /\bTEST_CASE\s*\(\s*"((?:\\.|[^"\\])*)"/g }],
|
|
112
|
+
ex: [{ re: /\btest\s+"((?:\\.|[^"\\])*)"/g }],
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
function extractTestNames(text, lang) {
|
|
116
|
+
const names = new Map();
|
|
117
|
+
if (!text) return names;
|
|
118
|
+
for (const { re, pick } of NAME_PATTERNS[lang] ?? []) {
|
|
119
|
+
re.lastIndex = 0;
|
|
120
|
+
for (const m of text.matchAll(re)) {
|
|
121
|
+
const name = (pick ? pick(m) : m[m.length - 1] ?? m[1])?.trim();
|
|
122
|
+
if (!name) continue;
|
|
123
|
+
const entry = names.get(name);
|
|
124
|
+
if (entry) entry.count++;
|
|
125
|
+
else names.set(name, { count: 1, index: m.index });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return names;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const NAME_STOPWORDS = new Set(['a', 'an', 'the', 'is', 'are', 'it', 'to', 'of', 'on', 'in', 'and', 'or', 'when', 'if', 'with', 'for', 'should', 'be', 'not', 'still', 'that', 'does', 'do', 'test']);
|
|
132
|
+
const nameTokens = (name) => new Set(name.toLowerCase().split(/[^a-z0-9]+/).filter((t) => t.length > 1 && !NAME_STOPWORDS.has(t)));
|
|
133
|
+
|
|
134
|
+
// Two test names describe the same behaviour: a test split or moved under a new name, not one that went.
|
|
135
|
+
function similarNames(a, b) {
|
|
136
|
+
const A = nameTokens(a);
|
|
137
|
+
const B = nameTokens(b);
|
|
138
|
+
if (!A.size || !B.size) return false;
|
|
139
|
+
let shared = 0;
|
|
140
|
+
for (const t of A) if (B.has(t)) shared++;
|
|
141
|
+
return shared >= 2 && shared / Math.min(A.size, B.size) >= 0.5;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ---------- assertions ----------
|
|
145
|
+
|
|
146
|
+
const STRONG = new Set([
|
|
147
|
+
'toBe', 'toEqual', 'toStrictEqual', 'toMatchObject', 'toHaveBeenCalledWith', 'toHaveBeenNthCalledWith',
|
|
148
|
+
'toHaveBeenLastCalledWith', 'toHaveBeenCalledTimes', 'toHaveLength', 'toHaveProperty', 'toThrow', 'toThrowError',
|
|
149
|
+
'toMatch', 'toContainEqual', 'toBeCloseTo', 'equal', 'equals', 'eql', 'eq', 'isEqualTo', 'containsExactly',
|
|
150
|
+
'hasSize', 'isSameAs', 'assertEqual', 'assertEquals', 'assertSame', 'assertIs', 'assertDictEqual',
|
|
151
|
+
'assertListEqual', 'assertCountEqual', 'assertAlmostEqual', 'assertRaises', 'assertStrictEqual', 'assert_equal',
|
|
152
|
+
'assert_eq!', 'Equal', 'Equals', 'AreEqual', 'Same', 'Exactly', 'EqualValues', 'StrictEqual', 'deepEqual',
|
|
153
|
+
'deepStrictEqual', 'strictEqual', 'XCTAssertEqual', 'EXPECT_EQ', 'ASSERT_EQ', 'to eq', 'to eql', 'to equal',
|
|
154
|
+
'to match', 'to raise_error', '==',
|
|
155
|
+
]);
|
|
156
|
+
const WEAK = new Set([
|
|
157
|
+
'toBeDefined', 'toBeTruthy', 'toBeFalsy', 'toBeInstanceOf', 'toBeTypeOf', 'toHaveBeenCalled', 'not.toBeNull',
|
|
158
|
+
'not.toBeUndefined', 'ok', 'exist', 'exists', 'isNotNull', 'isNotEmpty', 'isInstanceOf', 'assertTrue', 'assertFalse',
|
|
159
|
+
'assertIsNotNone', 'assertIsInstance', 'assertIn', 'assertNotNull', 'assertNotNil', 'assert_not_nil', 'assert',
|
|
160
|
+
'assert!', 'NotNil', 'NotNull', 'NotEmpty', 'True', 'IsNotNull', 'IsTrue', 'XCTAssertNotNil', 'XCTAssertTrue',
|
|
161
|
+
'EXPECT_TRUE', 'ASSERT_TRUE', 'to be_truthy', 'to be_present', 'not_to be_nil', 'to_not be_nil', 'is not',
|
|
162
|
+
'truthy',
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
const norm = (s) => normalizeLine(String(s ?? '')).replace(/;$/, '').replace(/\s*,\s*$/, '');
|
|
166
|
+
|
|
167
|
+
function parseExpectStyle(t, opener) {
|
|
168
|
+
const m = opener.exec(t);
|
|
169
|
+
if (!m) return null;
|
|
170
|
+
const open = m.index + m[0].length - 1;
|
|
171
|
+
const close = matchParen(t, open);
|
|
172
|
+
if (close < 0) return null;
|
|
173
|
+
const subject = t.slice(open + 1, close);
|
|
174
|
+
const rest = t.slice(close + 1).replace(/->/g, '.');
|
|
175
|
+
let mm = /^\s*((?:\.\s*(?:not|resolves|rejects|to|be|been|is|deep|own|nested|and|has|have)\s*)*)\.\s*(\w+)\s*\(/.exec(rest);
|
|
176
|
+
if (mm) {
|
|
177
|
+
const argOpen = mm[0].length - 1;
|
|
178
|
+
const argClose = matchParen(rest, argOpen);
|
|
179
|
+
const chain = mm[1].replace(/\s+/g, '').replace(/^\./, '');
|
|
180
|
+
const matcher = (chain ? chain + '.' : '') + mm[2];
|
|
181
|
+
const args = argClose > 0 ? rest.slice(argOpen + 1, argClose) : rest.slice(argOpen + 1);
|
|
182
|
+
return { kind: 'expect', subject: norm(subject), matcher, args: norm(args) };
|
|
183
|
+
}
|
|
184
|
+
mm = /^\s*\.\s*(to|not_to|to_not)\s+(\w+)\s*\(?(.*?)\)?\s*$/.exec(rest);
|
|
185
|
+
if (mm) return { kind: 'expect', subject: norm(subject), matcher: `${mm[1]} ${mm[2]}`, args: norm(mm[3]) };
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const CALL_OPENER =
|
|
190
|
+
/(?:^|[^\w.$])((?:self\s*\.\s*|this\s*\.\s*|\$this\s*->\s*)?(?:assert[A-Z_]\w*|assert_\w+|assert_eq!|assert_ne!|assert!)|(?:assert|Assert|require)\s*\.\s*\w+|XCTAssert\w*|(?:EXPECT|ASSERT)_[A-Z_]+)\s*\(/;
|
|
191
|
+
|
|
192
|
+
function parseCallStyle(t) {
|
|
193
|
+
const m = CALL_OPENER.exec(t);
|
|
194
|
+
if (!m) return null;
|
|
195
|
+
const open = m.index + m[0].length - 1;
|
|
196
|
+
const close = matchParen(t, open);
|
|
197
|
+
if (close < 0) return null;
|
|
198
|
+
let fn = m[1].replace(/\s+/g, '').replace(/^(?:self\.|this\.|\$this->)/, '');
|
|
199
|
+
let args = splitArgs(t.slice(open + 1, close)).map(norm);
|
|
200
|
+
if (/^(?:assert|require)\./.test(fn) && /^[A-Z]/.test(fn.split('.')[1] ?? '') && args[0] === 't') args = args.slice(1); // Go testify
|
|
201
|
+
fn = fn.replace(/^(?:assert|Assert|require)\./, '');
|
|
202
|
+
return { kind: 'call', matcher: fn, args };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function parsePyAssert(t) {
|
|
206
|
+
const m = /^assert\s+(.+)$/.exec(t);
|
|
207
|
+
if (!m) return null;
|
|
208
|
+
const cond = splitArgs(m[1])[0] ?? '';
|
|
209
|
+
const op = /\s(==|!=|<=|>=|is not|is|not in|in)\s/.exec(cond);
|
|
210
|
+
if (!op) return { kind: 'py', subject: norm(cond), matcher: 'assert', args: '' };
|
|
211
|
+
const left = cond.slice(0, op.index);
|
|
212
|
+
const right = cond.slice(op.index + op[0].length);
|
|
213
|
+
if (op[1] === 'is not' && /^None$/.test(right.trim())) return { kind: 'py', subject: norm(left), matcher: 'is not', args: 'None' };
|
|
214
|
+
return { kind: 'py', subject: norm(left), matcher: op[1], args: norm(right) };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const KV = /^\s*(?:(?:const|let|var|val|final|auto|my)\s+)?["']?((?:want|wanted|expected|expect|exp|golden|answer)\w*)["']?\s*(?::=|=|:)\s*(.+?)\s*[,;]?\s*$/i;
|
|
218
|
+
|
|
219
|
+
function parseAssertion(text) {
|
|
220
|
+
const t = text.trim().replace(/;\s*$/, '');
|
|
221
|
+
if (!t || isCommentLine(t)) return null;
|
|
222
|
+
return (
|
|
223
|
+
parseExpectStyle(t, /\bexpect\s*\(/) ||
|
|
224
|
+
parseExpectStyle(t, /\bassertThat\s*\(/) ||
|
|
225
|
+
parseCallStyle(t) ||
|
|
226
|
+
parsePyAssert(t) ||
|
|
227
|
+
(() => {
|
|
228
|
+
const m = KV.exec(t);
|
|
229
|
+
return m ? { kind: 'kv', subject: m[1].toLowerCase(), matcher: 'kv', args: norm(m[2]) } : null;
|
|
230
|
+
})()
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const LITERALISH = /^(?:-?\d[\d_.]*n?|'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`[^`$]*`|true|false|null|undefined|None|True|False|nil|\/.+\/[a-z]*|\{[^()]*\}|\[[^()]*\]|[A-Z][A-Z0-9_]+)$/;
|
|
235
|
+
|
|
236
|
+
// The expression with its literals blanked out: `count + 3` and `count + 4` share a shape.
|
|
237
|
+
const shapeOf = (s) => s.replace(/'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`(?:\\.|[^`\\])*`/g, 'S').replace(/\b\d+(?:\.\d+)?n?\b/g, '#');
|
|
238
|
+
|
|
239
|
+
// The expectation moved (`46` to `47`, `n + 3` to `n + 4`), as opposed to the expression computing the actual value.
|
|
240
|
+
const goalpostMoved = (before, after) => LITERALISH.test(before) || LITERALISH.test(after) || shapeOf(before) === shapeOf(after);
|
|
241
|
+
|
|
242
|
+
const OPPOSITES = new Map(
|
|
243
|
+
[
|
|
244
|
+
['assertTrue', 'assertFalse'], ['assertEqual', 'assertNotEqual'], ['assertEquals', 'assertNotEquals'],
|
|
245
|
+
['assertIs', 'assertIsNot'], ['assertIn', 'assertNotIn'], ['assertIsNone', 'assertIsNotNone'],
|
|
246
|
+
['assertNull', 'assertNotNull'], ['assertSame', 'assertNotSame'], ['assertRegex', 'assertNotRegex'],
|
|
247
|
+
['assertIsInstance', 'assertNotIsInstance'], ['assert_equal', 'refute_equal'], ['assert_nil', 'refute_nil'],
|
|
248
|
+
['assert_eq!', 'assert_ne!'], ['equal', 'notEqual'], ['strictEqual', 'notStrictEqual'], ['deepEqual', 'notDeepEqual'],
|
|
249
|
+
['deepStrictEqual', 'notDeepStrictEqual'], ['throws', 'doesNotThrow'], ['rejects', 'doesNotReject'],
|
|
250
|
+
['match', 'doesNotMatch'], ['True', 'False'], ['Equal', 'NotEqual'], ['Nil', 'NotNil'], ['Error', 'NoError'],
|
|
251
|
+
['Empty', 'NotEmpty'], ['Contains', 'NotContains'], ['Zero', 'NotZero'], ['IsTrue', 'IsFalse'],
|
|
252
|
+
['AreEqual', 'AreNotEqual'], ['IsNull', 'IsNotNull'], ['XCTAssertTrue', 'XCTAssertFalse'],
|
|
253
|
+
['XCTAssertEqual', 'XCTAssertNotEqual'], ['XCTAssertNil', 'XCTAssertNotNil'], ['EXPECT_TRUE', 'EXPECT_FALSE'],
|
|
254
|
+
['ASSERT_TRUE', 'ASSERT_FALSE'], ['EXPECT_EQ', 'EXPECT_NE'], ['ASSERT_EQ', 'ASSERT_NE'],
|
|
255
|
+
].flatMap(([a, b]) => [[a, b], [b, a]]),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
function negated(a, b) {
|
|
259
|
+
const strip = (s) => {
|
|
260
|
+
const m = /^(?:!\s*|not\s+)(.+)$/.exec(s);
|
|
261
|
+
return m ? m[1].replace(/^\((.*)\)$/, '$1').trim() : null;
|
|
262
|
+
};
|
|
263
|
+
const unwrap = (s) => s.replace(/^\((.*)\)$/, '$1').trim();
|
|
264
|
+
return strip(a) === unwrap(b) || strip(b) === unwrap(a);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// `f(x)` to `f(x, opts)`: the same function, called with arguments added or dropped at the end.
|
|
268
|
+
function callArgsExtended(before, after) {
|
|
269
|
+
const call = (s) => {
|
|
270
|
+
const m = /^([\w$.]+)\s*\(/.exec(s);
|
|
271
|
+
if (!m) return null;
|
|
272
|
+
const close = matchParen(s, m[0].length - 1);
|
|
273
|
+
if (close !== s.length - 1) return null;
|
|
274
|
+
return { callee: m[1], args: splitArgs(s.slice(m[0].length, close)).map(norm).filter(Boolean) };
|
|
275
|
+
};
|
|
276
|
+
const b = call(before);
|
|
277
|
+
const a = call(after);
|
|
278
|
+
if (!b || !a || b.callee !== a.callee || b.args.length === a.args.length) return false;
|
|
279
|
+
const [short, long] = b.args.length < a.args.length ? [b.args, a.args] : [a.args, b.args];
|
|
280
|
+
return short.every((x, i) => x === long[i]);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// A removed assertion against an added one: null, or { type, what, from, to }.
|
|
284
|
+
// type 'weakened' or 'changed'; what says which change, for the finding's wording.
|
|
285
|
+
const pair = (type, what, from, to) => ({ type, what, from: truncate(from, 50), to: truncate(to, 50) });
|
|
286
|
+
function compareAssertions(r, a) {
|
|
287
|
+
if (r.kind === 'call' && a.kind === 'call') {
|
|
288
|
+
const showCall = (x) => `${x.matcher}(${x.args.join(', ')})`;
|
|
289
|
+
if (r.matcher === a.matcher && r.args.length === a.args.length && r.args.length >= 2) {
|
|
290
|
+
const diffs = [0, 1].filter((i) => r.args[i] !== a.args[i]);
|
|
291
|
+
if (diffs.length !== 1) return null;
|
|
292
|
+
const i = diffs[0];
|
|
293
|
+
if (goalpostMoved(r.args[i], a.args[i])) return pair('changed', 'expected', r.args[i], a.args[i]);
|
|
294
|
+
return callArgsExtended(r.args[i], a.args[i]) ? pair('changed', 'call', r.args[i], a.args[i]) : null;
|
|
295
|
+
}
|
|
296
|
+
if (r.matcher === a.matcher && r.args.length === 1 && a.args.length === 1 && negated(r.args[0], a.args[0])) return pair('changed', 'flipped', showCall(r), showCall(a));
|
|
297
|
+
if (OPPOSITES.get(r.matcher) === a.matcher && r.args.length === a.args.length && r.args.every((x, i) => x === a.args[i])) return pair('changed', 'flipped', showCall(r), showCall(a));
|
|
298
|
+
if (STRONG.has(r.matcher) && WEAK.has(a.matcher) && a.args.some((x) => r.args.slice(0, 2).includes(x))) return pair('weakened', 'weakened', `${r.matcher}(…)`, `${a.matcher}(…)`);
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
if (r.kind === 'py' && a.kind === 'py' && r.matcher === 'assert' && a.matcher === 'assert' && negated(r.subject, a.subject)) return pair('changed', 'flipped', `assert ${r.subject}`, `assert ${a.subject}`);
|
|
302
|
+
if (r.kind === a.kind && r.kind !== 'kv' && r.subject !== a.subject && r.matcher === a.matcher && r.args === a.args && callArgsExtended(r.subject, a.subject)) return pair('changed', 'call', r.subject, a.subject);
|
|
303
|
+
if (r.kind !== a.kind || r.subject === undefined || r.subject !== a.subject) return null;
|
|
304
|
+
if (r.kind === 'kv') return r.args === a.args || !goalpostMoved(r.args, a.args) ? null : pair('changed', 'expected', `${r.subject} ${r.args}`, `${a.subject} ${a.args}`);
|
|
305
|
+
if (r.matcher === a.matcher) {
|
|
306
|
+
if (r.args === a.args) return null;
|
|
307
|
+
if (a.args === '' && r.args !== '') return pair('weakened', 'weakened', `${r.matcher}(${truncate(r.args, 40)})`, `${a.matcher}()`);
|
|
308
|
+
return pair('changed', 'expected', r.args, a.args);
|
|
309
|
+
}
|
|
310
|
+
const strongOld = STRONG.has(r.matcher) || STRONG.has(r.matcher.replace(/^not\./, ''));
|
|
311
|
+
const weakNew = WEAK.has(a.matcher) || (r.kind === 'py' && (a.matcher === 'assert' || a.matcher === 'is not'));
|
|
312
|
+
const show = (x) => (x.kind === 'py' ? `assert ${x.subject}${x.matcher === 'assert' ? '' : ` ${x.matcher} ${truncate(x.args, 40)}`}` : `${x.matcher}(${truncate(x.args, 40)})`);
|
|
313
|
+
if (strongOld && weakNew) return pair('weakened', 'weakened', show(r), show(a));
|
|
314
|
+
return pair('changed', 'assertion', show(r), show(a));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---------- test tables (table-driven tests, parametrize lists, `for (x of [..])`) ----------
|
|
318
|
+
|
|
319
|
+
const STRING_SRC = String.raw`'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"`;
|
|
320
|
+
const LITERAL_SRC = String.raw`${STRING_SRC}|-?\d[\d_]*(?:\.\d+)?(?:[eE][+-]?\d+)?n?|\b(?:true|false|null|undefined|None|True|False|nil)\b`;
|
|
321
|
+
const ROW_TOKEN = new RegExp(String.raw`(\s+)|((?:[A-Za-z_$][\w$]*|${STRING_SRC})\s*:(?!:))|(${LITERAL_SRC})|([[\](){},;])`, 'y');
|
|
322
|
+
|
|
323
|
+
// A line that is only data, such as `[1900, true],`, `(1900, True),`, `{ in: 5, want: 120 },` or `1900,`.
|
|
324
|
+
function dataRow(text) {
|
|
325
|
+
const t = text.trim();
|
|
326
|
+
if (!t || !(/^[[({]/.test(t) || /,$/.test(t))) return null;
|
|
327
|
+
const literals = [];
|
|
328
|
+
let shape = '';
|
|
329
|
+
let depth = 0;
|
|
330
|
+
ROW_TOKEN.lastIndex = 0;
|
|
331
|
+
while (ROW_TOKEN.lastIndex < t.length) {
|
|
332
|
+
const m = ROW_TOKEN.exec(t);
|
|
333
|
+
if (!m) return null;
|
|
334
|
+
if (m[1]) continue;
|
|
335
|
+
if (m[3]) {
|
|
336
|
+
literals.push(m[3]);
|
|
337
|
+
shape += 'L';
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (m[4] && '[({'.includes(m[4])) depth++;
|
|
341
|
+
if (m[4] && '])}'.includes(m[4]) && --depth < 0) return null; // the tail of a call: `{ name: 'X' });`
|
|
342
|
+
shape += (m[2] ?? m[4]).replace(/\s+/g, '');
|
|
343
|
+
}
|
|
344
|
+
// open brackets are the start of a multi-line value (`[true,`); a key with one value (`code: 1,`) is a property
|
|
345
|
+
if (depth !== 0 || !literals.length || (!/^[[({]/.test(t) && (literals.length > 1 || shape.includes(':')))) return null;
|
|
346
|
+
return { literals, shape };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const INLINE_LIST = new RegExp(String.raw`\[\s*((?:${LITERAL_SRC})(?:\s*,\s*(?:${LITERAL_SRC}))*)\s*,?\s*\]`, 'g');
|
|
350
|
+
function inlineLists(text) {
|
|
351
|
+
const lists = [];
|
|
352
|
+
const shape = text.replace(INLINE_LIST, (_, items) => {
|
|
353
|
+
lists.push(items.match(new RegExp(LITERAL_SRC, 'g')));
|
|
354
|
+
return '[L]';
|
|
355
|
+
});
|
|
356
|
+
return { lists, shape: normalizeLine(shape) };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// What is gone from big, if small is big with some items removed and nothing else.
|
|
360
|
+
function dropped(big, small) {
|
|
361
|
+
if (!small || small.length >= big.length) return null;
|
|
362
|
+
const gone = [];
|
|
363
|
+
let j = 0;
|
|
364
|
+
for (const item of big) {
|
|
365
|
+
if (j < small.length && small[j] === item) j++;
|
|
366
|
+
else gone.push(item);
|
|
367
|
+
}
|
|
368
|
+
return j === small.length ? gone : null;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Items dropped from a list on one line whose rest is unchanged, and rows moved within their table. Whole rows
|
|
372
|
+
// that disappear from multi-line tables are not reported: tables shrink when features go, so that signal is noise.
|
|
373
|
+
function tableChanges(file) {
|
|
374
|
+
const out = [];
|
|
375
|
+
for (const hunk of file.hunks) {
|
|
376
|
+
const used = new Set();
|
|
377
|
+
for (const r of hunk.removed) {
|
|
378
|
+
if (parseAssertion(r.text) || dataRow(r.text)) continue;
|
|
379
|
+
const before = inlineLists(r.text);
|
|
380
|
+
if (!before.lists.length) continue;
|
|
381
|
+
const a = hunk.added.find((x) => !used.has(x.line) && inlineLists(x.text).shape === before.shape);
|
|
382
|
+
if (!a) continue;
|
|
383
|
+
used.add(a.line);
|
|
384
|
+
const after = inlineLists(a.text);
|
|
385
|
+
before.lists.forEach((items, i) => {
|
|
386
|
+
const gone = dropped(items, after.lists[i]);
|
|
387
|
+
if (gone) out.push({ type: 'changed', what: 'dropped', from: truncate(gone.join(', '), 40) });
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (!file.oldLines || !file.newLines) return out;
|
|
392
|
+
const key = (text) => normalizeLine(text).replace(/,$/, '');
|
|
393
|
+
const rowLines = (lines) => lines.filter((l) => dataRow(l.text) && !parseAssertion(l.text));
|
|
394
|
+
const table = (lines, at) => {
|
|
395
|
+
let start = at - 1;
|
|
396
|
+
let end = at - 1;
|
|
397
|
+
while (start > 0 && dataRow(lines[start - 1])) start--;
|
|
398
|
+
while (end < lines.length - 1 && dataRow(lines[end + 1])) end++;
|
|
399
|
+
return lines.slice(start, end + 1).map(key);
|
|
400
|
+
};
|
|
401
|
+
const addedRows = rowLines(file.added);
|
|
402
|
+
for (const r of rowLines(file.removed)) {
|
|
403
|
+
const a = addedRows.find((x) => key(x.text) === key(r.text));
|
|
404
|
+
if (!a) continue;
|
|
405
|
+
const before = table(file.oldLines, r.line);
|
|
406
|
+
const after = table(file.newLines, a.line);
|
|
407
|
+
const sorted = (list) => [...list].sort().join('\n');
|
|
408
|
+
if (before.join('\n') !== after.join('\n') && sorted(before) === sorted(after)) {
|
|
409
|
+
out.push({ type: 'changed', what: 'reordered', from: truncate(key(r.text), 50) });
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// ---------- the file model, from a unified diff and the files as they are now ----------
|
|
417
|
+
|
|
418
|
+
function parseDiff(diff) {
|
|
419
|
+
const files = [];
|
|
420
|
+
let f = null;
|
|
421
|
+
let h = null;
|
|
422
|
+
let oldLine = 0;
|
|
423
|
+
let newLine = 0;
|
|
424
|
+
let from = '';
|
|
425
|
+
for (const line of diff.split('\n')) {
|
|
426
|
+
if (line.startsWith('diff --git ')) {
|
|
427
|
+
f = null;
|
|
428
|
+
h = null;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (line.startsWith('--- ') && !h) {
|
|
432
|
+
from = unquotePath(line.slice(4)).replace(/^a\//, '');
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (line.startsWith('+++ ') && !h) {
|
|
436
|
+
const to = unquotePath(line.slice(4)).replace(/^b\//, '');
|
|
437
|
+
f = { path: to === '/dev/null' ? from : to, status: from === '/dev/null' ? 'A' : to === '/dev/null' ? 'D' : 'M', hunks: [], removed: [], added: [] };
|
|
438
|
+
files.push(f);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
const m = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
442
|
+
if (m && f) {
|
|
443
|
+
h = { oldStart: +m[1], newStart: +m[3], newCount: m[4] === undefined ? 1 : +m[4], removed: [], added: [], lines: [] };
|
|
444
|
+
f.hunks.push(h);
|
|
445
|
+
[oldLine, newLine] = [h.oldStart, h.newStart];
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (!h || line.startsWith('\\')) continue;
|
|
449
|
+
if (line.startsWith('-')) {
|
|
450
|
+
const r = { line: oldLine++, text: line.slice(1, 10001) }; // the patterns can take quadratic time on a long line
|
|
451
|
+
h.removed.push(r), f.removed.push(r), h.lines.push(line);
|
|
452
|
+
} else if (line.startsWith('+')) {
|
|
453
|
+
const a = { line: newLine++, text: line.slice(1, 10001) };
|
|
454
|
+
h.added.push(a), f.added.push(a), h.lines.push(line);
|
|
455
|
+
} else if (line.startsWith(' ')) oldLine++, newLine++, h.lines.push(line);
|
|
456
|
+
else h = null; // the end of this file's hunks
|
|
457
|
+
}
|
|
458
|
+
return files;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// The file before the change: the file as it is now with each hunk undone. Null if they do not match.
|
|
462
|
+
function undo(newLines, hunks) {
|
|
463
|
+
const old = [];
|
|
464
|
+
let cur = 0;
|
|
465
|
+
for (const h of hunks) {
|
|
466
|
+
const upto = h.newCount === 0 ? h.newStart : h.newStart - 1;
|
|
467
|
+
if (upto < cur || upto > newLines.length) return null;
|
|
468
|
+
while (cur < upto) old.push(newLines[cur++]);
|
|
469
|
+
for (const l of h.lines) {
|
|
470
|
+
if (l[0] === '-') old.push(l.slice(1));
|
|
471
|
+
else if (newLines[cur]?.replace(/\r$/, '') !== l.slice(1).replace(/\r$/, '')) return null;
|
|
472
|
+
else if (l[0] === ' ') old.push(newLines[cur++]);
|
|
473
|
+
else cur++;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
while (cur < newLines.length) old.push(newLines[cur++]);
|
|
477
|
+
return old;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const OPTIONS_SKIP = /\b(?:it|test|describe|suite|specify)\s*\(\s*(['"`])((?:\\.|(?!\1).)*)\1\s*,\s*\{[^}]*\b(?:skip|todo)\b(?!\s*:\s*(?:false|null|undefined|0)\b)/;
|
|
481
|
+
const lineOfIndex = (text, index) => text.slice(0, index).split('\n').length;
|
|
482
|
+
|
|
483
|
+
// The changes, as { type: 'changed' | 'weakened' | 'replaced' | 'skipped', file, what, from, to }.
|
|
484
|
+
// isTestFile picks the test files; read(file) returns a file as it is now, or null.
|
|
485
|
+
export function testChanges(diff, { read, isTestFile }) {
|
|
486
|
+
const files = parseDiff(diff).filter((f) => isTestFile(f.path) && languageOf(f.path));
|
|
487
|
+
for (const f of files) {
|
|
488
|
+
const now = f.status === 'D' ? null : read(f.path);
|
|
489
|
+
f.newLines = now == null ? null : now.split('\n');
|
|
490
|
+
f.oldLines = f.status === 'A' ? [] : f.status === 'D' ? f.removed.map((r) => r.text) : f.newLines && undo(f.newLines, f.hunks);
|
|
491
|
+
f.newText = f.status === 'D' ? '' : f.newLines && f.newLines.join('\n');
|
|
492
|
+
f.oldText = f.oldLines && f.oldLines.join('\n');
|
|
493
|
+
}
|
|
494
|
+
const changes = [];
|
|
495
|
+
|
|
496
|
+
// Tests that went: moved to another file, replaced by a new one, or removed. Only replaced ones are reported
|
|
497
|
+
// here; a removed test is counted with the removed assertions, and a deleted file on its own.
|
|
498
|
+
const perFile = [];
|
|
499
|
+
const addedPool = new Map();
|
|
500
|
+
for (const file of files.filter((x) => x.oldText != null && x.newText != null)) {
|
|
501
|
+
const lang = languageOf(file.path);
|
|
502
|
+
const oldNames = extractTestNames(file.oldText, lang);
|
|
503
|
+
const newNames = extractTestNames(file.newText, lang);
|
|
504
|
+
const removed = [];
|
|
505
|
+
const added = [];
|
|
506
|
+
for (const [name, { count, index }] of oldNames) for (let i = newNames.get(name)?.count ?? 0; i < count; i++) removed.push({ name, index });
|
|
507
|
+
for (const [name, { count, index }] of newNames) {
|
|
508
|
+
const before = oldNames.get(name)?.count ?? 0;
|
|
509
|
+
for (let i = before; i < count; i++) added.push({ name, index });
|
|
510
|
+
if (count > before) addedPool.set(name, (addedPool.get(name) ?? 0) + (count - before));
|
|
511
|
+
}
|
|
512
|
+
perFile.push({ file, removed, added });
|
|
513
|
+
}
|
|
514
|
+
for (const entry of perFile) {
|
|
515
|
+
entry.removed = entry.removed.filter(({ name }) => {
|
|
516
|
+
const n = addedPool.get(name) ?? 0;
|
|
517
|
+
if (n === 0) return true;
|
|
518
|
+
addedPool.set(name, n - 1);
|
|
519
|
+
return false;
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
const removedIn = new Set();
|
|
523
|
+
const addedIn = (pred) => perFile.filter((e) => e.file.status !== 'D' && pred(e.file)).flatMap((e) => e.added.map((a) => ({ ...a, file: e.file })));
|
|
524
|
+
const take = (list, pick) => {
|
|
525
|
+
const i = list.findIndex((a) => (addedPool.get(a.name) ?? 0) > 0 && pick(a));
|
|
526
|
+
if (i < 0) return null;
|
|
527
|
+
const [hit] = list.splice(i, 1);
|
|
528
|
+
addedPool.set(hit.name, addedPool.get(hit.name) - 1);
|
|
529
|
+
return hit;
|
|
530
|
+
};
|
|
531
|
+
for (const { file, removed } of perFile) {
|
|
532
|
+
if (file.status === 'D') {
|
|
533
|
+
if (removed.length) removedIn.add(file.path);
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
// only a file that lost tests needs the added ones; building them for every file is quadratic in new files
|
|
537
|
+
// (cold review, L13 round 7)
|
|
538
|
+
if (!removed.length) continue;
|
|
539
|
+
const own = addedIn((x) => x === file);
|
|
540
|
+
const elsewhere = addedIn((x) => x !== file);
|
|
541
|
+
for (const r of removed) {
|
|
542
|
+
const replacement = take(own, () => true) ?? take(elsewhere, (a) => similarNames(r.name, a.name));
|
|
543
|
+
if (replacement) changes.push({ type: 'replaced', file: file.path, what: 'replaced', from: truncate(r.name, 50), to: truncate(replacement.name, 50), where: replacement.file === file ? null : replacement.file.path, line: lineOfIndex(file.newText, replacement.index) });
|
|
544
|
+
else removedIn.add(file.path);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// Assertions changed in place: each removed assertion against the first added one in its hunk that it matches.
|
|
549
|
+
for (const file of files) {
|
|
550
|
+
for (const hunk of file.hunks) {
|
|
551
|
+
if (!hunk.removed.length || !hunk.added.length) continue;
|
|
552
|
+
const rem = hunk.removed.map((r) => ({ ...r, a: parseAssertion(r.text) })).filter((x) => x.a);
|
|
553
|
+
const add = hunk.added.map((x) => ({ ...x, a: parseAssertion(x.text) })).filter((x) => x.a);
|
|
554
|
+
if (rem.length * add.length > 250000) continue; // a rewritten test file, not an edit; the other checks still read it
|
|
555
|
+
const used = new Set();
|
|
556
|
+
for (const r of rem) {
|
|
557
|
+
for (const a of add) {
|
|
558
|
+
if (used.has(a.line)) continue;
|
|
559
|
+
if (normalizeLine(r.text) === normalizeLine(a.text)) {
|
|
560
|
+
used.add(a.line);
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
const cmp = compareAssertions(r.a, a.a);
|
|
564
|
+
if (!cmp) continue;
|
|
565
|
+
used.add(a.line);
|
|
566
|
+
changes.push({ ...cmp, file: file.path });
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// A skip option added to a test that already existed: test('name', { skip }, fn). New tests use the same
|
|
574
|
+
// option to declare what they need (`{ skip: !hasCrypto }`), so a test whose name is new does not count.
|
|
575
|
+
for (const file of files) {
|
|
576
|
+
if (languageOf(file.path) !== 'js' || file.oldText == null) continue;
|
|
577
|
+
const before = new Set(file.removed.map((r) => normalizeLine(r.text)));
|
|
578
|
+
for (const a of file.added) {
|
|
579
|
+
const m = OPTIONS_SKIP.exec(a.text);
|
|
580
|
+
if (m && !isCommentLine(a.text) && !before.has(normalizeLine(a.text)) && file.oldText.includes(m[2])) {
|
|
581
|
+
changes.push({ type: 'skipped', file: file.path, what: 'skipped', from: truncate(m[2], 50) });
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Test data dropped or reordered, in files that lost no whole test.
|
|
588
|
+
for (const file of files) if (file.status === 'M' && !removedIn.has(file.path)) for (const c of tableChanges(file)) changes.push({ ...c, file: file.path });
|
|
589
|
+
return changes;
|
|
590
|
+
}
|