-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathgrep.test.ts
More file actions
167 lines (152 loc) · 5.85 KB
/
Copy pathgrep.test.ts
File metadata and controls
167 lines (152 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, expect, it } from "vitest";
import { grep } from "./grep.js";
import { mkdir } from "./mkdir.js";
import { withDB } from "./with-db.js";
import { CHUNK_SIZE, writeFile } from "./writeFile.js";
describe("grep", () => {
it("returns no matches for an empty file", async () => {
await withDB(async (db) => {
await writeFile(db, "/empty", "", {}, () => 0);
expect(await grep(db, "TODO", "/empty")).toEqual([]);
});
});
it("finds a single match in a single file", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "line one\nTODO fix me\nline three\n", {}, () => 0);
expect(await grep(db, "TODO", "/a.txt")).toEqual([
{ path: "/a.txt", line: 2, text: "TODO fix me" },
]);
});
});
it("returns multiple matches in one file", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "a TODO here\nplain\nb TODO there\n", {}, () => 0);
expect(await grep(db, "TODO", "/a.txt")).toEqual([
{ path: "/a.txt", line: 1, text: "a TODO here" },
{ path: "/a.txt", line: 3, text: "b TODO there" },
]);
});
});
it("walks a directory recursively", async () => {
await withDB(async (db) => {
mkdir(db, "/d/sub", { recursive: true }, () => 0);
await writeFile(db, "/d/a.txt", "TODO root\n", {}, () => 0);
await writeFile(db, "/d/sub/b.txt", "TODO nested\n", {}, () => 0);
await writeFile(db, "/d/sub/c.txt", "no match\n", {}, () => 0);
const matches = await grep(db, "TODO", "/d");
expect(matches.map((m) => m.path).sort()).toEqual(["/d/a.txt", "/d/sub/b.txt"]);
});
});
it("respects the ignoreCase option", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "todo\nTODO\nTodo\n", {}, () => 0);
expect((await grep(db, "TODO", "/a.txt", { ignoreCase: true })).length).toBe(3);
expect((await grep(db, "TODO", "/a.txt", { ignoreCase: false })).length).toBe(1);
expect((await grep(db, "TODO", "/a.txt")).length).toBe(1);
});
});
it("supports opt-in regular expressions and literal strings by default", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "task 12\ntask \\d+\ntask xx\n", {}, () => 0);
expect(
(await grep(db, String.raw`task \d+`, "/a.txt", { regex: true })).map(
(match) => match.line,
),
).toEqual([1]);
expect((await grep(db, String.raw`task \d+`, "/a.txt")).map((match) => match.line)).toEqual([
2,
]);
await expect(grep(db, "[", "/a.txt", { regex: true })).rejects.toThrow(
"Invalid regular expression",
);
});
});
it("returns numbered context around matches", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "one\ntwo\nTODO\nfour\nfive\n", {}, () => 0);
expect(await grep(db, "TODO", "/a.txt", { context: 1 })).toEqual([
{
path: "/a.txt",
line: 3,
text: "TODO",
context: [
{ line: 2, text: "two", isMatch: false },
{ line: 3, text: "TODO", isMatch: true },
{ line: 4, text: "four", isMatch: false },
],
},
]);
});
});
it("marks adjacent matches as matching context", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "TODO one\nTODO two\nplain\n", {}, () => 0);
const matches = await grep(db, "TODO", "/a.txt", { context: 1 });
expect(matches).toEqual([
{
path: "/a.txt",
line: 1,
text: "TODO one",
context: [
{ line: 1, text: "TODO one", isMatch: true },
{ line: 2, text: "TODO two", isMatch: true },
],
},
{
path: "/a.txt",
line: 2,
text: "TODO two",
context: [
{ line: 1, text: "TODO one", isMatch: true },
{ line: 2, text: "TODO two", isMatch: true },
{ line: 3, text: "plain", isMatch: false },
],
},
]);
if (matches[0].context === undefined || matches[1].context === undefined) {
throw new Error("expected grep context");
}
matches[0].context[0].text = "changed";
expect(matches[1].context[0].text).toBe("TODO one");
});
});
it("applies offset and limit across files in path and line order", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "TODO a1\nTODO a2\n", {}, () => 0);
await writeFile(db, "/b.txt", "TODO b1\nTODO b2\n", {}, () => 0);
expect(
(await grep(db, "TODO", "/", { offset: 1, limit: 2 })).map(
(match) => `${match.path}:${match.line}`,
),
).toEqual(["/a.txt:2", "/b.txt:1"]);
});
});
it("matches across a chunk boundary", async () => {
await withDB(async (db) => {
// Lay out a file whose line straddles the 512KiB chunk boundary.
const pad = "a".repeat(CHUNK_SIZE - 5);
const content = `${pad}TODO straddle\nafter\n`;
await writeFile(db, "/big.txt", content, {}, () => 0);
const matches = await grep(db, "TODO", "/big.txt");
expect(matches).toHaveLength(1);
expect(matches[0].path).toBe("/big.txt");
expect(matches[0].text.endsWith("TODO straddle")).toBe(true);
});
});
it("returns lines 1-indexed", async () => {
await withDB(async (db) => {
await writeFile(db, "/a.txt", "first\nsecond\nthird\n", {}, () => 0);
expect(await grep(db, "first", "/a.txt")).toEqual([
{ path: "/a.txt", line: 1, text: "first" },
]);
expect(await grep(db, "third", "/a.txt")).toEqual([
{ path: "/a.txt", line: 3, text: "third" },
]);
});
});
it("rejects ENOENT when the path is missing", async () => {
await withDB(async (db) => {
await expect(grep(db, "x", "/missing")).rejects.toMatchObject({ code: "ENOENT" });
});
});
});