@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -1,1659 +0,0 @@
|
|
|
1
|
-
import { makeMockGrep } from "../pi/grep-rg.testing.ts";
|
|
2
|
-
/**
|
|
3
|
-
* Explicit integration coverage for the platform rg bundled by @vscode/ripgrep.
|
|
4
|
-
* This suite is excluded from the default test script.
|
|
5
|
-
*/
|
|
6
|
-
import { test } from "node:test";
|
|
7
|
-
import assert from "node:assert/strict";
|
|
8
|
-
import childProcess from "node:child_process";
|
|
9
|
-
import { syncBuiltinESMExports } from "node:module";
|
|
10
|
-
import {
|
|
11
|
-
lstat,
|
|
12
|
-
mkdir,
|
|
13
|
-
mkdtemp,
|
|
14
|
-
readFile,
|
|
15
|
-
readlink,
|
|
16
|
-
rm,
|
|
17
|
-
stat,
|
|
18
|
-
symlink,
|
|
19
|
-
writeFile,
|
|
20
|
-
} from "node:fs/promises";
|
|
21
|
-
import { tmpdir } from "node:os";
|
|
22
|
-
import { dirname, join, parse } from "node:path";
|
|
23
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
24
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
25
|
-
|
|
26
|
-
const { makeEditOverride } = await import("../pi/tool-factories.testing.ts");
|
|
27
|
-
const { makeReadOverride } = await import("../pi/tool-factories.testing.ts");
|
|
28
|
-
const { makeWriteOverride } = await import("../pi/tool-factories.testing.ts");
|
|
29
|
-
const { makeReplaceTool } = await import("../pi/tool-factories.testing.ts");
|
|
30
|
-
import { COMMON_RG_ARGS, runRgPaths } from "../pi/rg-process.ts";
|
|
31
|
-
const { DEFAULT_OUTPUT_BUDGETS } = await import("../pi/tool-factories.testing.ts");
|
|
32
|
-
const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
|
|
33
|
-
import { computeLineHash } from "../core/hash.ts";
|
|
34
|
-
import { callTool } from "../pi/tool-call.testing.ts";
|
|
35
|
-
const { DEFAULT_CONFIG, makeGrepOverride } = await import("../pi/tool-factories.testing.ts");
|
|
36
|
-
import { canonicalPath } from "../pi/path.ts";
|
|
37
|
-
import { reportOf } from "../pi/report.ts";
|
|
38
|
-
const { grepCounts } = await import("../pi/grep-output.testing.ts");
|
|
39
|
-
import { hostCompiles, unsupportedByHost } from "../testing/regex-host.testing.ts";
|
|
40
|
-
|
|
41
|
-
const invoke = (
|
|
42
|
-
tool: any,
|
|
43
|
-
toolCallId: string,
|
|
44
|
-
args: unknown,
|
|
45
|
-
signal?: AbortSignal,
|
|
46
|
-
onUpdate?: unknown,
|
|
47
|
-
ctx?: unknown,
|
|
48
|
-
) => callTool(tool, args, { toolCallId, signal, onUpdate, ctx });
|
|
49
|
-
|
|
50
|
-
test("real rg emits anchored matches from a temporary directory", async () => {
|
|
51
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-integration-"));
|
|
52
|
-
try {
|
|
53
|
-
const file = join(directory, "fixture.ts");
|
|
54
|
-
await writeFile(file, "needle\nother\n");
|
|
55
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
56
|
-
|
|
57
|
-
for (const pattern of ["needle", ["needle"]]) {
|
|
58
|
-
const result: any = await invoke(tool, "0", { pattern, literal: true }, undefined, undefined);
|
|
59
|
-
assert.match(result.content[0].text, /fixture\.ts · 1 match/);
|
|
60
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
|
|
61
|
-
}
|
|
62
|
-
} finally {
|
|
63
|
-
await rm(directory, { recursive: true, force: true });
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test("real rg finds whitespace-only literal queries and OR alternatives", async () => {
|
|
68
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-whitespace-"));
|
|
69
|
-
try {
|
|
70
|
-
const file = join(directory, "fixture.txt");
|
|
71
|
-
await writeFile(file, "plain word\nfirst\tsecond\nboth \tforms\n");
|
|
72
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
73
|
-
for (const { pattern, expected } of [
|
|
74
|
-
{ pattern: " ", expected: 2 },
|
|
75
|
-
{ pattern: "\t", expected: 2 },
|
|
76
|
-
{ pattern: [" ", "\t"], expected: 3 },
|
|
77
|
-
]) {
|
|
78
|
-
const result = await invoke(
|
|
79
|
-
tool,
|
|
80
|
-
"0",
|
|
81
|
-
{ pattern, path: file, literal: true, outputMode: "count" },
|
|
82
|
-
undefined,
|
|
83
|
-
undefined,
|
|
84
|
-
);
|
|
85
|
-
assert.equal(grepCounts(result).matches, expected);
|
|
86
|
-
assert.equal(grepCounts(result).files, 1);
|
|
87
|
-
}
|
|
88
|
-
const lineBreaks = await invoke(
|
|
89
|
-
tool,
|
|
90
|
-
"0",
|
|
91
|
-
{ pattern: "\n", path: file, literal: true, multiline: true, outputMode: "count" },
|
|
92
|
-
undefined,
|
|
93
|
-
undefined,
|
|
94
|
-
);
|
|
95
|
-
assert.equal(grepCounts(lineBreaks).matches, 3);
|
|
96
|
-
assert.equal(grepCounts(lineBreaks).files, 1);
|
|
97
|
-
} finally {
|
|
98
|
-
await rm(directory, { recursive: true, force: true });
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("real rg uses smart-case across OR patterns and Unicode matches", async () => {
|
|
103
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-"));
|
|
104
|
-
try {
|
|
105
|
-
await writeFile(join(directory, "fixture.ts"), "FOO abc\nfoo BAR\nfoo bar\nK zip\nk zip\n");
|
|
106
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
107
|
-
const lower = await invoke(
|
|
108
|
-
tool,
|
|
109
|
-
"0",
|
|
110
|
-
{ literal: true, pattern: ["foo", "bar"] },
|
|
111
|
-
undefined,
|
|
112
|
-
undefined,
|
|
113
|
-
);
|
|
114
|
-
assert.match(lower.content[0].text, /fixture\.ts · 3 matches/);
|
|
115
|
-
assert.match(lower.content[0].text, /│FOO abc/);
|
|
116
|
-
const mixed = await invoke(
|
|
117
|
-
tool,
|
|
118
|
-
"0",
|
|
119
|
-
{ literal: true, pattern: ["foo", "BAR"] },
|
|
120
|
-
undefined,
|
|
121
|
-
undefined,
|
|
122
|
-
);
|
|
123
|
-
assert.match(mixed.content[0].text, /fixture\.ts · 2 matches/);
|
|
124
|
-
assert.doesNotMatch(mixed.content[0].text, /│FOO abc/);
|
|
125
|
-
const unicode = await invoke(tool, "0", { literal: true, pattern: "k" }, undefined, undefined);
|
|
126
|
-
assert.match(unicode.content[0].text, /fixture\.ts · 1 match/);
|
|
127
|
-
assert.match(unicode.content[0].text, /│k zip/);
|
|
128
|
-
assert.doesNotMatch(unicode.content[0].text, /│K zip/);
|
|
129
|
-
} finally {
|
|
130
|
-
await rm(directory, { recursive: true, force: true });
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test("explicit ignoreCase overrides smart-case for regex and literal queries", async () => {
|
|
135
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-override-"));
|
|
136
|
-
try {
|
|
137
|
-
const file = join(directory, "fixture.txt");
|
|
138
|
-
await writeFile(file, "FOO\nfoo\n");
|
|
139
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
140
|
-
const sensitive = await invoke(
|
|
141
|
-
tool,
|
|
142
|
-
"0",
|
|
143
|
-
{ literal: true, pattern: "foo", path: file, ignoreCase: false },
|
|
144
|
-
undefined,
|
|
145
|
-
undefined,
|
|
146
|
-
);
|
|
147
|
-
assert.match(sensitive.content[0].text, /fixture\.txt · 1 match/);
|
|
148
|
-
assert.match(sensitive.content[0].text, /2#[0-9A-Z]+│foo/);
|
|
149
|
-
const insensitive = await invoke(
|
|
150
|
-
tool,
|
|
151
|
-
"0",
|
|
152
|
-
{ pattern: "FOO", path: file, ignoreCase: true, literal: true },
|
|
153
|
-
undefined,
|
|
154
|
-
undefined,
|
|
155
|
-
);
|
|
156
|
-
assert.match(insensitive.content[0].text, /fixture\.txt · 2 matches/);
|
|
157
|
-
const regex = await invoke(
|
|
158
|
-
tool,
|
|
159
|
-
"0",
|
|
160
|
-
{ pattern: "^FOO$", path: file, ignoreCase: true, literal: false },
|
|
161
|
-
undefined,
|
|
162
|
-
undefined,
|
|
163
|
-
);
|
|
164
|
-
assert.match(regex.content[0].text, /fixture\.txt · 2 matches/);
|
|
165
|
-
if (!hostCompiles("(?i:a)")) return;
|
|
166
|
-
const inlineSensitive = await invoke(
|
|
167
|
-
tool,
|
|
168
|
-
"0",
|
|
169
|
-
{ pattern: "(?-i:^FOO$)", path: file, ignoreCase: true, literal: false },
|
|
170
|
-
undefined,
|
|
171
|
-
undefined,
|
|
172
|
-
);
|
|
173
|
-
assert.match(inlineSensitive.content[0].text, /fixture\.txt · 1 match/);
|
|
174
|
-
const inlineInsensitive = await invoke(
|
|
175
|
-
tool,
|
|
176
|
-
"0",
|
|
177
|
-
{ pattern: "(?i:^FOO$)", path: file, ignoreCase: false, literal: false },
|
|
178
|
-
undefined,
|
|
179
|
-
undefined,
|
|
180
|
-
);
|
|
181
|
-
assert.match(inlineInsensitive.content[0].text, /fixture\.txt · 2 matches/);
|
|
182
|
-
} finally {
|
|
183
|
-
await rm(directory, { recursive: true, force: true });
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
test("owned rg processes ignore RIPGREP_CONFIG_PATH", async () => {
|
|
188
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-config-"));
|
|
189
|
-
const previous = process.env.RIPGREP_CONFIG_PATH;
|
|
190
|
-
try {
|
|
191
|
-
const config = join(directory, "ripgreprc");
|
|
192
|
-
await writeFile(
|
|
193
|
-
config,
|
|
194
|
-
"--ignore-case\n--fixed-strings\n--invert-match\n--pcre2\n--multiline\n",
|
|
195
|
-
);
|
|
196
|
-
await writeFile(join(directory, "fixture.ts"), "FOO\nfoo\nbar\n");
|
|
197
|
-
process.env.RIPGREP_CONFIG_PATH = config;
|
|
198
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
199
|
-
const result: any = await invoke(
|
|
200
|
-
tool,
|
|
201
|
-
"0",
|
|
202
|
-
{ literal: false, pattern: ["^foo$", "foo"] },
|
|
203
|
-
undefined,
|
|
204
|
-
undefined,
|
|
205
|
-
);
|
|
206
|
-
assert.match(result.content[0].text, /fixture\.ts · 2 matches/);
|
|
207
|
-
assert.match(result.content[0].text, /│foo/);
|
|
208
|
-
assert.match(result.content[0].text, /│FOO/);
|
|
209
|
-
assert.doesNotMatch(result.content[0].text, /│bar/);
|
|
210
|
-
const lineOnly = await invoke(tool, "0", { pattern: "foo\\nbar", literal: false });
|
|
211
|
-
assert.equal(lineOnly.isError, undefined);
|
|
212
|
-
assert.equal(grepCounts(lineOnly).matches, 0);
|
|
213
|
-
const multiline = await invoke(
|
|
214
|
-
tool,
|
|
215
|
-
"0",
|
|
216
|
-
{ pattern: "foo\\nbar", literal: false, multiline: true },
|
|
217
|
-
undefined,
|
|
218
|
-
undefined,
|
|
219
|
-
);
|
|
220
|
-
assert.match(multiline.content[0].text, /fixture\.ts · 2 matches/);
|
|
221
|
-
} finally {
|
|
222
|
-
if (previous === undefined) delete process.env.RIPGREP_CONFIG_PATH;
|
|
223
|
-
else process.env.RIPGREP_CONFIG_PATH = previous;
|
|
224
|
-
await rm(directory, { recursive: true, force: true });
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
test("real rg separates exact text from regex", async () => {
|
|
229
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-regex-"));
|
|
230
|
-
try {
|
|
231
|
-
await writeFile(
|
|
232
|
-
join(directory, "fixture.ts"),
|
|
233
|
-
"FOO\nfoo\nfoo0\nfoo(0)\nqueueTool(\nfoo(?=bar)\n",
|
|
234
|
-
);
|
|
235
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
236
|
-
for (const pattern of ["(?i:^foo$)", "(?<name>foo)$"].filter((source) =>
|
|
237
|
-
hostCompiles(source),
|
|
238
|
-
)) {
|
|
239
|
-
const result = await invoke(tool, "regex", { pattern, literal: false });
|
|
240
|
-
assert.match(result.content[0].text, /│foo/);
|
|
241
|
-
}
|
|
242
|
-
const regex = await invoke(tool, "regex-punctuation", { pattern: "foo(0)", literal: false });
|
|
243
|
-
assert.match(regex.content[0].text, /│foo0/);
|
|
244
|
-
assert.doesNotMatch(regex.content[0].text, /│foo\(0\)/);
|
|
245
|
-
for (const pattern of ["foo(0)", "queueTool(", "foo(?=bar)"]) {
|
|
246
|
-
const result = await invoke(tool, "literal", { pattern, literal: true });
|
|
247
|
-
assert.ok(result.content[0].text.includes(`│${pattern}`));
|
|
248
|
-
}
|
|
249
|
-
const missing = await invoke(tool, "no-match", { pattern: "missing(", literal: true });
|
|
250
|
-
assert.equal(grepCounts(missing).matches, 0);
|
|
251
|
-
assert.equal(missing.isError, undefined);
|
|
252
|
-
} finally {
|
|
253
|
-
await rm(directory, { recursive: true, force: true });
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
test("real rg keeps CRLF line-end anchors with regex OR matching", async () => {
|
|
258
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-crlf-"));
|
|
259
|
-
try {
|
|
260
|
-
await writeFile(join(directory, "fixture.ts"), "alpha beta drop\r\nalpha beta\r\nalpha only");
|
|
261
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
262
|
-
const ending = await invoke(
|
|
263
|
-
tool,
|
|
264
|
-
"0",
|
|
265
|
-
{ pattern: "beta$", literal: false },
|
|
266
|
-
undefined,
|
|
267
|
-
undefined,
|
|
268
|
-
);
|
|
269
|
-
assert.match(ending.content[0].text, /fixture\.ts · 1 match/);
|
|
270
|
-
assert.match(ending.content[0].text, /2#[0-9A-Z]+│alpha beta$/);
|
|
271
|
-
const alternatives = await invoke(
|
|
272
|
-
tool,
|
|
273
|
-
"0",
|
|
274
|
-
{ pattern: ["beta$", "alpha only"], literal: false },
|
|
275
|
-
undefined,
|
|
276
|
-
undefined,
|
|
277
|
-
);
|
|
278
|
-
assert.match(alternatives.content[0].text, /fixture\.ts · 2 matches/);
|
|
279
|
-
assert.match(alternatives.content[0].text, /3#[0-9A-Z]+│alpha only$/);
|
|
280
|
-
} finally {
|
|
281
|
-
await rm(directory, { recursive: true, force: true });
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
test("real rg finds a late OR match and respects the line limit", async () => {
|
|
286
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-limit-"));
|
|
287
|
-
try {
|
|
288
|
-
await writeFile(
|
|
289
|
-
join(directory, "fixture.ts"),
|
|
290
|
-
"foo skip\n".repeat(4096) + "foo keep\nfoo keep later\n",
|
|
291
|
-
);
|
|
292
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
293
|
-
const result = await invoke(
|
|
294
|
-
tool,
|
|
295
|
-
"0",
|
|
296
|
-
{ pattern: ["foo keep", "missing"], literal: true, limit: 1 },
|
|
297
|
-
undefined,
|
|
298
|
-
undefined,
|
|
299
|
-
);
|
|
300
|
-
assert.match(result.content[0].text, /4097#[0-9A-Z]+│foo keep$/m);
|
|
301
|
-
assert.doesNotMatch(result.content[0].text, /foo keep later|4098#/);
|
|
302
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
303
|
-
assert.equal(reportOf(result, "grep")?.matchLimit, 1);
|
|
304
|
-
} finally {
|
|
305
|
-
await rm(directory, { recursive: true, force: true });
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
test("shared rg runner stops and cleans up after limits, callback failures, and cancellation", async (t) => {
|
|
310
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-process-"));
|
|
311
|
-
const spawn = childProcess.spawn;
|
|
312
|
-
const children: ReturnType<typeof spawn>[] = [];
|
|
313
|
-
const closed = new Set<ReturnType<typeof spawn>>();
|
|
314
|
-
let missingExecutable = false;
|
|
315
|
-
try {
|
|
316
|
-
t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) => {
|
|
317
|
-
const child = spawn(
|
|
318
|
-
missingExecutable ? join(directory, "missing-rg") : process.execPath,
|
|
319
|
-
args[1],
|
|
320
|
-
args[2],
|
|
321
|
-
);
|
|
322
|
-
children.push(child);
|
|
323
|
-
child.once("close", () => closed.add(child));
|
|
324
|
-
return child;
|
|
325
|
-
});
|
|
326
|
-
syncBuiltinESMExports();
|
|
327
|
-
const fixture = fileURLToPath(
|
|
328
|
-
new URL("../testing/rg-line-listing.fixture.mjs", import.meta.url),
|
|
329
|
-
);
|
|
330
|
-
const processTimeoutMs = 5_000;
|
|
331
|
-
const args = [
|
|
332
|
-
fixture,
|
|
333
|
-
JSON.stringify([Buffer.from("first\0second\0").toString("base64")]),
|
|
334
|
-
"wait",
|
|
335
|
-
];
|
|
336
|
-
for (const outcome of ["limit", "callback", "cancel"] as const) {
|
|
337
|
-
const controller = new AbortController();
|
|
338
|
-
const consumed: string[] = [];
|
|
339
|
-
let aliveAtCallback = false;
|
|
340
|
-
const run = runRgPaths(
|
|
341
|
-
args,
|
|
342
|
-
AbortSignal.any([controller.signal, AbortSignal.timeout(processTimeoutMs)]),
|
|
343
|
-
(path) => {
|
|
344
|
-
consumed.push(path);
|
|
345
|
-
const child = children.at(-1);
|
|
346
|
-
aliveAtCallback = child?.exitCode === null && child.signalCode === null;
|
|
347
|
-
if (outcome === "callback") throw new Error("callback failed");
|
|
348
|
-
if (outcome === "cancel") controller.abort();
|
|
349
|
-
return outcome !== "limit";
|
|
350
|
-
},
|
|
351
|
-
);
|
|
352
|
-
if (outcome === "limit") assert.equal((await run).stopped, true);
|
|
353
|
-
else
|
|
354
|
-
await assert.rejects(run, outcome === "callback" ? /callback failed/ : /Operation aborted/);
|
|
355
|
-
assert.deepEqual(consumed, ["first"]);
|
|
356
|
-
assert.equal(aliveAtCallback, true);
|
|
357
|
-
const child = children.at(-1);
|
|
358
|
-
assert.ok(child && closed.has(child), "runner must await child close");
|
|
359
|
-
assert.ok(child.exitCode !== null || child.signalCode !== null);
|
|
360
|
-
}
|
|
361
|
-
missingExecutable = true;
|
|
362
|
-
await assert.rejects(
|
|
363
|
-
runRgPaths([], undefined, () => true),
|
|
364
|
-
/Failed to run ripgrep/,
|
|
365
|
-
);
|
|
366
|
-
} finally {
|
|
367
|
-
for (const child of children) if (!closed.has(child)) child.kill("SIGKILL");
|
|
368
|
-
t.mock.restoreAll();
|
|
369
|
-
syncBuiltinESMExports();
|
|
370
|
-
await rm(directory, { recursive: true, force: true });
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
test("directory searches respect ignores but explicit files and hidden files remain searchable", async () => {
|
|
375
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-ignore-"));
|
|
376
|
-
try {
|
|
377
|
-
await writeFile(join(directory, ".ignore"), "ignored.txt\n");
|
|
378
|
-
const ignored = join(directory, "ignored.txt");
|
|
379
|
-
await writeFile(ignored, "needle\n");
|
|
380
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
381
|
-
const normal = await invoke(
|
|
382
|
-
tool,
|
|
383
|
-
"0",
|
|
384
|
-
{ literal: true, pattern: "needle" },
|
|
385
|
-
undefined,
|
|
386
|
-
undefined,
|
|
387
|
-
);
|
|
388
|
-
assert.equal(grepCounts(normal).matches, 0);
|
|
389
|
-
const explicit = await invoke(
|
|
390
|
-
tool,
|
|
391
|
-
"0",
|
|
392
|
-
{ literal: true, pattern: "needle", path: ignored },
|
|
393
|
-
undefined,
|
|
394
|
-
undefined,
|
|
395
|
-
);
|
|
396
|
-
assert.match(explicit.content[0].text, /ignored\.txt · 1 match/);
|
|
397
|
-
const excluded = await invoke(
|
|
398
|
-
tool,
|
|
399
|
-
"0",
|
|
400
|
-
{ literal: true, pattern: "needle", path: ignored, glob: "!ignored.txt" },
|
|
401
|
-
undefined,
|
|
402
|
-
undefined,
|
|
403
|
-
);
|
|
404
|
-
assert.equal(grepCounts(excluded).matches, 0);
|
|
405
|
-
await writeFile(join(directory, ".hidden.txt"), "needle\n");
|
|
406
|
-
const hidden = await invoke(
|
|
407
|
-
tool,
|
|
408
|
-
"0",
|
|
409
|
-
{ literal: true, pattern: "needle" },
|
|
410
|
-
undefined,
|
|
411
|
-
undefined,
|
|
412
|
-
);
|
|
413
|
-
assert.match(hidden.content[0].text, /\.hidden\.txt · 1 match/);
|
|
414
|
-
assert.doesNotMatch(hidden.content[0].text, /ignored\.txt · 1 match/);
|
|
415
|
-
} finally {
|
|
416
|
-
await rm(directory, { recursive: true, force: true });
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
test("gitignored directory → root traversal skips it and an explicit directory path searches it", async (t) => {
|
|
421
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-ignore-scope-"));
|
|
422
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
423
|
-
const ignored = join(directory, "ignored");
|
|
424
|
-
await mkdir(join(directory, ".git"));
|
|
425
|
-
await mkdir(ignored);
|
|
426
|
-
await writeFile(join(directory, ".gitignore"), "ignored/\n");
|
|
427
|
-
await writeFile(join(ignored, "x.txt"), "needle\n");
|
|
428
|
-
await writeFile(join(directory, "a.txt"), "needle\n");
|
|
429
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
430
|
-
for (const [path, expected] of [
|
|
431
|
-
[directory, "a.txt"],
|
|
432
|
-
[ignored, "ignored/x.txt"],
|
|
433
|
-
]) {
|
|
434
|
-
const result = await callTool(tool, {
|
|
435
|
-
path,
|
|
436
|
-
literal: true,
|
|
437
|
-
pattern: "needle",
|
|
438
|
-
outputMode: "files",
|
|
439
|
-
});
|
|
440
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
441
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
442
|
-
assert.equal(result.content[0].type, "text");
|
|
443
|
-
assert.equal(result.content[0].text.replace(/\\/g, "/"), expected);
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
test("named directory → nested ignores, .git and links still limit traversal", async (t) => {
|
|
448
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-nested-scope-"));
|
|
449
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
450
|
-
await mkdir(join(directory, ".git"));
|
|
451
|
-
await writeFile(join(directory, ".gitignore"), "node_modules/\ndist/\n*.map\n");
|
|
452
|
-
for (const file of [
|
|
453
|
-
"node_modules/foo/index.js",
|
|
454
|
-
"node_modules/foo/dist/bundle.js",
|
|
455
|
-
"node_modules/foo/index.js.map",
|
|
456
|
-
"named/index.js",
|
|
457
|
-
"named/nested/.git/config",
|
|
458
|
-
"target/linked.js",
|
|
459
|
-
]) {
|
|
460
|
-
await mkdir(dirname(join(directory, file)), { recursive: true });
|
|
461
|
-
await writeFile(join(directory, file), "needle\n");
|
|
462
|
-
}
|
|
463
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
464
|
-
for (const { path, expected, linked } of [
|
|
465
|
-
{ path: "node_modules/foo", expected: "node_modules/foo/index.js" },
|
|
466
|
-
{ path: "node_modules/foo/dist", expected: "node_modules/foo/dist/bundle.js" },
|
|
467
|
-
{ path: "node_modules/foo/index.js.map", expected: "node_modules/foo/index.js.map" },
|
|
468
|
-
{ path: "named", expected: "named/index.js" },
|
|
469
|
-
{ path: "named", expected: "named/index.js", linked: true },
|
|
470
|
-
]) {
|
|
471
|
-
await t.test(`${path}${linked ? " with nested link" : ""}`, async (t) => {
|
|
472
|
-
if (linked) {
|
|
473
|
-
try {
|
|
474
|
-
await symlink(
|
|
475
|
-
join(directory, "target"),
|
|
476
|
-
join(directory, "named/nested/link"),
|
|
477
|
-
process.platform === "win32" ? "junction" : "dir",
|
|
478
|
-
);
|
|
479
|
-
} catch (error) {
|
|
480
|
-
if (
|
|
481
|
-
error instanceof Error &&
|
|
482
|
-
"code" in error &&
|
|
483
|
-
["EPERM", "EACCES", "ENOTSUP"].includes(String(error.code))
|
|
484
|
-
) {
|
|
485
|
-
t.skip("symbolic link creation unavailable");
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
throw error;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
const result = await callTool(tool, {
|
|
492
|
-
path: [path],
|
|
493
|
-
literal: true,
|
|
494
|
-
pattern: ["needle"],
|
|
495
|
-
outputMode: "files",
|
|
496
|
-
});
|
|
497
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
498
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
499
|
-
assert.equal(result.content[0].type, "text");
|
|
500
|
-
assert.equal(result.content[0].text.replace(/\\/g, "/"), expected);
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
test("directory traversal with caller globs → excludes .git while an explicit .git path remains searchable", async (t) => {
|
|
506
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-git-scope-"));
|
|
507
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
508
|
-
const git = join(directory, ".git");
|
|
509
|
-
await mkdir(git);
|
|
510
|
-
await writeFile(join(git, "x.txt"), "needle\n");
|
|
511
|
-
await writeFile(join(directory, "a.txt"), "needle\n");
|
|
512
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
513
|
-
for (const multiline of [false, true]) {
|
|
514
|
-
for (const glob of [undefined, "*"]) {
|
|
515
|
-
for (const [path, expected] of [
|
|
516
|
-
[directory, "a.txt"],
|
|
517
|
-
[git, ".git/x.txt"],
|
|
518
|
-
]) {
|
|
519
|
-
const result = await callTool(tool, {
|
|
520
|
-
path,
|
|
521
|
-
glob,
|
|
522
|
-
multiline,
|
|
523
|
-
literal: true,
|
|
524
|
-
pattern: "needle",
|
|
525
|
-
outputMode: "files",
|
|
526
|
-
});
|
|
527
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
528
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
529
|
-
assert.equal(result.content[0].type, "text");
|
|
530
|
-
assert.equal(result.content[0].text.replace(/\\/g, "/"), expected);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
test("case-variant candidate paths → deduplicate aliases while retaining distinct files and later matches", async (t) => {
|
|
537
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-dedup-"));
|
|
538
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
539
|
-
const first = join(directory, "MiXeD.txt");
|
|
540
|
-
const variant = join(directory, "mixed.txt");
|
|
541
|
-
const later = join(directory, "later.txt");
|
|
542
|
-
await writeFile(first, "needle\n");
|
|
543
|
-
await writeFile(later, "needle\n");
|
|
544
|
-
let distinct = true;
|
|
545
|
-
try {
|
|
546
|
-
await writeFile(variant, "needle\n", { flag: "wx" });
|
|
547
|
-
} catch (error) {
|
|
548
|
-
if (error instanceof Error && "code" in error && error.code === "EEXIST") distinct = false;
|
|
549
|
-
else throw error;
|
|
550
|
-
}
|
|
551
|
-
const expected = distinct
|
|
552
|
-
? "MiXeD.txt: 1\nmixed.txt: 1\nlater.txt: 1"
|
|
553
|
-
: "MiXeD.txt: 1\nlater.txt: 1";
|
|
554
|
-
const limit = distinct ? 3 : 2;
|
|
555
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
556
|
-
for (const [pattern, multiline] of [
|
|
557
|
-
["needle", false],
|
|
558
|
-
["needle", true],
|
|
559
|
-
[".", true],
|
|
560
|
-
] as const) {
|
|
561
|
-
await t.test(`${pattern}, multiline=${multiline}`, async () => {
|
|
562
|
-
const result = await callTool(tool, {
|
|
563
|
-
path: [first, variant, later],
|
|
564
|
-
pattern: [pattern],
|
|
565
|
-
literal: false,
|
|
566
|
-
multiline,
|
|
567
|
-
outputMode: "count",
|
|
568
|
-
limit,
|
|
569
|
-
});
|
|
570
|
-
assert.equal(result.isError, undefined);
|
|
571
|
-
assert.equal(result.content[0].type, "text");
|
|
572
|
-
assert.equal(result.content[0].text.split("\n{\n")[0].replaceAll("\\", "/"), expected);
|
|
573
|
-
assert.equal(grepCounts(result).matches, limit);
|
|
574
|
-
assert.equal(grepCounts(result).files, limit);
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
test("explicit file with different case and a glob → admits the same file on a case-insensitive filesystem", async (t) => {
|
|
580
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-case-admission-"));
|
|
581
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
582
|
-
const file = join(directory, "MiXeD.txt");
|
|
583
|
-
const requested = join(directory, "mixed.txt");
|
|
584
|
-
await writeFile(file, "needle\n");
|
|
585
|
-
let alias;
|
|
586
|
-
try {
|
|
587
|
-
alias = await stat(requested, { bigint: true });
|
|
588
|
-
} catch (error) {
|
|
589
|
-
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
590
|
-
return t.skip("Temporary filesystem is case-sensitive");
|
|
591
|
-
throw error;
|
|
592
|
-
}
|
|
593
|
-
const original = await stat(file, { bigint: true });
|
|
594
|
-
assert.equal(alias.dev, original.dev);
|
|
595
|
-
assert.equal(alias.ino, original.ino);
|
|
596
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
597
|
-
const result = await callTool(tool, {
|
|
598
|
-
path: requested,
|
|
599
|
-
glob: "*.txt",
|
|
600
|
-
literal: true,
|
|
601
|
-
pattern: "needle",
|
|
602
|
-
});
|
|
603
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
604
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
605
|
-
assert.equal(grepCounts(result).files, 1);
|
|
606
|
-
assert.equal(result.content[0].type, "text");
|
|
607
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│needle/);
|
|
608
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
test("explicit file with a dangling sibling link and a glob → returns complete matches without listing diagnostics", async (t) => {
|
|
612
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-dangling-sibling-"));
|
|
613
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
614
|
-
const file = join(directory, "fixture.txt");
|
|
615
|
-
const link = join(directory, "broken.txt");
|
|
616
|
-
await writeFile(file, "needle\n");
|
|
617
|
-
try {
|
|
618
|
-
await symlink(
|
|
619
|
-
join(directory, "missing"),
|
|
620
|
-
link,
|
|
621
|
-
process.platform === "win32" ? "junction" : "dir",
|
|
622
|
-
);
|
|
623
|
-
} catch (error) {
|
|
624
|
-
if (
|
|
625
|
-
error instanceof Error &&
|
|
626
|
-
"code" in error &&
|
|
627
|
-
typeof error.code === "string" &&
|
|
628
|
-
["EPERM", "EACCES", "ENOTSUP"].includes(error.code)
|
|
629
|
-
)
|
|
630
|
-
return t.skip("Symbolic link creation unavailable");
|
|
631
|
-
throw error;
|
|
632
|
-
}
|
|
633
|
-
await assert.rejects(stat(link), { code: "ENOENT" });
|
|
634
|
-
let listingDiagnostics = "";
|
|
635
|
-
const tool = makeMockGrep(directory, DEFAULT_CONFIG, {
|
|
636
|
-
runRgPaths: async (...args) => {
|
|
637
|
-
const listed = await runRgPaths(...args);
|
|
638
|
-
listingDiagnostics = listed.stderr;
|
|
639
|
-
return listed;
|
|
640
|
-
},
|
|
641
|
-
});
|
|
642
|
-
for (const path of [[file], [file, file]]) {
|
|
643
|
-
await t.test(`${path.length} explicit path entries`, async () => {
|
|
644
|
-
listingDiagnostics = "";
|
|
645
|
-
const result = await callTool(tool, {
|
|
646
|
-
path,
|
|
647
|
-
glob: "*.txt",
|
|
648
|
-
literal: true,
|
|
649
|
-
pattern: "needle",
|
|
650
|
-
});
|
|
651
|
-
assert.match(listingDiagnostics, /broken\.txt/);
|
|
652
|
-
const report = reportOf(result, "grep");
|
|
653
|
-
assert.equal(result.isError, undefined, JSON.stringify(report));
|
|
654
|
-
assert.deepEqual(grepCounts(result), { matches: 1, files: 1 });
|
|
655
|
-
assert.equal(report?.diagnostics, undefined);
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
});
|
|
659
|
-
|
|
660
|
-
test("explicit file paths obey ordered glob filters without inheriting ignore rules", async () => {
|
|
661
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-file-glob-"));
|
|
662
|
-
try {
|
|
663
|
-
const kept = join(directory, "keep.ts");
|
|
664
|
-
const excluded = join(directory, "drop.test.ts");
|
|
665
|
-
const ignored = join(directory, "ignored.ts");
|
|
666
|
-
await writeFile(join(directory, ".ignore"), "ignored.ts\n");
|
|
667
|
-
await Promise.all([kept, excluded, ignored].map((path) => writeFile(path, "needle\n")));
|
|
668
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
669
|
-
const result: any = await invoke(
|
|
670
|
-
tool,
|
|
671
|
-
"0",
|
|
672
|
-
{
|
|
673
|
-
literal: true,
|
|
674
|
-
pattern: "needle",
|
|
675
|
-
path: [kept, excluded, ignored],
|
|
676
|
-
glob: ["*.ts", "!**/*.test.ts"],
|
|
677
|
-
outputMode: "files",
|
|
678
|
-
},
|
|
679
|
-
undefined,
|
|
680
|
-
undefined,
|
|
681
|
-
);
|
|
682
|
-
const files = new Set(result.content[0].text.split("\n"));
|
|
683
|
-
assert.deepEqual(files, new Set(["keep.ts", "ignored.ts"]));
|
|
684
|
-
|
|
685
|
-
const none: any = await invoke(
|
|
686
|
-
tool,
|
|
687
|
-
"0",
|
|
688
|
-
{ literal: true, pattern: "needle", path: excluded, glob: "!**/*.test.ts" },
|
|
689
|
-
undefined,
|
|
690
|
-
undefined,
|
|
691
|
-
);
|
|
692
|
-
assert.equal(grepCounts(none).matches, 0);
|
|
693
|
-
} finally {
|
|
694
|
-
await rm(directory, { recursive: true, force: true });
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
|
|
698
|
-
test("directory traversal skips symlinks but explicit linked directories and files remain searchable", async (t) => {
|
|
699
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-follow-"));
|
|
700
|
-
try {
|
|
701
|
-
const root = join(directory, "root");
|
|
702
|
-
const target = join(directory, "target");
|
|
703
|
-
const link = join(root, "link");
|
|
704
|
-
const secondLink = join(root, "second-link");
|
|
705
|
-
await mkdir(root);
|
|
706
|
-
await mkdir(target);
|
|
707
|
-
const targetFile = join(target, "linked.txt");
|
|
708
|
-
await writeFile(targetFile, "needle\n");
|
|
709
|
-
try {
|
|
710
|
-
await symlink(target, link, process.platform === "win32" ? "junction" : "dir");
|
|
711
|
-
await symlink(target, secondLink, process.platform === "win32" ? "junction" : "dir");
|
|
712
|
-
} catch (error: any) {
|
|
713
|
-
if (["EPERM", "EACCES", "ENOTSUP"].includes(error?.code))
|
|
714
|
-
return t.skip("symbolic link creation unavailable");
|
|
715
|
-
throw error;
|
|
716
|
-
}
|
|
717
|
-
const grep = makeGrepOverride(root, DEFAULT_CONFIG);
|
|
718
|
-
const hidden: any = await invoke(
|
|
719
|
-
grep,
|
|
720
|
-
"0",
|
|
721
|
-
{ literal: true, pattern: "needle" },
|
|
722
|
-
undefined,
|
|
723
|
-
undefined,
|
|
724
|
-
);
|
|
725
|
-
assert.equal(grepCounts(hidden).matches, 0);
|
|
726
|
-
const linkedDirectory = await callTool(grep, {
|
|
727
|
-
path: link,
|
|
728
|
-
literal: true,
|
|
729
|
-
pattern: "needle",
|
|
730
|
-
outputMode: "files",
|
|
731
|
-
});
|
|
732
|
-
assert.equal(
|
|
733
|
-
linkedDirectory.isError,
|
|
734
|
-
undefined,
|
|
735
|
-
JSON.stringify(reportOf(linkedDirectory, "grep")),
|
|
736
|
-
);
|
|
737
|
-
assert.equal(grepCounts(linkedDirectory).matches, 1);
|
|
738
|
-
assert.equal(linkedDirectory.content[0].type, "text");
|
|
739
|
-
assert.equal(linkedDirectory.content[0].text.replace(/\\/g, "/"), "link/linked.txt");
|
|
740
|
-
const found: any = await invoke(
|
|
741
|
-
grep,
|
|
742
|
-
"0",
|
|
743
|
-
{ literal: true, pattern: "needle", path: join(link, "linked.txt") },
|
|
744
|
-
undefined,
|
|
745
|
-
undefined,
|
|
746
|
-
);
|
|
747
|
-
const [header, anchored] = found.content[0].text.split("\n");
|
|
748
|
-
const resultPath = header.slice(0, header.lastIndexOf(" · "));
|
|
749
|
-
assert.equal(resultPath.replace(/\\/g, "/"), "link/linked.txt");
|
|
750
|
-
assert.equal(found.content[0].text.match(/ · 1 match/g)?.length, 1);
|
|
751
|
-
const anchor = anchored.slice(0, anchored.indexOf("│"));
|
|
752
|
-
const edit: any = makeEditOverride(root, DEFAULT_CONFIG);
|
|
753
|
-
await invoke(
|
|
754
|
-
edit,
|
|
755
|
-
"0",
|
|
756
|
-
{ path: resultPath, steps: [{ action: "replace", anchor, lines: ["updated"] }] },
|
|
757
|
-
undefined,
|
|
758
|
-
undefined,
|
|
759
|
-
);
|
|
760
|
-
assert.equal(await readFile(targetFile, "utf8"), "updated\n");
|
|
761
|
-
assert.equal((await lstat(link)).isSymbolicLink(), true);
|
|
762
|
-
assert.equal(await readlink(link), target);
|
|
763
|
-
assert.equal((await lstat(secondLink)).isSymbolicLink(), true);
|
|
764
|
-
} finally {
|
|
765
|
-
await rm(directory, { recursive: true, force: true });
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
test("unbounded lookbehind → native JS matches after rg discovery", async (t) => {
|
|
770
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-lookbehind-"));
|
|
771
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
772
|
-
await writeFile(join(directory, "fixture.txt"), "aaab\n");
|
|
773
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
774
|
-
const result = await invoke(tool, "0", { pattern: "(?<=a+)b", literal: false });
|
|
775
|
-
assert.equal(result.isError, undefined, JSON.stringify(reportOf(result, "grep")));
|
|
776
|
-
assert.equal(grepCounts(result).matches, 1);
|
|
777
|
-
});
|
|
778
|
-
|
|
779
|
-
test("line-based grep uses context for display but never matches across CRLF", async () => {
|
|
780
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-lines-"));
|
|
781
|
-
try {
|
|
782
|
-
await writeFile(join(directory, "fixture.txt"), "alpha\r\nbeta\r\ngamma\r\n");
|
|
783
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
784
|
-
const context = await invoke(
|
|
785
|
-
tool,
|
|
786
|
-
"0",
|
|
787
|
-
{ literal: true, pattern: "alpha", context: 1 },
|
|
788
|
-
undefined,
|
|
789
|
-
undefined,
|
|
790
|
-
);
|
|
791
|
-
assert.match(context.content[0].text, /fixture\.txt · 1 match/);
|
|
792
|
-
assert.match(context.content[0].text, /1#[0-9A-Z]+│alpha/);
|
|
793
|
-
assert.match(context.content[0].text, /2#[0-9A-Z]+│beta/);
|
|
794
|
-
assert.doesNotMatch(context.content[0].text, /3#[0-9A-Z]+│gamma/);
|
|
795
|
-
for (const [pattern, literal] of [
|
|
796
|
-
["alpha\\nbeta", false],
|
|
797
|
-
["alpha\nbeta", true],
|
|
798
|
-
] as const) {
|
|
799
|
-
const result = await invoke(tool, "0", { pattern, literal });
|
|
800
|
-
assert.equal(result.isError, undefined);
|
|
801
|
-
assert.equal(grepCounts(result).matches, 0);
|
|
802
|
-
}
|
|
803
|
-
const limited = await invoke(
|
|
804
|
-
tool,
|
|
805
|
-
"0",
|
|
806
|
-
{ literal: true, pattern: ["alpha", "beta"], outputMode: "count", limit: 1 },
|
|
807
|
-
undefined,
|
|
808
|
-
undefined,
|
|
809
|
-
);
|
|
810
|
-
assert.match(limited.content[0].text, /fixture\.txt: 1/);
|
|
811
|
-
assert.equal(reportOf(limited, "grep")?.matchLimit, 1);
|
|
812
|
-
} finally {
|
|
813
|
-
await rm(directory, { recursive: true, force: true });
|
|
814
|
-
}
|
|
815
|
-
});
|
|
816
|
-
|
|
817
|
-
test("multiline grep anchors CRLF spans and counts overlapping physical lines once", async () => {
|
|
818
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-"));
|
|
819
|
-
try {
|
|
820
|
-
const file = join(directory, "fixture.txt");
|
|
821
|
-
await writeFile(file, "alpha\r\nbeta\r\ngamma\r\n");
|
|
822
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
823
|
-
const regex = await invoke(
|
|
824
|
-
tool,
|
|
825
|
-
"0",
|
|
826
|
-
{ pattern: "alpha\\nbeta", path: file, literal: false, multiline: true },
|
|
827
|
-
undefined,
|
|
828
|
-
undefined,
|
|
829
|
-
);
|
|
830
|
-
assert.match(regex.content[0].text, /fixture\.txt · 2 matches/);
|
|
831
|
-
assert.match(regex.content[0].text, /1#[0-9A-Z]+│alpha/);
|
|
832
|
-
assert.match(regex.content[0].text, /2#[0-9A-Z]+│beta/);
|
|
833
|
-
const literal = await invoke(
|
|
834
|
-
tool,
|
|
835
|
-
"0",
|
|
836
|
-
{ pattern: "alpha\nbeta", path: file, literal: true, multiline: true },
|
|
837
|
-
undefined,
|
|
838
|
-
undefined,
|
|
839
|
-
);
|
|
840
|
-
assert.match(literal.content[0].text, /fixture\.txt · 2 matches/);
|
|
841
|
-
const overlapping = await invoke(
|
|
842
|
-
tool,
|
|
843
|
-
"0",
|
|
844
|
-
{ pattern: ["alpha\\nbeta", "beta\\ngamma"], path: file, multiline: true, literal: false },
|
|
845
|
-
undefined,
|
|
846
|
-
undefined,
|
|
847
|
-
);
|
|
848
|
-
assert.match(overlapping.content[0].text, /fixture\.txt · 3 matches/);
|
|
849
|
-
const bounded = await invoke(
|
|
850
|
-
tool,
|
|
851
|
-
"0",
|
|
852
|
-
{
|
|
853
|
-
pattern: "alpha[\\w\\s]*gamma",
|
|
854
|
-
literal: false,
|
|
855
|
-
path: file,
|
|
856
|
-
multiline: true,
|
|
857
|
-
outputMode: "count",
|
|
858
|
-
limit: 2,
|
|
859
|
-
},
|
|
860
|
-
undefined,
|
|
861
|
-
undefined,
|
|
862
|
-
);
|
|
863
|
-
assert.match(bounded.content[0].text, /fixture\.txt: 2/);
|
|
864
|
-
assert.equal(grepCounts(bounded).matches, 2);
|
|
865
|
-
assert.equal(grepCounts(bounded).files, 1);
|
|
866
|
-
assert.equal(reportOf(bounded, "grep")?.matchLimit, 2);
|
|
867
|
-
const files = await invoke(
|
|
868
|
-
tool,
|
|
869
|
-
"0",
|
|
870
|
-
{
|
|
871
|
-
pattern: "alpha\\nbeta",
|
|
872
|
-
literal: false,
|
|
873
|
-
path: file,
|
|
874
|
-
multiline: true,
|
|
875
|
-
outputMode: "files",
|
|
876
|
-
limit: 1,
|
|
877
|
-
},
|
|
878
|
-
undefined,
|
|
879
|
-
undefined,
|
|
880
|
-
);
|
|
881
|
-
assert.equal(files.content[0].text, "fixture.txt");
|
|
882
|
-
assert.equal(reportOf(files, "grep")?.matchLimit, 1);
|
|
883
|
-
const boundary = await invoke(
|
|
884
|
-
tool,
|
|
885
|
-
"0",
|
|
886
|
-
{ pattern: "alpha\\n", path: file, multiline: true, literal: false },
|
|
887
|
-
undefined,
|
|
888
|
-
undefined,
|
|
889
|
-
);
|
|
890
|
-
assert.match(boundary.content[0].text, /fixture\.txt · 1 match/);
|
|
891
|
-
const edit: any = makeEditOverride(directory, DEFAULT_CONFIG);
|
|
892
|
-
const anchor = regex.content[0].text.match(/(2#[0-9A-Z]+)│beta/)?.[1];
|
|
893
|
-
assert.ok(anchor);
|
|
894
|
-
await invoke(
|
|
895
|
-
edit,
|
|
896
|
-
"0",
|
|
897
|
-
{ path: file, steps: [{ action: "replace", anchor, lines: ["updated"] }] },
|
|
898
|
-
undefined,
|
|
899
|
-
undefined,
|
|
900
|
-
);
|
|
901
|
-
assert.equal(await readFile(file, "utf8"), "alpha\r\nupdated\r\ngamma\r\n");
|
|
902
|
-
} finally {
|
|
903
|
-
await rm(directory, { recursive: true, force: true });
|
|
904
|
-
}
|
|
905
|
-
});
|
|
906
|
-
|
|
907
|
-
test("multiline grep preserves BOM and standalone CR while normalizing CRLF", async () => {
|
|
908
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-cr-"));
|
|
909
|
-
try {
|
|
910
|
-
const file = join(directory, "fixture.txt");
|
|
911
|
-
await writeFile(file, "\uFEFFalpha\rsolo\r\nbeta\r\n");
|
|
912
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
913
|
-
const result = await invoke(
|
|
914
|
-
tool,
|
|
915
|
-
"0",
|
|
916
|
-
{ pattern: "alpha\\rsolo\\nbeta", path: file, multiline: true, literal: false },
|
|
917
|
-
undefined,
|
|
918
|
-
undefined,
|
|
919
|
-
);
|
|
920
|
-
assert.match(result.content[0].text, /fixture\.txt · 2 matches/);
|
|
921
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│\uFEFFalpha␍solo/);
|
|
922
|
-
assert.match(result.content[0].text, /2#[0-9A-Z]+│beta/);
|
|
923
|
-
const literal = await invoke(
|
|
924
|
-
tool,
|
|
925
|
-
"0",
|
|
926
|
-
{ pattern: "alpha\rsolo\r\nbeta", path: file, multiline: true, literal: true },
|
|
927
|
-
undefined,
|
|
928
|
-
undefined,
|
|
929
|
-
);
|
|
930
|
-
assert.match(literal.content[0].text, /fixture\.txt · 2 matches/);
|
|
931
|
-
} finally {
|
|
932
|
-
await rm(directory, { recursive: true, force: true });
|
|
933
|
-
}
|
|
934
|
-
});
|
|
935
|
-
|
|
936
|
-
test("multiline grep anchors empty lines and line-start zero-width matches", async () => {
|
|
937
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-empty-"));
|
|
938
|
-
try {
|
|
939
|
-
const file = join(directory, "fixture.txt");
|
|
940
|
-
await writeFile(file, "alpha\r\n\r\nbeta\r\n");
|
|
941
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
942
|
-
const span = await invoke(
|
|
943
|
-
tool,
|
|
944
|
-
"0",
|
|
945
|
-
{ pattern: "alpha\\n\\nbeta", path: file, multiline: true, literal: false },
|
|
946
|
-
undefined,
|
|
947
|
-
undefined,
|
|
948
|
-
);
|
|
949
|
-
assert.match(span.content[0].text, /fixture\.txt · 3 matches/);
|
|
950
|
-
assert.match(span.content[0].text, /2#[0-9A-Z]+│\n3#[0-9A-Z]+│beta/);
|
|
951
|
-
const starts = await invoke(
|
|
952
|
-
tool,
|
|
953
|
-
"0",
|
|
954
|
-
{ pattern: "^", path: file, multiline: true, literal: false },
|
|
955
|
-
undefined,
|
|
956
|
-
undefined,
|
|
957
|
-
);
|
|
958
|
-
assert.match(starts.content[0].text, /fixture\.txt · 3 matches/);
|
|
959
|
-
assert.match(starts.content[0].text, /1#[0-9A-Z]+│alpha/);
|
|
960
|
-
assert.match(starts.content[0].text, /3#[0-9A-Z]+│beta/);
|
|
961
|
-
} finally {
|
|
962
|
-
await rm(directory, { recursive: true, force: true });
|
|
963
|
-
}
|
|
964
|
-
});
|
|
965
|
-
|
|
966
|
-
test("multiline zero-width EOF stays on the last existing physical line", {
|
|
967
|
-
skip: unsupportedByHost(["(?-m:$)"]),
|
|
968
|
-
}, async () => {
|
|
969
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-multiline-eof-"));
|
|
970
|
-
try {
|
|
971
|
-
const file = join(directory, "fixture.txt");
|
|
972
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
973
|
-
for (const content of ["last line", "last line\n"]) {
|
|
974
|
-
await writeFile(file, content);
|
|
975
|
-
const result = await invoke(
|
|
976
|
-
tool,
|
|
977
|
-
"0",
|
|
978
|
-
{ pattern: "(?-m:$)", path: file, multiline: true, literal: false },
|
|
979
|
-
undefined,
|
|
980
|
-
undefined,
|
|
981
|
-
);
|
|
982
|
-
if (content.endsWith("\n")) {
|
|
983
|
-
assert.equal(grepCounts(result).matches, 0);
|
|
984
|
-
} else {
|
|
985
|
-
assert.match(result.content[0].text, /fixture\.txt · 1 match/);
|
|
986
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│last line/);
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
} finally {
|
|
990
|
-
await rm(directory, { recursive: true, force: true });
|
|
991
|
-
}
|
|
992
|
-
});
|
|
993
|
-
|
|
994
|
-
test("files and count modes aggregate only matching lines inside the limit", async () => {
|
|
995
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-output-modes-"));
|
|
996
|
-
try {
|
|
997
|
-
const first = join(directory, "a.txt");
|
|
998
|
-
const second = join(directory, "b.txt");
|
|
999
|
-
await writeFile(first, "needle needle\nneedle\n");
|
|
1000
|
-
await writeFile(second, "needle\n");
|
|
1001
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1002
|
-
const query = { literal: true, pattern: "needle", path: [first, second], limit: 1 };
|
|
1003
|
-
const files = await invoke(
|
|
1004
|
-
tool,
|
|
1005
|
-
"files",
|
|
1006
|
-
{ ...query, outputMode: "files" },
|
|
1007
|
-
undefined,
|
|
1008
|
-
undefined,
|
|
1009
|
-
);
|
|
1010
|
-
assert.match(files.content[0].text, /^(?:a|b)\.txt$/);
|
|
1011
|
-
assert.equal(reportOf(files, "grep")?.matchLimit, 1);
|
|
1012
|
-
const limited = await invoke(
|
|
1013
|
-
tool,
|
|
1014
|
-
"count",
|
|
1015
|
-
{ ...query, outputMode: "count" },
|
|
1016
|
-
undefined,
|
|
1017
|
-
undefined,
|
|
1018
|
-
);
|
|
1019
|
-
assert.equal(grepCounts(limited).matches, 1);
|
|
1020
|
-
assert.equal(grepCounts(limited).files, 1);
|
|
1021
|
-
const complete = await invoke(
|
|
1022
|
-
tool,
|
|
1023
|
-
"count-all",
|
|
1024
|
-
{ ...query, outputMode: "count", limit: 10 },
|
|
1025
|
-
undefined,
|
|
1026
|
-
undefined,
|
|
1027
|
-
);
|
|
1028
|
-
assert.equal(grepCounts(complete).matches, 3);
|
|
1029
|
-
assert.equal(grepCounts(complete).files, 2);
|
|
1030
|
-
const contextual = await invoke(
|
|
1031
|
-
tool,
|
|
1032
|
-
"content",
|
|
1033
|
-
{ ...query, path: first, outputMode: "content", context: 1 },
|
|
1034
|
-
undefined,
|
|
1035
|
-
undefined,
|
|
1036
|
-
);
|
|
1037
|
-
assert.match(contextual.content[0].text, /a\.txt · 1 match/);
|
|
1038
|
-
assert.match(contextual.content[0].text, /2#[0-9A-Z]+│needle/);
|
|
1039
|
-
} finally {
|
|
1040
|
-
await rm(directory, { recursive: true, force: true });
|
|
1041
|
-
}
|
|
1042
|
-
});
|
|
1043
|
-
|
|
1044
|
-
test("real rg accepts wildcard-only regexes and preserves limits and literal mode", async () => {
|
|
1045
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-wildcard-"));
|
|
1046
|
-
try {
|
|
1047
|
-
await writeFile(join(directory, "fixture.txt"), "first\n\n.*\n");
|
|
1048
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1049
|
-
for (const [pattern, expected] of [
|
|
1050
|
-
["[\\w.*]*", 3],
|
|
1051
|
-
["^[\\w.*]+$", 2],
|
|
1052
|
-
["[\\w.*]?", 3],
|
|
1053
|
-
] as const) {
|
|
1054
|
-
const result: any = await invoke(
|
|
1055
|
-
tool,
|
|
1056
|
-
"count",
|
|
1057
|
-
{ pattern, literal: false, outputMode: "count" },
|
|
1058
|
-
undefined,
|
|
1059
|
-
undefined,
|
|
1060
|
-
);
|
|
1061
|
-
assert.equal(grepCounts(result).matches, expected);
|
|
1062
|
-
assert.equal(grepCounts(result).files, 1);
|
|
1063
|
-
}
|
|
1064
|
-
const limited: any = await invoke(
|
|
1065
|
-
tool,
|
|
1066
|
-
"limited",
|
|
1067
|
-
{ pattern: "[\\w.*]*", literal: false, limit: 1 },
|
|
1068
|
-
undefined,
|
|
1069
|
-
undefined,
|
|
1070
|
-
);
|
|
1071
|
-
assert.match(limited.content[0].text, /1#[0-9A-Z]+│first/);
|
|
1072
|
-
assert.doesNotMatch(limited.content[0].text, /[23]#[0-9A-Z]+│/);
|
|
1073
|
-
const literal: any = await invoke(
|
|
1074
|
-
tool,
|
|
1075
|
-
"literal",
|
|
1076
|
-
{ pattern: ".*", literal: true },
|
|
1077
|
-
undefined,
|
|
1078
|
-
undefined,
|
|
1079
|
-
);
|
|
1080
|
-
assert.match(literal.content[0].text, /3#[0-9A-Z]+│\.\*/);
|
|
1081
|
-
} finally {
|
|
1082
|
-
await rm(directory, { recursive: true, force: true });
|
|
1083
|
-
}
|
|
1084
|
-
});
|
|
1085
|
-
|
|
1086
|
-
test("tools share physical lines and anchors across text representations", async () => {
|
|
1087
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-text-integration-"));
|
|
1088
|
-
const call = (tool: any, params: any) => invoke(tool, "0", params, undefined, undefined);
|
|
1089
|
-
const rows = (result: any): string[] =>
|
|
1090
|
-
result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line));
|
|
1091
|
-
try {
|
|
1092
|
-
const file = join(directory, "fixture.txt");
|
|
1093
|
-
for (const [index, before] of [
|
|
1094
|
-
"a\nold\n",
|
|
1095
|
-
"a\r\nold\r\n",
|
|
1096
|
-
"\uFEFFa\r\nold\nkeep\r\n",
|
|
1097
|
-
"a\rb\nold\n",
|
|
1098
|
-
"a\rb\nold",
|
|
1099
|
-
].entries()) {
|
|
1100
|
-
await call(makeWriteOverride(directory), {
|
|
1101
|
-
path: file,
|
|
1102
|
-
content: before,
|
|
1103
|
-
mode: index === 0 ? "create" : "overwrite",
|
|
1104
|
-
});
|
|
1105
|
-
assert.equal(await readFile(file, "utf8"), before);
|
|
1106
|
-
const read = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
|
|
1107
|
-
const grep = await call(makeGrepOverride(directory, DEFAULT_CONFIG), {
|
|
1108
|
-
literal: true,
|
|
1109
|
-
path: file,
|
|
1110
|
-
pattern: "old",
|
|
1111
|
-
context: 2,
|
|
1112
|
-
});
|
|
1113
|
-
assert.deepEqual(rows(grep), rows(read));
|
|
1114
|
-
if (before.includes("a\rb")) assert.match(rows(read)[0], /│a␍b$/);
|
|
1115
|
-
const anchor = rows(grep)[1].split("│")[0];
|
|
1116
|
-
const edit = await call(makeEditOverride(directory, DEFAULT_CONFIG), {
|
|
1117
|
-
path: file,
|
|
1118
|
-
steps: [{ action: "replace", anchor, lines: ["new"] }],
|
|
1119
|
-
});
|
|
1120
|
-
assert.equal(await readFile(file, "utf8"), before.replace("old", "new"));
|
|
1121
|
-
assert.match(edit.details.diff, /^-2 old/m);
|
|
1122
|
-
assert.match(edit.details.diff, /^\+2 new/m);
|
|
1123
|
-
assert.ok(!edit.details.diff.includes("\r"));
|
|
1124
|
-
await call(makeReplaceTool(directory, DEFAULT_CONFIG), {
|
|
1125
|
-
path: file,
|
|
1126
|
-
replacements: [{ literal: true, find: "new", replace: "next" }],
|
|
1127
|
-
});
|
|
1128
|
-
assert.equal(await readFile(file, "utf8"), before.replace("old", "next"));
|
|
1129
|
-
}
|
|
1130
|
-
} finally {
|
|
1131
|
-
await rm(directory, { recursive: true, force: true });
|
|
1132
|
-
}
|
|
1133
|
-
});
|
|
1134
|
-
|
|
1135
|
-
test("long-line previews preserve full-line anchors across literal and JavaScript regex queries", async () => {
|
|
1136
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
|
|
1137
|
-
try {
|
|
1138
|
-
const long = "😀界".repeat(220) + "NEEDLE" + "tail".repeat(200);
|
|
1139
|
-
const context = "x" + "😀".repeat(400);
|
|
1140
|
-
await writeFile(join(directory, "long.txt"), `${long}\r\nfollow\r\n${context}\r\n`);
|
|
1141
|
-
const tool = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1142
|
-
for (const params of [
|
|
1143
|
-
{ literal: true, pattern: "NEEDLE" },
|
|
1144
|
-
{ pattern: ["NEEDLE", "tail"], literal: true },
|
|
1145
|
-
{ pattern: "NEEDLE(?:tail)+", literal: false },
|
|
1146
|
-
]) {
|
|
1147
|
-
const result: any = await invoke(
|
|
1148
|
-
tool,
|
|
1149
|
-
"preview",
|
|
1150
|
-
{ ...params, context: 2 },
|
|
1151
|
-
undefined,
|
|
1152
|
-
undefined,
|
|
1153
|
-
);
|
|
1154
|
-
const output = result.content[0].text;
|
|
1155
|
-
const row = output.split("\n").find((line: string) => line.startsWith("1#"));
|
|
1156
|
-
assert.ok(row.startsWith(`1#${computeLineHash(1, long, 4)}│[partial, columns `));
|
|
1157
|
-
assert.ok(row.includes("NEEDLE"));
|
|
1158
|
-
assert.equal(Buffer.from(row).toString("utf8"), row);
|
|
1159
|
-
assert.ok(
|
|
1160
|
-
output.includes(
|
|
1161
|
-
`3#${computeLineHash(3, context, 4)}│[partial, columns 1-${DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits - 1}]`,
|
|
1162
|
-
),
|
|
1163
|
-
);
|
|
1164
|
-
assert.equal(
|
|
1165
|
-
reportOf(result, "grep")?.linePreviewLimit,
|
|
1166
|
-
DEFAULT_OUTPUT_BUDGETS.grepPreviewUnits - 1,
|
|
1167
|
-
);
|
|
1168
|
-
}
|
|
1169
|
-
} finally {
|
|
1170
|
-
await rm(directory, { recursive: true, force: true });
|
|
1171
|
-
}
|
|
1172
|
-
});
|
|
1173
|
-
|
|
1174
|
-
test("all text tools share logical CRLF matching, anchors, and mutation separators", async () => {
|
|
1175
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-shared-text-"));
|
|
1176
|
-
try {
|
|
1177
|
-
const path = join(directory, "mixed.txt");
|
|
1178
|
-
const before = "\uFEFFhead\r\nalpha\r\nbeta\nstand\rCR\r\r\nlast";
|
|
1179
|
-
const write = makeWriteOverride(directory);
|
|
1180
|
-
const read = makeReadOverride(directory, DEFAULT_CONFIG);
|
|
1181
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1182
|
-
await invoke(write, "write", { path, content: before, mode: "create" }, undefined, undefined, {
|
|
1183
|
-
cwd: directory,
|
|
1184
|
-
} as ExtensionContext);
|
|
1185
|
-
assert.equal(await readFile(path, "utf8"), before);
|
|
1186
|
-
const observed = await invoke(read, "read", { path }, undefined, undefined, {
|
|
1187
|
-
cwd: directory,
|
|
1188
|
-
} as ExtensionContext);
|
|
1189
|
-
assert.ok(observed.content[0].type === "text");
|
|
1190
|
-
const rows = observed.content[0].text
|
|
1191
|
-
.split("\n")
|
|
1192
|
-
.filter((line: string) => /^\d+#[0-9A-Z]+│/.test(line));
|
|
1193
|
-
for (const query of [
|
|
1194
|
-
{ pattern: ["alpha", "beta"], literal: true },
|
|
1195
|
-
{ pattern: "alpha|beta", literal: false },
|
|
1196
|
-
]) {
|
|
1197
|
-
const found = await invoke(grep, "grep", { path, ...query }, undefined, undefined);
|
|
1198
|
-
assert.ok(found.content[0].text.includes(rows[1]), JSON.stringify(query));
|
|
1199
|
-
assert.ok(found.content[0].text.includes(rows[2]), JSON.stringify(query));
|
|
1200
|
-
}
|
|
1201
|
-
const contextual = await invoke(
|
|
1202
|
-
grep,
|
|
1203
|
-
"grep",
|
|
1204
|
-
{ literal: true, path, pattern: "alpha", context: 1 },
|
|
1205
|
-
undefined,
|
|
1206
|
-
undefined,
|
|
1207
|
-
);
|
|
1208
|
-
assert.match(contextual.content[0].text, /mixed\.txt · 1 match/);
|
|
1209
|
-
assert.ok(contextual.content[0].text.includes(rows[1]));
|
|
1210
|
-
assert.ok(contextual.content[0].text.includes(rows[2]));
|
|
1211
|
-
const carriage = await invoke(
|
|
1212
|
-
grep,
|
|
1213
|
-
"grep",
|
|
1214
|
-
{ path, pattern: "\\r", literal: false },
|
|
1215
|
-
undefined,
|
|
1216
|
-
undefined,
|
|
1217
|
-
);
|
|
1218
|
-
assert.ok(carriage.content[0].text.includes(rows[3]));
|
|
1219
|
-
assert.ok(!carriage.content[0].text.includes(rows[1]));
|
|
1220
|
-
const standaloneEnd = await invoke(
|
|
1221
|
-
grep,
|
|
1222
|
-
"grep",
|
|
1223
|
-
{ path, pattern: "CR\\r$", literal: false },
|
|
1224
|
-
undefined,
|
|
1225
|
-
undefined,
|
|
1226
|
-
);
|
|
1227
|
-
assert.ok(standaloneEnd.content[0].text.includes(rows[3]));
|
|
1228
|
-
|
|
1229
|
-
const expected = "\uFEFFhead\r\nA\r\nB\r\nC\nstand\rCR\r\r\nlast";
|
|
1230
|
-
for (const mode of ["edit", "literal", "regex"]) {
|
|
1231
|
-
await invoke(
|
|
1232
|
-
write,
|
|
1233
|
-
"reset",
|
|
1234
|
-
{ path, content: before, mode: "overwrite" },
|
|
1235
|
-
undefined,
|
|
1236
|
-
undefined,
|
|
1237
|
-
{
|
|
1238
|
-
cwd: directory,
|
|
1239
|
-
} as ExtensionContext,
|
|
1240
|
-
);
|
|
1241
|
-
if (mode === "edit") {
|
|
1242
|
-
await invoke(
|
|
1243
|
-
makeEditOverride(directory, DEFAULT_CONFIG),
|
|
1244
|
-
"edit",
|
|
1245
|
-
{
|
|
1246
|
-
path,
|
|
1247
|
-
steps: [
|
|
1248
|
-
{
|
|
1249
|
-
action: "replace",
|
|
1250
|
-
anchor: rows[1].split("│")[0],
|
|
1251
|
-
end: rows[2].split("│")[0],
|
|
1252
|
-
lines: ["A", "B", "C"],
|
|
1253
|
-
},
|
|
1254
|
-
],
|
|
1255
|
-
},
|
|
1256
|
-
undefined,
|
|
1257
|
-
undefined,
|
|
1258
|
-
{ cwd: directory } as ExtensionContext,
|
|
1259
|
-
);
|
|
1260
|
-
} else {
|
|
1261
|
-
await invoke(
|
|
1262
|
-
makeReplaceTool(directory, DEFAULT_CONFIG),
|
|
1263
|
-
"replace",
|
|
1264
|
-
{
|
|
1265
|
-
path,
|
|
1266
|
-
replacements: [
|
|
1267
|
-
{
|
|
1268
|
-
find: mode === "regex" ? "alpha\\nbeta" : "alpha\r\nbeta",
|
|
1269
|
-
replace: "A\nB\nC",
|
|
1270
|
-
literal: mode !== "regex",
|
|
1271
|
-
},
|
|
1272
|
-
],
|
|
1273
|
-
},
|
|
1274
|
-
undefined,
|
|
1275
|
-
undefined,
|
|
1276
|
-
{ cwd: directory } as ExtensionContext,
|
|
1277
|
-
);
|
|
1278
|
-
}
|
|
1279
|
-
assert.equal(await readFile(path, "utf8"), expected, mode);
|
|
1280
|
-
}
|
|
1281
|
-
// Whole-file write remains the explicit representation boundary, including EOL conversion.
|
|
1282
|
-
await invoke(
|
|
1283
|
-
write,
|
|
1284
|
-
"convert",
|
|
1285
|
-
{ path, content: "alpha\nbeta\n", mode: "overwrite" },
|
|
1286
|
-
undefined,
|
|
1287
|
-
undefined,
|
|
1288
|
-
{
|
|
1289
|
-
cwd: directory,
|
|
1290
|
-
} as ExtensionContext,
|
|
1291
|
-
);
|
|
1292
|
-
assert.equal(await readFile(path, "utf8"), "alpha\nbeta\n");
|
|
1293
|
-
} finally {
|
|
1294
|
-
await rm(directory, { recursive: true, force: true });
|
|
1295
|
-
}
|
|
1296
|
-
});
|
|
1297
|
-
|
|
1298
|
-
test("visible source escapes remain readable and searchable while real CRLF stays preserved", async () => {
|
|
1299
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-source-escapes-"));
|
|
1300
|
-
try {
|
|
1301
|
-
const path = join(directory, "escapes.ts");
|
|
1302
|
-
const code = 'const eol = "\\r\\n";\r\n';
|
|
1303
|
-
await invoke(
|
|
1304
|
-
makeWriteOverride(directory),
|
|
1305
|
-
"write",
|
|
1306
|
-
{ path, content: code, mode: "create" },
|
|
1307
|
-
undefined,
|
|
1308
|
-
undefined,
|
|
1309
|
-
{ cwd: directory } as ExtensionContext,
|
|
1310
|
-
);
|
|
1311
|
-
const read = await invoke(
|
|
1312
|
-
makeReadOverride(directory, DEFAULT_CONFIG),
|
|
1313
|
-
"read",
|
|
1314
|
-
{ path },
|
|
1315
|
-
undefined,
|
|
1316
|
-
undefined,
|
|
1317
|
-
{
|
|
1318
|
-
cwd: directory,
|
|
1319
|
-
} as ExtensionContext,
|
|
1320
|
-
);
|
|
1321
|
-
assert.ok(read.content[0].type === "text");
|
|
1322
|
-
assert.ok(read.content[0].text.includes(String.raw`const eol = "\r\n";`));
|
|
1323
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1324
|
-
for (const query of [
|
|
1325
|
-
{ pattern: String.raw`\r\n`, literal: true },
|
|
1326
|
-
{ pattern: String.raw`\\r\\n`, literal: false },
|
|
1327
|
-
]) {
|
|
1328
|
-
const result = await invoke(grep, "grep", { path, ...query }, undefined, undefined);
|
|
1329
|
-
assert.ok(result.content[0].text.includes(String.raw`const eol = "\r\n";`));
|
|
1330
|
-
}
|
|
1331
|
-
const replace = makeReplaceTool(directory, DEFAULT_CONFIG);
|
|
1332
|
-
await invoke(
|
|
1333
|
-
replace,
|
|
1334
|
-
"literal",
|
|
1335
|
-
{ path, replacements: [{ literal: true, find: String.raw`\r\n`, replace: String.raw`\n` }] },
|
|
1336
|
-
undefined,
|
|
1337
|
-
undefined,
|
|
1338
|
-
{ cwd: directory } as ExtensionContext,
|
|
1339
|
-
);
|
|
1340
|
-
assert.equal(await readFile(path, "utf8"), 'const eol = "\\n";\r\n');
|
|
1341
|
-
await invoke(
|
|
1342
|
-
replace,
|
|
1343
|
-
"regex",
|
|
1344
|
-
{
|
|
1345
|
-
path,
|
|
1346
|
-
replacements: [{ find: String.raw`\\n`, replace: String.raw`\r\n`, literal: false }],
|
|
1347
|
-
},
|
|
1348
|
-
undefined,
|
|
1349
|
-
undefined,
|
|
1350
|
-
{ cwd: directory } as ExtensionContext,
|
|
1351
|
-
);
|
|
1352
|
-
assert.equal(await readFile(path, "utf8"), code);
|
|
1353
|
-
} finally {
|
|
1354
|
-
await rm(directory, { recursive: true, force: true });
|
|
1355
|
-
}
|
|
1356
|
-
});
|
|
1357
|
-
|
|
1358
|
-
test("grep retains original paths and ignores unmatched invalid UTF-8 across many files", async () => {
|
|
1359
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-many-files-"));
|
|
1360
|
-
try {
|
|
1361
|
-
await Promise.all(
|
|
1362
|
-
Array.from({ length: 70 }, (_, i) => writeFile(join(directory, `file ${i}.txt`), "--\r\n")),
|
|
1363
|
-
);
|
|
1364
|
-
await writeFile(join(directory, "invalid.txt"), Buffer.from([0xc3, 0x28]));
|
|
1365
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1366
|
-
const result = await invoke(
|
|
1367
|
-
grep,
|
|
1368
|
-
"grep",
|
|
1369
|
-
{ path: directory, pattern: "--", literal: true, outputMode: "count", limit: 100 },
|
|
1370
|
-
undefined,
|
|
1371
|
-
undefined,
|
|
1372
|
-
);
|
|
1373
|
-
assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
|
|
1374
|
-
assert.doesNotMatch(result.content[0].text, /invalid\.txt|UNSUPPORTED_ENCODING/);
|
|
1375
|
-
assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
|
|
1376
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1377
|
-
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1378
|
-
} finally {
|
|
1379
|
-
await rm(directory, { recursive: true, force: true });
|
|
1380
|
-
}
|
|
1381
|
-
});
|
|
1382
|
-
|
|
1383
|
-
test("NUL files are silently skipped across search scopes and output modes", async () => {
|
|
1384
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-nul-"));
|
|
1385
|
-
try {
|
|
1386
|
-
const binary = join(directory, "binary.txt");
|
|
1387
|
-
const source = Buffer.concat([
|
|
1388
|
-
Buffer.from("needle\r\n"),
|
|
1389
|
-
Buffer.alloc(65536, 97),
|
|
1390
|
-
Buffer.from("\0needle\n"),
|
|
1391
|
-
]);
|
|
1392
|
-
await writeFile(binary, source);
|
|
1393
|
-
await writeFile(join(directory, "valid.txt"), "needle\n");
|
|
1394
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1395
|
-
for (const outputMode of ["content", "files", "count"] as const) {
|
|
1396
|
-
for (const multiline of [false, true]) {
|
|
1397
|
-
const skipped = await invoke(
|
|
1398
|
-
grep,
|
|
1399
|
-
"binary",
|
|
1400
|
-
{ path: binary, pattern: "needle", literal: true, outputMode, multiline },
|
|
1401
|
-
undefined,
|
|
1402
|
-
undefined,
|
|
1403
|
-
);
|
|
1404
|
-
assert.equal(grepCounts(skipped).matches, 0);
|
|
1405
|
-
assert.equal(reportOf(skipped, "grep")?.diagnostics, undefined);
|
|
1406
|
-
const result = await invoke(
|
|
1407
|
-
grep,
|
|
1408
|
-
"directory",
|
|
1409
|
-
{ path: directory, pattern: "needle", literal: true, outputMode, multiline, limit: 1 },
|
|
1410
|
-
undefined,
|
|
1411
|
-
undefined,
|
|
1412
|
-
);
|
|
1413
|
-
assert.match(result.content[0].text, /valid\.txt/);
|
|
1414
|
-
assert.doesNotMatch(result.content[0].text, /binary\.txt|UNSUPPORTED_TEXT/);
|
|
1415
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1416
|
-
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
assert.deepEqual(await readFile(binary), source);
|
|
1420
|
-
} finally {
|
|
1421
|
-
await rm(directory, { recursive: true, force: true });
|
|
1422
|
-
}
|
|
1423
|
-
});
|
|
1424
|
-
|
|
1425
|
-
test("invalid UTF-8 matches use plain previews while valid files retain edit anchors", async () => {
|
|
1426
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-preview-"));
|
|
1427
|
-
try {
|
|
1428
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1429
|
-
const invalid = join(directory, "invalid.txt");
|
|
1430
|
-
for (const separator of ["\n", "\r\n"]) {
|
|
1431
|
-
const source = Buffer.concat([
|
|
1432
|
-
Buffer.from(`before${separator}needle `),
|
|
1433
|
-
Buffer.from([0xff]),
|
|
1434
|
-
Buffer.from(`${separator}after${separator}`),
|
|
1435
|
-
]);
|
|
1436
|
-
await writeFile(invalid, source);
|
|
1437
|
-
const result = await invoke(
|
|
1438
|
-
grep,
|
|
1439
|
-
"preview",
|
|
1440
|
-
{ path: invalid, pattern: "needle", literal: true, context: 1 },
|
|
1441
|
-
undefined,
|
|
1442
|
-
undefined,
|
|
1443
|
-
);
|
|
1444
|
-
assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
|
|
1445
|
-
assert.deepEqual(reportOf(result, "grep")?.invalidUtf8, ["invalid.txt"]);
|
|
1446
|
-
assert.doesNotMatch(result.content[0].text, /\d+#/);
|
|
1447
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1448
|
-
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1449
|
-
assert.deepEqual(await readFile(invalid), source);
|
|
1450
|
-
}
|
|
1451
|
-
await writeFile(join(directory, "valid.txt"), "needle\n");
|
|
1452
|
-
const mixed = await invoke(
|
|
1453
|
-
grep,
|
|
1454
|
-
"mixed",
|
|
1455
|
-
{ path: directory, pattern: "needle", literal: true },
|
|
1456
|
-
undefined,
|
|
1457
|
-
undefined,
|
|
1458
|
-
);
|
|
1459
|
-
assert.match(mixed.content[0].text, /valid\.txt · 1 match\n1#[A-Z0-9]+│needle/);
|
|
1460
|
-
assert.match(mixed.content[0].text, /invalid\.txt · 1 match\n2│needle �/);
|
|
1461
|
-
for (const outputMode of ["files", "count"] as const) {
|
|
1462
|
-
const result = await invoke(
|
|
1463
|
-
grep,
|
|
1464
|
-
"summary",
|
|
1465
|
-
{ path: directory, pattern: "needle", literal: true, outputMode },
|
|
1466
|
-
undefined,
|
|
1467
|
-
undefined,
|
|
1468
|
-
);
|
|
1469
|
-
assert.match(result.content[0].text, /invalid\.txt/);
|
|
1470
|
-
assert.match(result.content[0].text, /valid\.txt/);
|
|
1471
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1472
|
-
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1473
|
-
}
|
|
1474
|
-
const multiline = await invoke(
|
|
1475
|
-
grep,
|
|
1476
|
-
"multiline",
|
|
1477
|
-
{ path: invalid, pattern: "needle[\\w ]*\\nafter", literal: false, multiline: true },
|
|
1478
|
-
undefined,
|
|
1479
|
-
undefined,
|
|
1480
|
-
);
|
|
1481
|
-
// The explicit class excludes the replacement character in the lossy view.
|
|
1482
|
-
assert.equal(grepCounts(multiline).matches, 0);
|
|
1483
|
-
} finally {
|
|
1484
|
-
await rm(directory, { recursive: true, force: true });
|
|
1485
|
-
}
|
|
1486
|
-
});
|
|
1487
|
-
|
|
1488
|
-
test("grep paths with input shorthand or literal separators still edit the searched file", async (t) => {
|
|
1489
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-path-roundtrip-"));
|
|
1490
|
-
try {
|
|
1491
|
-
const cases = [
|
|
1492
|
-
{ name: "@config.txt", other: "config.txt" },
|
|
1493
|
-
{ name: "space\u00a0file.txt", other: "space file.txt" },
|
|
1494
|
-
...(process.platform === "win32"
|
|
1495
|
-
? []
|
|
1496
|
-
: [{ name: "dir\\target.txt", other: "dir/target.txt" }]),
|
|
1497
|
-
];
|
|
1498
|
-
for (const { name, other } of cases) {
|
|
1499
|
-
await t.test(name, async () => {
|
|
1500
|
-
const file = join(directory, name);
|
|
1501
|
-
const otherFile = join(directory, other);
|
|
1502
|
-
await mkdir(dirname(otherFile), { recursive: true });
|
|
1503
|
-
await writeFile(file, "needle\n");
|
|
1504
|
-
await writeFile(otherFile, "needle\n");
|
|
1505
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1506
|
-
let content = "";
|
|
1507
|
-
let returnedPath = "";
|
|
1508
|
-
for (const outputMode of ["content", "files", "count"] as const) {
|
|
1509
|
-
const result = await invoke(grep, "path", {
|
|
1510
|
-
path: pathToFileURL(file).href,
|
|
1511
|
-
pattern: "needle",
|
|
1512
|
-
literal: true,
|
|
1513
|
-
outputMode,
|
|
1514
|
-
});
|
|
1515
|
-
const text = result.content[0].text;
|
|
1516
|
-
const firstLine = text.split("\n")[0];
|
|
1517
|
-
returnedPath =
|
|
1518
|
-
outputMode === "content"
|
|
1519
|
-
? firstLine.slice(0, firstLine.lastIndexOf(" · "))
|
|
1520
|
-
: outputMode === "count"
|
|
1521
|
-
? firstLine.slice(0, firstLine.lastIndexOf(": "))
|
|
1522
|
-
: firstLine;
|
|
1523
|
-
assert.equal(canonicalPath(directory, returnedPath), file, outputMode);
|
|
1524
|
-
if (outputMode === "content") content = text;
|
|
1525
|
-
}
|
|
1526
|
-
const anchor = content.match(/(1#[0-9A-Z]+)│needle/)?.[1];
|
|
1527
|
-
assert.ok(anchor);
|
|
1528
|
-
await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
|
|
1529
|
-
path: returnedPath,
|
|
1530
|
-
steps: [{ action: "replace", anchor, lines: ["updated"] }],
|
|
1531
|
-
});
|
|
1532
|
-
assert.equal(await readFile(file, "utf8"), "updated\n");
|
|
1533
|
-
assert.equal(await readFile(otherFile, "utf8"), "needle\n");
|
|
1534
|
-
});
|
|
1535
|
-
}
|
|
1536
|
-
} finally {
|
|
1537
|
-
await rm(directory, { recursive: true, force: true });
|
|
1538
|
-
}
|
|
1539
|
-
});
|
|
1540
|
-
|
|
1541
|
-
test("Windows localhost share → grep paths round-trip and native read opens the target", {
|
|
1542
|
-
skip: process.platform !== "win32",
|
|
1543
|
-
}, async (t) => {
|
|
1544
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-localhost-path-"));
|
|
1545
|
-
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
1546
|
-
const { root } = parse(directory);
|
|
1547
|
-
const share = join(`//localhost/${root[0]}$`, directory.slice(root.length));
|
|
1548
|
-
try {
|
|
1549
|
-
await stat(share);
|
|
1550
|
-
} catch (error) {
|
|
1551
|
-
if (
|
|
1552
|
-
error instanceof Error &&
|
|
1553
|
-
"code" in error &&
|
|
1554
|
-
["ENOENT", "EACCES", "EPERM"].includes(String(error.code))
|
|
1555
|
-
) {
|
|
1556
|
-
t.skip("The temporary directory is not accessible through the local administrative share");
|
|
1557
|
-
return;
|
|
1558
|
-
}
|
|
1559
|
-
throw error;
|
|
1560
|
-
}
|
|
1561
|
-
await writeFile(join(directory, "space\u202fname.txt"), "needle\n");
|
|
1562
|
-
await writeFile(join(directory, "data.bin"), "original\0bytes\n");
|
|
1563
|
-
const read = makeReadOverride(share, DEFAULT_CONFIG);
|
|
1564
|
-
await t.test("grep output identifies the exact text file", async () => {
|
|
1565
|
-
const result = await callTool(makeGrepOverride(share, DEFAULT_CONFIG), {
|
|
1566
|
-
pattern: ["needle"],
|
|
1567
|
-
literal: true,
|
|
1568
|
-
outputMode: "files",
|
|
1569
|
-
});
|
|
1570
|
-
assert.equal(result.isError, undefined);
|
|
1571
|
-
const body = result.content[0];
|
|
1572
|
-
assert.ok(body?.type === "text");
|
|
1573
|
-
const reread = await callTool(read, { path: body.text, anchors: false });
|
|
1574
|
-
assert.equal(reread.isError, undefined, JSON.stringify(reread.content));
|
|
1575
|
-
const text = reread.content[0];
|
|
1576
|
-
assert.ok(text?.type === "text" && text.text.endsWith("\nneedle"));
|
|
1577
|
-
});
|
|
1578
|
-
await t.test("NUL file is read through native read", async () => {
|
|
1579
|
-
const result = await callTool(read, { path: "data.bin" });
|
|
1580
|
-
assert.equal(result.isError, undefined, JSON.stringify(result.content));
|
|
1581
|
-
assert.equal(result.details?.nativeRead, true);
|
|
1582
|
-
assert.deepEqual(result.content[0], { type: "text", text: "original\0bytes\n" });
|
|
1583
|
-
});
|
|
1584
|
-
});
|
|
1585
|
-
|
|
1586
|
-
test("literal LF and CRLF queries match original valid and invalid UTF-8 files alike", async (t) => {
|
|
1587
|
-
const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-crlf-"));
|
|
1588
|
-
try {
|
|
1589
|
-
await writeFile(join(directory, "valid.txt"), "alpha\r\nbeta\r\n");
|
|
1590
|
-
await writeFile(
|
|
1591
|
-
join(directory, "invalid.txt"),
|
|
1592
|
-
Buffer.concat([Buffer.from("alpha\r\nbeta\r\n"), Buffer.from([0xff])]),
|
|
1593
|
-
);
|
|
1594
|
-
const grep = makeGrepOverride(directory, DEFAULT_CONFIG);
|
|
1595
|
-
for (const outputMode of ["content", "files", "count"] as const) {
|
|
1596
|
-
await t.test(outputMode, async () => {
|
|
1597
|
-
const result = await invoke(grep, "crlf", {
|
|
1598
|
-
path: directory,
|
|
1599
|
-
pattern: "alpha\r\nbeta",
|
|
1600
|
-
literal: true,
|
|
1601
|
-
multiline: true,
|
|
1602
|
-
outputMode,
|
|
1603
|
-
});
|
|
1604
|
-
assert.match(result.content[0].text, /(?:^|\n)valid\.txt/);
|
|
1605
|
-
assert.match(result.content[0].text, /(?:^|\n)invalid\.txt/);
|
|
1606
|
-
if (outputMode === "content") {
|
|
1607
|
-
assert.match(result.content[0].text, /1│alpha\n2│beta/);
|
|
1608
|
-
assert.match(result.content[0].text, /1#[0-9A-Z]+│alpha\n2#[0-9A-Z]+│beta/);
|
|
1609
|
-
} else if (outputMode === "count") {
|
|
1610
|
-
assert.equal(grepCounts(result).matches, 4);
|
|
1611
|
-
assert.equal(grepCounts(result).files, 2);
|
|
1612
|
-
}
|
|
1613
|
-
assert.equal(reportOf(result, "grep")?.diagnostics, undefined);
|
|
1614
|
-
assert.equal(reportOf(result, "grep")?.scanLimit, undefined);
|
|
1615
|
-
const lf = await invoke(grep, "lf", {
|
|
1616
|
-
path: directory,
|
|
1617
|
-
pattern: "alpha\nbeta",
|
|
1618
|
-
literal: true,
|
|
1619
|
-
multiline: true,
|
|
1620
|
-
outputMode,
|
|
1621
|
-
});
|
|
1622
|
-
assert.match(lf.content[0].text, /valid\.txt/);
|
|
1623
|
-
assert.match(lf.content[0].text, /invalid\.txt/);
|
|
1624
|
-
});
|
|
1625
|
-
}
|
|
1626
|
-
} finally {
|
|
1627
|
-
await rm(directory, { recursive: true, force: true });
|
|
1628
|
-
}
|
|
1629
|
-
});
|
|
1630
|
-
|
|
1631
|
-
test("oversized candidate path record → bounded runner rejects before invoking its consumer", async (t) => {
|
|
1632
|
-
const spawn = childProcess.spawn;
|
|
1633
|
-
t.mock.method(childProcess, "spawn", (...args: Parameters<typeof spawn>) =>
|
|
1634
|
-
spawn(process.execPath, args[1], args[2]),
|
|
1635
|
-
);
|
|
1636
|
-
syncBuiltinESMExports();
|
|
1637
|
-
t.after(() => {
|
|
1638
|
-
t.mock.restoreAll();
|
|
1639
|
-
syncBuiltinESMExports();
|
|
1640
|
-
});
|
|
1641
|
-
let called = false;
|
|
1642
|
-
await assert.rejects(
|
|
1643
|
-
runRgPaths(
|
|
1644
|
-
[
|
|
1645
|
-
"--input-type=module",
|
|
1646
|
-
"--eval",
|
|
1647
|
-
"process.stdout.write(Buffer.alloc(Number(process.argv[1]), 97))",
|
|
1648
|
-
String(MAX_RG_RECORD_BYTES + 1),
|
|
1649
|
-
],
|
|
1650
|
-
AbortSignal.timeout(10_000),
|
|
1651
|
-
() => {
|
|
1652
|
-
called = true;
|
|
1653
|
-
return true;
|
|
1654
|
-
},
|
|
1655
|
-
),
|
|
1656
|
-
(error: unknown) => error instanceof Error && error.message.includes("record exceeds"),
|
|
1657
|
-
);
|
|
1658
|
-
assert.equal(called, false);
|
|
1659
|
-
});
|