Skip to content

Commit 094a35e

Browse files
authored
fix: Decode grep context arrays in fff-node (#321)
* fix: Decode grep context arrays in fff-node * test: Normalize Windows paths in fff-node grep context e2e
1 parent 697481f commit 094a35e

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

packages/fff-node/src/ffi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ function readCStringArray(ptrArray: JsExternal, count: number): string[] {
627627
const elemPtr = ptrOffset(ptrArray, i * 8);
628628
const [charPtr] = restorePointer({
629629
retType: [DataType.External],
630-
paramsValue: wrapPointer([elemPtr]),
630+
paramsValue: [elemPtr],
631631
}) as unknown as [JsExternal];
632632
result.push(readCString(charPtr) ?? "");
633633
}

packages/fff-node/test/e2e.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { FileFinder, closeLibrary } from "../dist/src/index.js";
2020

2121
const __dirname = dirname(fileURLToPath(import.meta.url));
2222
const REPO_ROOT = resolve(__dirname, "..", "..", "..");
23+
const normalizePath = (p) => p.replace(/\\/g, "/");
2324

2425
/** @type {import("../dist/src/finder.js").FileFinder | null} */
2526
let finder = null;
@@ -167,6 +168,31 @@ describe("fff-node", { concurrency: 1 }, () => {
167168
assert.ok(r.ok, `regex grep failed: ${!r.ok ? r.error : ""}`);
168169
assert.ok(r.value.items.length > 0);
169170
});
171+
172+
it("decodes before/after context lines", () => {
173+
const r = finder.grep(
174+
"match.contextBefore = readCStringArray(raw.context_before, raw.context_before_count);",
175+
{
176+
mode: "plain",
177+
beforeContext: 1,
178+
afterContext: 1,
179+
maxMatchesPerFile: 5,
180+
},
181+
);
182+
assert.ok(r.ok, `grep with context failed: ${!r.ok ? r.error : ""}`);
183+
184+
const match = r.value.items.find(
185+
(m) => normalizePath(m.relativePath) === "packages/fff-node/src/ffi.ts",
186+
);
187+
assert.ok(
188+
match,
189+
`expected a match in packages/fff-node/src/ffi.ts, got: ${r.value.items
190+
.map((m) => normalizePath(m.relativePath))
191+
.join(", ")}`,
192+
);
193+
assert.deepEqual(match.contextBefore, [" if (raw.context_before_count > 0) {"]);
194+
assert.deepEqual(match.contextAfter, [" }"]);
195+
});
170196
});
171197

172198
describe("multiGrep", { concurrency: 1 }, () => {

0 commit comments

Comments
 (0)