@criogaid/pi-hashline-edit 0.4.0 → 0.5.0
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 +116 -20
- package/package.json +3 -3
- package/src/core/apply.test.ts +194 -104
- package/src/core/apply.ts +6 -37
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +8 -8
- package/src/core/hash.ts +5 -5
- package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
- package/src/core/text.ts +1 -3
- package/src/core/types.ts +4 -7
- package/src/index.ts +9 -11
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +73 -60
- package/src/integration/large-text.test.ts +9 -8
- package/src/integration/replace-package.test.ts +11 -7
- package/src/pi/action-fusion.test.ts +110 -34
- package/src/pi/action-fusion.ts +77 -114
- package/src/pi/anchor-format.test.ts +43 -22
- package/src/pi/anchor-format.ts +23 -8
- package/src/pi/budgets.ts +7 -2
- package/src/pi/config.test.ts +153 -40
- package/src/pi/config.ts +87 -41
- package/src/pi/e2e-scenarios.test.ts +12 -11
- package/src/pi/edit-tool.ts +20 -156
- package/src/pi/error-text.ts +10 -5
- package/src/pi/execute.test.ts +299 -175
- package/src/pi/failure-context.test.ts +7 -2
- package/src/pi/failure-context.ts +155 -8
- package/src/pi/failure-path.test.ts +33 -26
- package/src/pi/file-commit.ts +2 -1
- package/src/pi/fusion-card.ts +83 -2
- package/src/pi/grep-output.ts +26 -9
- package/src/pi/grep-render.ts +6 -5
- package/src/pi/grep-scope.ts +2 -15
- package/src/pi/grep-search.ts +29 -15
- package/src/pi/grep-tool.ts +94 -78
- package/src/pi/grep.test.ts +357 -320
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +51 -2
- package/src/pi/mutation-result.ts +62 -55
- package/src/pi/mutation-runner.ts +16 -33
- package/src/pi/pi.test.ts +10 -5
- package/src/pi/read-tool.ts +61 -33
- package/src/pi/registration.test.ts +1 -5
- package/src/pi/render.test.ts +16 -8
- package/src/pi/render.ts +4 -91
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +24 -58
- package/src/pi/replace-worker.ts +2 -2
- package/src/pi/replace.test.ts +62 -58
- package/src/pi/result-safety.test.ts +124 -84
- package/src/pi/rg-line-ranges.ts +3 -2
- package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +35 -4
- package/src/pi/rg-text-view.ts +17 -33
- package/src/pi/schema.ts +2 -10
- package/src/pi/write-tool.ts +1 -2
- package/src/pi/write.test.ts +10 -9
- package/src/core/index.ts +0 -14
- package/src/pi/state.ts +0 -26
package/src/pi/grep.test.ts
CHANGED
|
@@ -11,18 +11,18 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
11
11
|
import { tmpdir } from "node:os";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { rgPath } from "@vscode/ripgrep";
|
|
14
|
-
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
15
14
|
import { computeLineHash } from "../core/hash.ts";
|
|
16
15
|
import { makeGrepOverrideWithBackend, type GrepBackend } from "./grep-tool.ts";
|
|
16
|
+
import { scopeArgs, type SearchRequest } from "./grep-search.ts";
|
|
17
17
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
18
|
-
import { getState } from "./state.ts";
|
|
19
18
|
import { callTool } from "./tool-call.testing.ts";
|
|
19
|
+
import { DEFAULT_CONFIG } from "./config.ts";
|
|
20
20
|
|
|
21
21
|
type FakeOptions = {
|
|
22
22
|
lines?: string[];
|
|
23
23
|
code?: number | null;
|
|
24
24
|
stderr?: string;
|
|
25
|
-
validation?:
|
|
25
|
+
validation?: Awaited<ReturnType<GrepBackend["probeRegex"]>>;
|
|
26
26
|
error?: Error;
|
|
27
27
|
onRun?: () => void;
|
|
28
28
|
paths?: string[];
|
|
@@ -55,15 +55,13 @@ function fakeSmartCase(patterns: readonly string[]): boolean {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
function fakeBackend(options: FakeOptions = {}) {
|
|
58
|
-
const calls: { path: string;
|
|
58
|
+
const calls: { path: string; request: SearchRequest }[] = [];
|
|
59
|
+
const probes: Parameters<GrepBackend["probeRegex"]>[] = [];
|
|
59
60
|
const backend: GrepBackend = {
|
|
60
|
-
async
|
|
61
|
-
calls.push({ path,
|
|
61
|
+
async search(path, request, _signal, onLine) {
|
|
62
|
+
calls.push({ path, request });
|
|
62
63
|
options.onRun?.();
|
|
63
64
|
if (options.error) throw options.error;
|
|
64
|
-
if (args.includes("--quiet")) {
|
|
65
|
-
return { code: 1, stderr: "", ...options.validation, stopped: false };
|
|
66
|
-
}
|
|
67
65
|
for (const line of options.lines ?? []) {
|
|
68
66
|
if (!(await onLine(line)))
|
|
69
67
|
return { code: null, stderr: options.stderr ?? "", stopped: true };
|
|
@@ -83,8 +81,12 @@ function fakeBackend(options: FakeOptions = {}) {
|
|
|
83
81
|
async resolveIgnoreCase(_path, patterns, _modes, explicit) {
|
|
84
82
|
return explicit ?? fakeSmartCase(patterns);
|
|
85
83
|
},
|
|
84
|
+
async probeRegex(...args) {
|
|
85
|
+
probes.push(args);
|
|
86
|
+
return { code: 1, stderr: "", ...options.validation };
|
|
87
|
+
},
|
|
86
88
|
};
|
|
87
|
-
return { backend, calls };
|
|
89
|
+
return { backend, calls, probes };
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
const text = (result: any): string => result.content[0].text;
|
|
@@ -92,7 +94,7 @@ const call = (tool: any, params: any, signal?: AbortSignal) =>
|
|
|
92
94
|
callTool(tool, params, { toolCallId: "0", signal });
|
|
93
95
|
|
|
94
96
|
test("grep guidance covers query syntax, search scope, and edit anchors", () => {
|
|
95
|
-
const tool = makeGrepOverrideWithBackend(process.cwd(), {});
|
|
97
|
+
const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {});
|
|
96
98
|
for (const [topic, terms] of [
|
|
97
99
|
["literal code", [/literal:true/, /regex punctuation/]],
|
|
98
100
|
["Rust regex", [/ripgrep \(Rust\)/, /lookaround/, /backreferences/]],
|
|
@@ -129,10 +131,59 @@ test("grep guidance covers query syntax, search scope, and edit anchors", () =>
|
|
|
129
131
|
assert.match(description("ignoreCase"), /omitted.*smart-case.*whole query/);
|
|
130
132
|
});
|
|
131
133
|
|
|
134
|
+
test("configured grep defaults drive descriptions, match limits, and context", async () =>
|
|
135
|
+
withDir(async (dir) => {
|
|
136
|
+
const file = join(dir, "configured.txt");
|
|
137
|
+
const lines = [
|
|
138
|
+
"before one",
|
|
139
|
+
"needle one",
|
|
140
|
+
"after one",
|
|
141
|
+
"gap",
|
|
142
|
+
"before two",
|
|
143
|
+
"needle two",
|
|
144
|
+
"after two",
|
|
145
|
+
];
|
|
146
|
+
await writeFile(file, `${lines.join("\n")}\n`);
|
|
147
|
+
const fake = fakeBackend({
|
|
148
|
+
lines: [rgMatch(file, 2, "needle one\n"), rgMatch(file, 6, "needle two\n")],
|
|
149
|
+
});
|
|
150
|
+
const config = { ...DEFAULT_CONFIG, grep: { defaultLimit: 1, defaultContext: 2 } };
|
|
151
|
+
const tool = makeGrepOverrideWithBackend(dir, config, fake.backend);
|
|
152
|
+
for (const [field, expected] of [
|
|
153
|
+
["limit", 1],
|
|
154
|
+
["context", 2],
|
|
155
|
+
] as const) {
|
|
156
|
+
const description: unknown = Reflect.get(tool.parameters.properties[field], "description");
|
|
157
|
+
assert.ok(typeof description === "string");
|
|
158
|
+
assert.match(description, new RegExp(`default ${expected}\\)`));
|
|
159
|
+
}
|
|
160
|
+
const expectedRow = (line: number) =>
|
|
161
|
+
`${line}#${computeLineHash(line, lines[line - 1], config.hashLen)}│${lines[line - 1]}`;
|
|
162
|
+
const rows = (result: unknown) =>
|
|
163
|
+
text(result)
|
|
164
|
+
.split("\n")
|
|
165
|
+
.filter((line) => /^\d+#/.test(line));
|
|
166
|
+
|
|
167
|
+
const defaults = await call(tool, { path: file, pattern: "needle", literal: true });
|
|
168
|
+
assert.deepEqual(rows(defaults), [1, 2, 3, 4].map(expectedRow));
|
|
169
|
+
assert.match(text(defaults), /configured\.txt · 1 match\n/);
|
|
170
|
+
assert.match(text(defaults), /1 matches limit reached/);
|
|
171
|
+
|
|
172
|
+
const explicit = await call(tool, {
|
|
173
|
+
path: file,
|
|
174
|
+
pattern: "needle",
|
|
175
|
+
literal: true,
|
|
176
|
+
limit: 2,
|
|
177
|
+
context: 0,
|
|
178
|
+
});
|
|
179
|
+
assert.deepEqual(rows(explicit), [2, 6].map(expectedRow));
|
|
180
|
+
assert.match(text(explicit), /configured\.txt · 2 matches\n/);
|
|
181
|
+
}));
|
|
182
|
+
|
|
132
183
|
test("grep exposes nine parameters and rejects only the six removed fields", async () => {
|
|
133
184
|
await withDir(async (dir) => {
|
|
134
185
|
const fake = fakeBackend();
|
|
135
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
186
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
136
187
|
assert.deepEqual(Object.keys(tool.parameters.properties).sort(), [
|
|
137
188
|
"context",
|
|
138
189
|
"glob",
|
|
@@ -177,39 +228,27 @@ test("grep exposes nine parameters and rejects only the six removed fields", asy
|
|
|
177
228
|
test("grep limit accepts only positive integers", async () => {
|
|
178
229
|
await withDir(async (dir) => {
|
|
179
230
|
const fake = fakeBackend();
|
|
180
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
231
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
181
232
|
const limitSchema = tool.parameters.properties.limit;
|
|
182
233
|
assert.equal(limitSchema.type, "number");
|
|
183
234
|
assert.equal("minimum" in limitSchema ? limitSchema.minimum : undefined, 1);
|
|
184
235
|
assert.equal("multipleOf" in limitSchema ? limitSchema.multipleOf : undefined, 1);
|
|
185
|
-
const validate = (limit: number) =>
|
|
186
|
-
validateToolArguments(tool, {
|
|
187
|
-
type: "toolCall",
|
|
188
|
-
id: "0",
|
|
189
|
-
name: "grep",
|
|
190
|
-
arguments: { pattern: "needle", limit },
|
|
191
|
-
});
|
|
192
|
-
assert.doesNotThrow(() => validate(1));
|
|
193
236
|
for (const limit of [0, -3, 0.5, 2.5]) {
|
|
194
|
-
assert.throws(
|
|
195
|
-
() => validate(limit),
|
|
196
|
-
/Validation failed/,
|
|
197
|
-
`schema must reject limit ${limit}`,
|
|
198
|
-
);
|
|
199
237
|
await assert.rejects(
|
|
200
238
|
call(tool, { pattern: "needle", limit }),
|
|
201
239
|
/Validation failed for tool "grep":\n {2}- limit: /,
|
|
202
240
|
);
|
|
203
241
|
}
|
|
204
242
|
assert.equal(fake.calls.length, 0);
|
|
243
|
+
await assert.doesNotReject(call(tool, { pattern: "needle", limit: 1 }));
|
|
205
244
|
});
|
|
206
245
|
});
|
|
207
246
|
|
|
208
247
|
test("grep schema rejects empty search inputs and fractional context", async () =>
|
|
209
248
|
withDir(async (dir) => {
|
|
210
249
|
const fake = fakeBackend();
|
|
211
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
212
|
-
const invalidArgs
|
|
250
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
251
|
+
const invalidArgs = [
|
|
213
252
|
{ pattern: "" },
|
|
214
253
|
{ pattern: [] },
|
|
215
254
|
{ pattern: ["ok", ""] },
|
|
@@ -221,15 +260,9 @@ test("grep schema rejects empty search inputs and fractional context", async ()
|
|
|
221
260
|
{ pattern: "needle", limit: Number.MAX_SAFE_INTEGER + 1 },
|
|
222
261
|
];
|
|
223
262
|
for (const args of invalidArgs) {
|
|
224
|
-
assert.
|
|
225
|
-
()
|
|
226
|
-
|
|
227
|
-
type: "toolCall",
|
|
228
|
-
id: "invalid",
|
|
229
|
-
name: "grep",
|
|
230
|
-
arguments: args,
|
|
231
|
-
}),
|
|
232
|
-
/Validation failed/,
|
|
263
|
+
await assert.rejects(
|
|
264
|
+
call(tool, args),
|
|
265
|
+
/Validation failed for tool "grep"/,
|
|
233
266
|
JSON.stringify(args),
|
|
234
267
|
);
|
|
235
268
|
}
|
|
@@ -245,7 +278,7 @@ test("case and multiline options select only their matching ripgrep flags", asyn
|
|
|
245
278
|
const file = join(dir, "fixture.ts");
|
|
246
279
|
await writeFile(file, "FOO\nfoo\n");
|
|
247
280
|
const fake = fakeBackend();
|
|
248
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
281
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
249
282
|
await call(tool, {
|
|
250
283
|
path: file,
|
|
251
284
|
pattern: "FOO",
|
|
@@ -253,17 +286,17 @@ test("case and multiline options select only their matching ripgrep flags", asyn
|
|
|
253
286
|
multiline: true,
|
|
254
287
|
literal: true,
|
|
255
288
|
});
|
|
256
|
-
assert.ok(fake.calls[0].
|
|
257
|
-
assert.ok(fake.calls[0].
|
|
289
|
+
assert.ok(fake.calls[0].request.matcher.includes("--ignore-case"));
|
|
290
|
+
assert.ok(fake.calls[0].request.matcher.includes("--multiline"));
|
|
258
291
|
await call(tool, { path: file, pattern: "foo", ignoreCase: false, multiline: false });
|
|
259
|
-
assert.ok(fake.calls.at(-1)?.
|
|
260
|
-
assert.ok(fake.calls.at(-1)?.
|
|
292
|
+
assert.ok(fake.calls.at(-1)?.request.matcher.includes("--case-sensitive"));
|
|
293
|
+
assert.ok(fake.calls.at(-1)?.request.matcher.includes("--no-multiline"));
|
|
261
294
|
});
|
|
262
295
|
});
|
|
263
296
|
|
|
264
297
|
test("grep points wildcard paths to glob without changing ordinary missing-path errors", async () => {
|
|
265
298
|
await withDir(async (dir) => {
|
|
266
|
-
const tool = makeGrepOverrideWithBackend(dir, fakeBackend().backend);
|
|
299
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fakeBackend().backend);
|
|
267
300
|
await assert.rejects(
|
|
268
301
|
call(tool, { pattern: "value", path: "src/fusion-card*" }),
|
|
269
302
|
/Path not found: .*fusion-card\*.*Use an existing directory as path and a filename wildcard as glob/,
|
|
@@ -276,47 +309,36 @@ test("grep points wildcard paths to glob without changing ordinary missing-path
|
|
|
276
309
|
});
|
|
277
310
|
});
|
|
278
311
|
|
|
279
|
-
async function withEnabled<T>(enabled: boolean, fn: () => Promise<T>): Promise<T> {
|
|
280
|
-
const state = getState();
|
|
281
|
-
const previous = state.config.enabled;
|
|
282
|
-
state.config.enabled = enabled;
|
|
283
|
-
try {
|
|
284
|
-
return await fn();
|
|
285
|
-
} finally {
|
|
286
|
-
state.config.enabled = previous;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
312
|
test("formats parsed rg matches with full-line hash anchors", async () => {
|
|
291
|
-
await withDir(async (dir) =>
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
});
|
|
313
|
+
await withDir(async (dir) => {
|
|
314
|
+
const a = join(dir, "a.ts");
|
|
315
|
+
const b = join(dir, "b.ts");
|
|
316
|
+
await writeFile(a, "alpha beta\ngamma\nalpha only\n");
|
|
317
|
+
await writeFile(b, "alpha here\n");
|
|
318
|
+
const fake = fakeBackend({
|
|
319
|
+
lines: [
|
|
320
|
+
"not json",
|
|
321
|
+
JSON.stringify({ type: "begin" }),
|
|
322
|
+
rgMatch(a, 1, "alpha beta\n"),
|
|
323
|
+
rgMatch(a, 3, "alpha only\n"),
|
|
324
|
+
rgMatch(b, 1, "alpha here\n"),
|
|
325
|
+
],
|
|
326
|
+
});
|
|
306
327
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
328
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
329
|
+
assert.deepEqual(tool.parameters.required, ["pattern"]);
|
|
330
|
+
const result = await call(tool, {
|
|
331
|
+
pattern: "alpha",
|
|
332
|
+
});
|
|
333
|
+
const output = text(result);
|
|
334
|
+
assert.match(output, /a\.ts · 2 matches/);
|
|
335
|
+
assert.match(output, /b\.ts · 1 match/);
|
|
336
|
+
assert.match(output, new RegExp(`1#${computeLineHash(1, "alpha beta", 4)}│alpha beta`));
|
|
337
|
+
assert.match(output, /3#[0-9A-Z]+│alpha only/);
|
|
338
|
+
assert.deepEqual(fake.calls[0], {
|
|
339
|
+
path: rgPath,
|
|
340
|
+
request: {
|
|
341
|
+
matcher: [
|
|
320
342
|
"--no-config",
|
|
321
343
|
"--color=never",
|
|
322
344
|
"--no-crlf",
|
|
@@ -326,15 +348,13 @@ test("formats parsed rg matches with full-line hash anchors", async () => {
|
|
|
326
348
|
"--fixed-strings",
|
|
327
349
|
"--json",
|
|
328
350
|
"--line-number",
|
|
329
|
-
"--hidden",
|
|
330
351
|
"-e",
|
|
331
352
|
"alpha",
|
|
332
|
-
"--",
|
|
333
|
-
dir,
|
|
334
353
|
],
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
354
|
+
scope: { globs: [], noIgnore: false, follow: false, searchPaths: [dir] },
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
});
|
|
338
358
|
});
|
|
339
359
|
|
|
340
360
|
test("grep rejects a file changed between a match and anchor formatting", async () => {
|
|
@@ -344,14 +364,14 @@ test("grep rejects a file changed between a match and anchor formatting", async
|
|
|
344
364
|
const fake = fakeBackend({ lines: [rgMatch(file, 1, "needle\n")] });
|
|
345
365
|
const backend: GrepBackend = {
|
|
346
366
|
...fake.backend,
|
|
347
|
-
async
|
|
348
|
-
const result = await fake.backend.
|
|
367
|
+
async search(...args) {
|
|
368
|
+
const result = await fake.backend.search(...args);
|
|
349
369
|
await writeFile(file, "NOT_THE_MATCH\nneedle\n");
|
|
350
370
|
return result;
|
|
351
371
|
},
|
|
352
372
|
};
|
|
353
373
|
await assert.rejects(
|
|
354
|
-
call(makeGrepOverrideWithBackend(dir, backend), { pattern: "needle" }),
|
|
374
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), { pattern: "needle" }),
|
|
355
375
|
/File changed during search/,
|
|
356
376
|
);
|
|
357
377
|
});
|
|
@@ -364,13 +384,16 @@ test("grep rejects matches missing from the current file before anchoring contex
|
|
|
364
384
|
const fake = fakeBackend({ lines: [rgMatch(file, 5, "needle\n")] });
|
|
365
385
|
const backend: GrepBackend = {
|
|
366
386
|
...fake.backend,
|
|
367
|
-
async
|
|
387
|
+
async search(...args) {
|
|
368
388
|
await writeFile(file, "line1\nline2\nline3\n");
|
|
369
|
-
return fake.backend.
|
|
389
|
+
return fake.backend.search(...args);
|
|
370
390
|
},
|
|
371
391
|
};
|
|
372
392
|
await assert.rejects(
|
|
373
|
-
call(makeGrepOverrideWithBackend(dir, backend), {
|
|
393
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
|
|
394
|
+
pattern: "needle",
|
|
395
|
+
context: 2,
|
|
396
|
+
}),
|
|
374
397
|
/File changed during search/,
|
|
375
398
|
);
|
|
376
399
|
}));
|
|
@@ -394,14 +417,14 @@ test("multiline grep rejects a zero-width match beyond the current EOF", async (
|
|
|
394
417
|
});
|
|
395
418
|
const backend: GrepBackend = {
|
|
396
419
|
...fake.backend,
|
|
397
|
-
async
|
|
420
|
+
async search(...args) {
|
|
398
421
|
await writeFile(file, "one\ntwo\n");
|
|
399
|
-
return fake.backend.
|
|
422
|
+
return fake.backend.search(...args);
|
|
400
423
|
},
|
|
401
424
|
};
|
|
402
425
|
for (const outputMode of ["content", "files", "count"]) {
|
|
403
426
|
await assert.rejects(
|
|
404
|
-
call(makeGrepOverrideWithBackend(dir, backend), {
|
|
427
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
|
|
405
428
|
pattern: "(?m)^",
|
|
406
429
|
multiline: true,
|
|
407
430
|
literal: false,
|
|
@@ -430,7 +453,7 @@ test("multiline grep rejects spans beyond a truncated file", async () => {
|
|
|
430
453
|
],
|
|
431
454
|
});
|
|
432
455
|
await assert.rejects(
|
|
433
|
-
call(makeGrepOverrideWithBackend(dir, fake.backend), {
|
|
456
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
434
457
|
pattern: "alpha\\nbeta",
|
|
435
458
|
path: file,
|
|
436
459
|
literal: false,
|
|
@@ -447,137 +470,134 @@ test("grep rejects changed context even when the matched line stays the same", a
|
|
|
447
470
|
const fake = fakeBackend({ lines: [rgMatch(file, 1, "needle\n")] });
|
|
448
471
|
const backend: GrepBackend = {
|
|
449
472
|
...fake.backend,
|
|
450
|
-
async
|
|
451
|
-
const result = await fake.backend.
|
|
473
|
+
async search(...args) {
|
|
474
|
+
const result = await fake.backend.search(...args);
|
|
452
475
|
await writeFile(file, "needle\nnew context\n");
|
|
453
476
|
return result;
|
|
454
477
|
},
|
|
455
478
|
};
|
|
456
479
|
await assert.rejects(
|
|
457
|
-
call(makeGrepOverrideWithBackend(dir, backend), {
|
|
480
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
|
|
481
|
+
pattern: "needle",
|
|
482
|
+
context: 1,
|
|
483
|
+
}),
|
|
458
484
|
/File changed during search/,
|
|
459
485
|
);
|
|
460
486
|
});
|
|
461
487
|
});
|
|
462
488
|
|
|
463
489
|
test("grep in a subdirectory returns a path that edits the matching file", async () => {
|
|
464
|
-
await withDir(async (dir) =>
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}),
|
|
493
|
-
);
|
|
490
|
+
await withDir(async (dir) => {
|
|
491
|
+
await mkdir(join(dir, "src"));
|
|
492
|
+
const original = "export const status = 1;\n";
|
|
493
|
+
const rootFile = join(dir, "status.ts");
|
|
494
|
+
const matchedFile = join(dir, "src", "status.ts");
|
|
495
|
+
await writeFile(rootFile, original);
|
|
496
|
+
await writeFile(matchedFile, original);
|
|
497
|
+
const fake = fakeBackend({ lines: [rgMatch(matchedFile, 1, original)] });
|
|
498
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
499
|
+
pattern: "status",
|
|
500
|
+
path: "src",
|
|
501
|
+
});
|
|
502
|
+
const output = text(result);
|
|
503
|
+
const displayPath = output.split(" · ")[0];
|
|
504
|
+
const edit: any = makeEditOverride(dir, DEFAULT_CONFIG);
|
|
505
|
+
await call(edit, {
|
|
506
|
+
path: displayPath,
|
|
507
|
+
edits: [
|
|
508
|
+
{
|
|
509
|
+
op: "replace",
|
|
510
|
+
anchor: `1#${computeLineHash(1, original.trimEnd(), 4)}`,
|
|
511
|
+
body: ["export const status = 2;"],
|
|
512
|
+
},
|
|
513
|
+
],
|
|
514
|
+
});
|
|
515
|
+
assert.equal(await readFile(matchedFile, "utf-8"), "export const status = 2;\n");
|
|
516
|
+
assert.equal(await readFile(rootFile, "utf-8"), original);
|
|
517
|
+
});
|
|
494
518
|
});
|
|
495
519
|
|
|
496
520
|
test("context preserves logical CRLF anchors around a matched line", async () => {
|
|
497
|
-
await withDir(async (dir) =>
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
const fake = fakeBackend({ lines: [rgMatch(file, 4, "alpha beta\n")] });
|
|
521
|
+
await withDir(async (dir) => {
|
|
522
|
+
const file = join(dir, "a.ts");
|
|
523
|
+
await writeFile(
|
|
524
|
+
file,
|
|
525
|
+
"outside-before\r\nalpha beta drop\r\nbefore survivor\r\nalpha beta\r\nafter survivor\r\nalpha only\r\noutside-after\r\n",
|
|
526
|
+
);
|
|
527
|
+
const fake = fakeBackend({ lines: [rgMatch(file, 4, "alpha beta\n")] });
|
|
505
528
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
);
|
|
529
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
530
|
+
const contextSchema: any = tool.parameters.properties.context;
|
|
531
|
+
assert.equal(contextSchema.type, "number");
|
|
532
|
+
assert.equal(contextSchema.minimum, 0);
|
|
533
|
+
assert.equal(contextSchema.maximum, 20);
|
|
534
|
+
assert.equal(contextSchema.multipleOf, 1);
|
|
535
|
+
const result = await call(tool, { pattern: "beta$", context: 1 });
|
|
536
|
+
assert.equal(
|
|
537
|
+
text(result),
|
|
538
|
+
[
|
|
539
|
+
"a.ts · 1 match",
|
|
540
|
+
`3#${computeLineHash(3, "before survivor", 4)}│before survivor`,
|
|
541
|
+
`4#${computeLineHash(4, "alpha beta", 4)}│alpha beta`,
|
|
542
|
+
`5#${computeLineHash(5, "after survivor", 4)}│after survivor`,
|
|
543
|
+
].join("\n"),
|
|
544
|
+
);
|
|
545
|
+
});
|
|
524
546
|
});
|
|
525
547
|
|
|
526
548
|
test("passes output flags and formats files and counts", async () => {
|
|
527
|
-
await withDir(async (dir) =>
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
});
|
|
549
|
+
await withDir(async (dir) => {
|
|
550
|
+
const a = join(dir, "a.ts");
|
|
551
|
+
const b = join(dir, "b.ts");
|
|
552
|
+
await writeFile(a, "Foo a.b\n");
|
|
553
|
+
await writeFile(b, "foo a.b\n");
|
|
554
|
+
const fake = fakeBackend({
|
|
555
|
+
lines: [rgMatch(a, 1, "Foo a.b\n"), rgMatch(b, 1, "foo a.b\n")],
|
|
556
|
+
paths: [a, b],
|
|
557
|
+
});
|
|
537
558
|
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
559
|
+
const files = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
560
|
+
pattern: ["Foo", "a.b"],
|
|
561
|
+
path: ["a.ts", "b.ts"],
|
|
562
|
+
glob: ["*.ts", "!**/*.test.ts"],
|
|
563
|
+
literal: true,
|
|
564
|
+
outputMode: "files",
|
|
565
|
+
});
|
|
566
|
+
assert.equal(text(files), "a.ts\nb.ts");
|
|
567
|
+
assert.deepEqual(fake.calls[0].request.matcher, [
|
|
568
|
+
"--no-config",
|
|
569
|
+
"--color=never",
|
|
570
|
+
"--no-crlf",
|
|
571
|
+
"--engine=default",
|
|
572
|
+
"--no-multiline",
|
|
573
|
+
"--case-sensitive",
|
|
574
|
+
"--fixed-strings",
|
|
575
|
+
"--json",
|
|
576
|
+
"--line-number",
|
|
577
|
+
"-e",
|
|
578
|
+
"Foo",
|
|
579
|
+
"-e",
|
|
580
|
+
"a.b",
|
|
581
|
+
]);
|
|
582
|
+
assert.deepEqual(scopeArgs(fake.calls[0].request.scope), [
|
|
583
|
+
"--hidden",
|
|
584
|
+
"--glob",
|
|
585
|
+
"*.ts",
|
|
586
|
+
"--glob",
|
|
587
|
+
"!**/*.test.ts",
|
|
588
|
+
]);
|
|
589
|
+
assert.deepEqual(fake.calls[0].request.scope.searchPaths, [a, b]);
|
|
569
590
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
);
|
|
591
|
+
const count = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
592
|
+
pattern: "foo",
|
|
593
|
+
outputMode: "count",
|
|
594
|
+
});
|
|
595
|
+
assert.equal(text(count), "a.ts: 1\nb.ts: 1\nTotal: 2 matches in 2 files");
|
|
596
|
+
});
|
|
577
597
|
});
|
|
578
598
|
|
|
579
599
|
test("aligns TUI line numbers across files to the widest result", () => {
|
|
580
|
-
const tool = makeGrepOverrideWithBackend(process.cwd(), fakeBackend().backend);
|
|
600
|
+
const tool = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, fakeBackend().backend);
|
|
581
601
|
const raw = [
|
|
582
602
|
"a.ts · 2 matches",
|
|
583
603
|
"99#ABCD│ alpha",
|
|
@@ -602,29 +622,27 @@ test("aligns TUI line numbers across files to the widest result", () => {
|
|
|
602
622
|
});
|
|
603
623
|
|
|
604
624
|
test("limit counts matched lines and stops the fake runner", async () => {
|
|
605
|
-
await withDir(async (dir) =>
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
});
|
|
625
|
+
await withDir(async (dir) => {
|
|
626
|
+
const file = join(dir, "a.ts");
|
|
627
|
+
await writeFile(file, "alpha beta\nalpha only\nalpha later\n");
|
|
628
|
+
const fake = fakeBackend({
|
|
629
|
+
lines: [
|
|
630
|
+
rgMatch(file, 1, "alpha beta\n"),
|
|
631
|
+
rgMatch(file, 2, "alpha only\n"),
|
|
632
|
+
rgMatch(file, 3, "alpha later\n"),
|
|
633
|
+
],
|
|
634
|
+
});
|
|
616
635
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
);
|
|
636
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
637
|
+
pattern: "alpha",
|
|
638
|
+
limit: 1,
|
|
639
|
+
});
|
|
640
|
+
assert.match(text(result), /1#[0-9A-Z]+│alpha beta/);
|
|
641
|
+
assert.match(
|
|
642
|
+
text(result),
|
|
643
|
+
/\[1 matches limit reached\. Use limit=2 for more, or refine pattern\]/,
|
|
644
|
+
);
|
|
645
|
+
});
|
|
628
646
|
});
|
|
629
647
|
|
|
630
648
|
test("auto-detects literal or regex mode and keeps explicit overrides", async () => {
|
|
@@ -633,8 +651,8 @@ test("auto-detects literal or regex mode and keeps explicit overrides", async ()
|
|
|
633
651
|
const invalid = fakeBackend({
|
|
634
652
|
validation: { code: 2, stderr: "regex parse error:\nerror: unclosed group" },
|
|
635
653
|
});
|
|
636
|
-
const tool = makeGrepOverrideWithBackend(dir, valid.backend);
|
|
637
|
-
const fallback = makeGrepOverrideWithBackend(dir, invalid.backend);
|
|
654
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, valid.backend);
|
|
655
|
+
const fallback = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, invalid.backend);
|
|
638
656
|
|
|
639
657
|
const result = await call(fallback, { pattern: "queueTool(" });
|
|
640
658
|
assert.match(text(result), /Invalid regex; searched the pattern as literal text/);
|
|
@@ -647,31 +665,29 @@ test("auto-detects literal or regex mode and keeps explicit overrides", async ()
|
|
|
647
665
|
await call(tool, { pattern: "value.*", literal: true });
|
|
648
666
|
|
|
649
667
|
assert.deepEqual(
|
|
650
|
-
[...invalid.calls, ...valid.calls]
|
|
651
|
-
.
|
|
652
|
-
|
|
668
|
+
[...invalid.calls, ...valid.calls].map(({ request }) =>
|
|
669
|
+
request.matcher.includes("--fixed-strings"),
|
|
670
|
+
),
|
|
653
671
|
[true, false, false, true],
|
|
654
672
|
);
|
|
655
|
-
assert.
|
|
656
|
-
|
|
657
|
-
"
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
"
|
|
661
|
-
"
|
|
662
|
-
"--quiet",
|
|
663
|
-
"-e",
|
|
664
|
-
"queueTool(",
|
|
665
|
-
"--",
|
|
666
|
-
"-",
|
|
673
|
+
assert.deepEqual(valid.probes, [
|
|
674
|
+
[rgPath, ["value.*"], false, undefined],
|
|
675
|
+
[rgPath, ["plain"], false, undefined],
|
|
676
|
+
]);
|
|
677
|
+
assert.deepEqual(invalid.probes, [
|
|
678
|
+
[rgPath, ["queueTool("], false, undefined],
|
|
679
|
+
[rgPath, ["plain", "broken("], false, undefined],
|
|
667
680
|
]);
|
|
668
681
|
|
|
669
682
|
const failed = fakeBackend({ validation: { code: 2, stderr: "Permission denied" } });
|
|
670
683
|
await assert.rejects(
|
|
671
|
-
call(makeGrepOverrideWithBackend(dir, failed.backend), {
|
|
684
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, failed.backend), {
|
|
685
|
+
pattern: "value.*",
|
|
686
|
+
}),
|
|
672
687
|
/Permission denied/,
|
|
673
688
|
);
|
|
674
|
-
assert.equal(failed.
|
|
689
|
+
assert.equal(failed.probes.length, 1);
|
|
690
|
+
assert.equal(failed.calls.length, 0);
|
|
675
691
|
});
|
|
676
692
|
});
|
|
677
693
|
|
|
@@ -681,26 +697,33 @@ test("uses smart-case across the entire OR pattern array", async () => {
|
|
|
681
697
|
await writeFile(target, "FOO alpha\n");
|
|
682
698
|
|
|
683
699
|
const lower = fakeBackend({ lines: [rgMatch(target, 1, "FOO alpha\n")] });
|
|
684
|
-
const lowerResult = await call(
|
|
685
|
-
|
|
686
|
-
|
|
700
|
+
const lowerResult = await call(
|
|
701
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, lower.backend),
|
|
702
|
+
{
|
|
703
|
+
pattern: ["foo", "alpha"],
|
|
704
|
+
},
|
|
705
|
+
);
|
|
687
706
|
assert.match(text(lowerResult), /FOO alpha/);
|
|
688
707
|
|
|
689
708
|
const mixed = fakeBackend();
|
|
690
|
-
const mixedResult = await call(
|
|
691
|
-
|
|
692
|
-
|
|
709
|
+
const mixedResult = await call(
|
|
710
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, mixed.backend),
|
|
711
|
+
{
|
|
712
|
+
pattern: ["Foo", "alpha"],
|
|
713
|
+
},
|
|
714
|
+
);
|
|
693
715
|
assert.equal(text(mixedResult), "No matches found");
|
|
694
716
|
|
|
695
717
|
const flags = fakeBackend();
|
|
696
|
-
const tool = makeGrepOverrideWithBackend(dir, flags.backend);
|
|
718
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, flags.backend);
|
|
697
719
|
await call(tool, { pattern: "lower" });
|
|
698
720
|
await call(tool, { pattern: "Upper" });
|
|
699
721
|
await call(tool, { pattern: "foo\\S*" });
|
|
700
722
|
assert.deepEqual(
|
|
701
|
-
flags.calls
|
|
702
|
-
.
|
|
703
|
-
.
|
|
723
|
+
flags.calls.map(({ request }) => [
|
|
724
|
+
request.matcher.includes("--ignore-case"),
|
|
725
|
+
request.matcher.includes("--case-sensitive"),
|
|
726
|
+
]),
|
|
704
727
|
[
|
|
705
728
|
[true, false],
|
|
706
729
|
[false, true],
|
|
@@ -713,11 +736,13 @@ test("uses smart-case across the entire OR pattern array", async () => {
|
|
|
713
736
|
test("OR pattern arrays are not limited by the former AND filter", async () => {
|
|
714
737
|
await withDir(async (dir) => {
|
|
715
738
|
const fake = fakeBackend();
|
|
716
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
739
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
717
740
|
const patterns = Array.from({ length: 17 }, (_, index) => `pattern${index}`);
|
|
718
741
|
assert.equal(text(await call(tool, { pattern: patterns })), "No matches found");
|
|
719
742
|
assert.deepEqual(
|
|
720
|
-
fake.calls[0].
|
|
743
|
+
fake.calls[0].request.matcher.flatMap((arg, index, args) =>
|
|
744
|
+
args[index - 1] === "-e" ? [arg] : [],
|
|
745
|
+
),
|
|
721
746
|
patterns,
|
|
722
747
|
);
|
|
723
748
|
});
|
|
@@ -726,7 +751,7 @@ test("OR pattern arrays are not limited by the former AND filter", async () => {
|
|
|
726
751
|
test("rejects empty patterns while allowing wildcard, literal, and empty-line searches", async () => {
|
|
727
752
|
await withDir(async (dir) => {
|
|
728
753
|
const fake = fakeBackend();
|
|
729
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
754
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
730
755
|
|
|
731
756
|
for (const pattern of ["", [], ["valid", ""]]) {
|
|
732
757
|
await assert.rejects(call(tool, { pattern }), /Validation failed for tool "grep"/);
|
|
@@ -738,15 +763,15 @@ test("rejects empty patterns while allowing wildcard, literal, and empty-line se
|
|
|
738
763
|
|
|
739
764
|
assert.equal(text(await call(tool, { pattern: ".*", literal: true })), "No matches found");
|
|
740
765
|
assert.equal(text(await call(tool, { pattern: "^$" })), "No matches found");
|
|
741
|
-
assert.equal(fake.calls.
|
|
742
|
-
assert.equal(fake.
|
|
766
|
+
assert.equal(fake.calls.length, 5);
|
|
767
|
+
assert.equal(fake.probes.length, 4);
|
|
743
768
|
});
|
|
744
769
|
});
|
|
745
770
|
|
|
746
771
|
test("grep accepts nonempty whitespace literals", async () => {
|
|
747
772
|
await withDir(async (dir) => {
|
|
748
773
|
const fake = fakeBackend();
|
|
749
|
-
const tool = makeGrepOverrideWithBackend(dir, fake.backend);
|
|
774
|
+
const tool = makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend);
|
|
750
775
|
for (const pattern of [" ", "\t", ["word", " "]]) {
|
|
751
776
|
assert.equal(text(await call(tool, { pattern, literal: true })), "No matches found");
|
|
752
777
|
}
|
|
@@ -755,27 +780,32 @@ test("grep accepts nonempty whitespace literals", async () => {
|
|
|
755
780
|
});
|
|
756
781
|
|
|
757
782
|
test("reports empty output and ripgrep execution failures", async () => {
|
|
758
|
-
await withDir(async (dir) =>
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
783
|
+
await withDir(async (dir) => {
|
|
784
|
+
const empty = fakeBackend({ code: 1 });
|
|
785
|
+
assert.equal(
|
|
786
|
+
text(
|
|
787
|
+
await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, empty.backend), {
|
|
788
|
+
pattern: "missing",
|
|
789
|
+
}),
|
|
790
|
+
),
|
|
791
|
+
"No matches found",
|
|
792
|
+
);
|
|
765
793
|
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
794
|
+
const failed = fakeBackend({ code: 2, stderr: "bad regex" });
|
|
795
|
+
await assert.rejects(
|
|
796
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, failed.backend), {
|
|
797
|
+
pattern: "[",
|
|
798
|
+
literal: false,
|
|
799
|
+
}),
|
|
800
|
+
/bad regex/,
|
|
801
|
+
);
|
|
771
802
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
);
|
|
803
|
+
const rejected = fakeBackend({ error: new Error("spawn failed") });
|
|
804
|
+
await assert.rejects(
|
|
805
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, rejected.backend), { pattern: "x" }),
|
|
806
|
+
/spawn failed/,
|
|
807
|
+
);
|
|
808
|
+
});
|
|
779
809
|
});
|
|
780
810
|
|
|
781
811
|
test("rejects calls aborted before or during rg execution", async () => {
|
|
@@ -785,7 +815,7 @@ test("rejects calls aborted before or during rg execution", async () => {
|
|
|
785
815
|
first.abort();
|
|
786
816
|
await assert.rejects(
|
|
787
817
|
call(
|
|
788
|
-
makeGrepOverrideWithBackend(dir, alreadyAborted.backend),
|
|
818
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, alreadyAborted.backend),
|
|
789
819
|
{ pattern: ["x", "y"] },
|
|
790
820
|
first.signal,
|
|
791
821
|
),
|
|
@@ -796,7 +826,7 @@ test("rejects calls aborted before or during rg execution", async () => {
|
|
|
796
826
|
const interrupted = fakeBackend({ onRun: () => controller.abort() });
|
|
797
827
|
await assert.rejects(
|
|
798
828
|
call(
|
|
799
|
-
makeGrepOverrideWithBackend(dir, interrupted.backend),
|
|
829
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, interrupted.backend),
|
|
800
830
|
{ pattern: ["x", "y"] },
|
|
801
831
|
controller.signal,
|
|
802
832
|
),
|
|
@@ -812,7 +842,7 @@ test("OR patterns count each matched line once", async () => {
|
|
|
812
842
|
const fake = fakeBackend({
|
|
813
843
|
lines: Array.from({ length: 4097 }, (_, index) => rgMatch(file, index + 1, "foo bar\n")),
|
|
814
844
|
});
|
|
815
|
-
const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
|
|
845
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
816
846
|
pattern: ["foo", "bar"],
|
|
817
847
|
literal: true,
|
|
818
848
|
outputMode: "count",
|
|
@@ -820,7 +850,9 @@ test("OR patterns count each matched line once", async () => {
|
|
|
820
850
|
});
|
|
821
851
|
assert.match(text(result), /fixture\.ts: 4097/);
|
|
822
852
|
assert.deepEqual(
|
|
823
|
-
fake.calls[0].
|
|
853
|
+
fake.calls[0].request.matcher.flatMap((arg, index, args) =>
|
|
854
|
+
args[index - 1] === "-e" ? [arg] : [],
|
|
855
|
+
),
|
|
824
856
|
["foo", "bar"],
|
|
825
857
|
);
|
|
826
858
|
});
|
|
@@ -834,7 +866,7 @@ test("long candidate lines remain countable without content previews", async ()
|
|
|
834
866
|
const fake = fakeBackend({
|
|
835
867
|
lines: Array.from({ length: 3 }, (_, index) => rgMatch(file, index + 1, line)),
|
|
836
868
|
});
|
|
837
|
-
const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
|
|
869
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
838
870
|
pattern: "foo",
|
|
839
871
|
literal: true,
|
|
840
872
|
outputMode: "count",
|
|
@@ -845,28 +877,29 @@ test("long candidate lines remain countable without content previews", async ()
|
|
|
845
877
|
});
|
|
846
878
|
|
|
847
879
|
test("grep rejects malformed UTF-8 and NUL bytes instead of hashing binary text", async () => {
|
|
848
|
-
await withDir(async (dir) =>
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
)
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
880
|
+
await withDir(async (dir) => {
|
|
881
|
+
const cases = [
|
|
882
|
+
{
|
|
883
|
+
name: "invalid.txt",
|
|
884
|
+
bytes: Buffer.from([0x61, 0x0a, 0xc3, 0x28, 0x0a]),
|
|
885
|
+
error: /UNSUPPORTED_ENCODING/,
|
|
886
|
+
},
|
|
887
|
+
{ name: "nul.txt", bytes: Buffer.from([0x61, 0x00, 0x62]), error: /UNSUPPORTED_TEXT/ },
|
|
888
|
+
];
|
|
889
|
+
for (const fixture of cases) {
|
|
890
|
+
const file = join(dir, fixture.name);
|
|
891
|
+
await writeFile(file, fixture.bytes);
|
|
892
|
+
const fake = fakeBackend({ lines: [rgMatch(file, 1, "a\n")], paths: [file] });
|
|
893
|
+
await assert.rejects(
|
|
894
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
895
|
+
pattern: "a",
|
|
896
|
+
path: file,
|
|
897
|
+
}),
|
|
898
|
+
fixture.error,
|
|
899
|
+
);
|
|
900
|
+
assert.deepEqual(await readFile(file), fixture.bytes);
|
|
901
|
+
}
|
|
902
|
+
});
|
|
870
903
|
});
|
|
871
904
|
|
|
872
905
|
test("partial searches retain matches and surface stderr across output modes", async () => {
|
|
@@ -880,7 +913,7 @@ test("partial searches retain matches and surface stderr across output modes", a
|
|
|
880
913
|
code: 2,
|
|
881
914
|
stderr: "unreadable.txt: Permission denied",
|
|
882
915
|
});
|
|
883
|
-
const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
|
|
916
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
884
917
|
pattern: "needle",
|
|
885
918
|
outputMode,
|
|
886
919
|
limit,
|
|
@@ -896,7 +929,7 @@ test("partial searches retain matches and surface stderr across output modes", a
|
|
|
896
929
|
}
|
|
897
930
|
const fake = fakeBackend({ code: 2, stderr: "Permission denied" });
|
|
898
931
|
await assert.rejects(
|
|
899
|
-
call(makeGrepOverrideWithBackend(dir, fake.backend), { pattern: "needle" }),
|
|
932
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), { pattern: "needle" }),
|
|
900
933
|
/No matches confirmed.*Search incomplete/s,
|
|
901
934
|
);
|
|
902
935
|
});
|
|
@@ -913,12 +946,14 @@ test("first-match revision read errors retain results from other files", async (
|
|
|
913
946
|
});
|
|
914
947
|
const backend: GrepBackend = {
|
|
915
948
|
...fake.backend,
|
|
916
|
-
async
|
|
949
|
+
async search(...args) {
|
|
917
950
|
await rm(gone);
|
|
918
|
-
return fake.backend.
|
|
951
|
+
return fake.backend.search(...args);
|
|
919
952
|
},
|
|
920
953
|
};
|
|
921
|
-
const result = await call(makeGrepOverrideWithBackend(dir, backend), {
|
|
954
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, backend), {
|
|
955
|
+
pattern: "needle",
|
|
956
|
+
});
|
|
922
957
|
assert.match(text(result), /good\.txt · 1 match/);
|
|
923
958
|
assert.doesNotMatch(text(result), /gone\.txt · 1 match/);
|
|
924
959
|
assert.match(text(result), /Search incomplete/);
|
|
@@ -936,10 +971,10 @@ test("failed result reads report incomplete coverage while retaining readable fi
|
|
|
936
971
|
lines: [rgMatch(gone, 1, "needle\n"), rgMatch(good, 1, "needle\n")],
|
|
937
972
|
});
|
|
938
973
|
const result = await call(
|
|
939
|
-
makeGrepOverrideWithBackend(dir, {
|
|
974
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, {
|
|
940
975
|
...fake.backend,
|
|
941
|
-
|
|
942
|
-
const result = await fake.backend.
|
|
976
|
+
search: async (...args) => {
|
|
977
|
+
const result = await fake.backend.search(...args);
|
|
943
978
|
await rm(gone);
|
|
944
979
|
return result;
|
|
945
980
|
},
|
|
@@ -968,7 +1003,7 @@ test("explicit-file glob listing warnings retain confirmed matches", async () =>
|
|
|
968
1003
|
});
|
|
969
1004
|
const fake = fakeBackend({ paths: [file], lines: [event] });
|
|
970
1005
|
const listing = await call(
|
|
971
|
-
makeGrepOverrideWithBackend(dir, {
|
|
1006
|
+
makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, {
|
|
972
1007
|
...fake.backend,
|
|
973
1008
|
runRgPaths: async (...args) => {
|
|
974
1009
|
const result = await fake.backend.runRgPaths(...args);
|
|
@@ -993,7 +1028,9 @@ test("concurrent file reads preserve discovery order across multiple matched fil
|
|
|
993
1028
|
matches.push(rgMatch(file, 2, `match_${i}\n`));
|
|
994
1029
|
}
|
|
995
1030
|
const fake = fakeBackend({ lines: matches, paths: files });
|
|
996
|
-
const result = await call(makeGrepOverrideWithBackend(dir, fake.backend), {
|
|
1031
|
+
const result = await call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), {
|
|
1032
|
+
pattern: "match",
|
|
1033
|
+
});
|
|
997
1034
|
const content = text(result);
|
|
998
1035
|
// Verify each file block appears in exact discovery order
|
|
999
1036
|
let lastIndex = -1;
|
|
@@ -1029,7 +1066,7 @@ test("fatal format errors stop workers from starting new file reads after reject
|
|
|
1029
1066
|
try {
|
|
1030
1067
|
const fake = fakeBackend({ lines: events });
|
|
1031
1068
|
await assert.rejects(
|
|
1032
|
-
call(makeGrepOverrideWithBackend(dir, fake.backend), { pattern: "needle" }),
|
|
1069
|
+
call(makeGrepOverrideWithBackend(dir, DEFAULT_CONFIG, fake.backend), { pattern: "needle" }),
|
|
1033
1070
|
/File changed during search/,
|
|
1034
1071
|
);
|
|
1035
1072
|
const atRejection = started;
|