|
| 1 | +/** |
| 2 | + * JSON output tests imported from ripgrep |
| 3 | + * |
| 4 | + * Source: https://github.com/BurntSushi/ripgrep/blob/master/tests/json.rs |
| 5 | + * |
| 6 | + * Not implemented (tests not imported): |
| 7 | + * - notutf8, notutf8_file: Non-UTF8 file handling not supported |
| 8 | + * - crlf, r1095_missing_crlf, r1095_crlf_empty_match: --crlf flag not implemented |
| 9 | + * - r1412_look_behind_match_missing: Requires PCRE2 look-behind |
| 10 | + */ |
| 11 | + |
| 12 | +import { describe, expect, it } from "vitest"; |
| 13 | +import { Bash } from "../../../Bash.js"; |
| 14 | + |
| 15 | +const SHERLOCK = `For the Doctor Watsons of this world, as opposed to the Sherlock |
| 16 | +Holmeses, success in the province of detective work must always |
| 17 | +be, to a very large extent, the result of luck. Sherlock Holmes |
| 18 | +can extract a clew from a wisp of straw or a flake of cigar ash; |
| 19 | +but Doctor Watson has to have it taken out for him and dusted, |
| 20 | +and exhibited clearly, with a label attached. |
| 21 | +`; |
| 22 | + |
| 23 | +interface JsonMessage { |
| 24 | + type: "begin" | "end" | "match" | "context" | "summary"; |
| 25 | + data: Record<string, unknown>; |
| 26 | +} |
| 27 | + |
| 28 | +function parseJsonLines(output: string): JsonMessage[] { |
| 29 | + return output |
| 30 | + .trim() |
| 31 | + .split("\n") |
| 32 | + .filter((line) => line.length > 0) |
| 33 | + .map((line) => JSON.parse(line) as JsonMessage); |
| 34 | +} |
| 35 | + |
| 36 | +describe("rg json: basic", () => { |
| 37 | + it("basic: JSON output structure", async () => { |
| 38 | + const bash = new Bash({ |
| 39 | + cwd: "/home/user", |
| 40 | + files: { |
| 41 | + "/home/user/sherlock": SHERLOCK, |
| 42 | + }, |
| 43 | + }); |
| 44 | + const result = await bash.exec("rg --json 'Sherlock Holmes' sherlock"); |
| 45 | + expect(result.exitCode).toBe(0); |
| 46 | + |
| 47 | + const msgs = parseJsonLines(result.stdout); |
| 48 | + |
| 49 | + // Check begin message |
| 50 | + expect(msgs[0].type).toBe("begin"); |
| 51 | + expect(msgs[0].data.path).toEqual({ text: "sherlock" }); |
| 52 | + |
| 53 | + // Check match message |
| 54 | + expect(msgs[1].type).toBe("match"); |
| 55 | + expect(msgs[1].data.path).toEqual({ text: "sherlock" }); |
| 56 | + expect(msgs[1].data.lines).toEqual({ |
| 57 | + text: "be, to a very large extent, the result of luck. Sherlock Holmes\n", |
| 58 | + }); |
| 59 | + expect(msgs[1].data.line_number).toBe(3); |
| 60 | + expect(msgs[1].data.absolute_offset).toBe(129); |
| 61 | + const submatches = msgs[1].data.submatches as Array<{ |
| 62 | + match: { text: string }; |
| 63 | + start: number; |
| 64 | + end: number; |
| 65 | + }>; |
| 66 | + expect(submatches.length).toBe(1); |
| 67 | + expect(submatches[0].match).toEqual({ text: "Sherlock Holmes" }); |
| 68 | + expect(submatches[0].start).toBe(48); |
| 69 | + expect(submatches[0].end).toBe(63); |
| 70 | + |
| 71 | + // Check end message |
| 72 | + expect(msgs[2].type).toBe("end"); |
| 73 | + expect(msgs[2].data.path).toEqual({ text: "sherlock" }); |
| 74 | + expect(msgs[2].data.binary_offset).toBeNull(); |
| 75 | + |
| 76 | + // Check summary message |
| 77 | + expect(msgs[3].type).toBe("summary"); |
| 78 | + const stats = msgs[3].data.stats as { searches_with_match: number }; |
| 79 | + expect(stats.searches_with_match).toBe(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it("replacement: JSON output with replacement text", async () => { |
| 83 | + const bash = new Bash({ |
| 84 | + cwd: "/home/user", |
| 85 | + files: { |
| 86 | + "/home/user/sherlock": SHERLOCK, |
| 87 | + }, |
| 88 | + }); |
| 89 | + const result = await bash.exec( |
| 90 | + "rg --json 'Sherlock Holmes' -r 'John Watson' sherlock", |
| 91 | + ); |
| 92 | + expect(result.exitCode).toBe(0); |
| 93 | + |
| 94 | + const msgs = parseJsonLines(result.stdout); |
| 95 | + expect(msgs[1].type).toBe("match"); |
| 96 | + const submatches = msgs[1].data.submatches as Array<{ |
| 97 | + match: { text: string }; |
| 98 | + replacement: { text: string }; |
| 99 | + }>; |
| 100 | + expect(submatches[0].replacement).toEqual({ text: "John Watson" }); |
| 101 | + }); |
| 102 | + |
| 103 | + it("quiet_stats: JSON with --quiet shows only summary", async () => { |
| 104 | + const bash = new Bash({ |
| 105 | + cwd: "/home/user", |
| 106 | + files: { |
| 107 | + "/home/user/sherlock": SHERLOCK, |
| 108 | + }, |
| 109 | + }); |
| 110 | + const result = await bash.exec( |
| 111 | + "rg --json --quiet 'Sherlock Holmes' sherlock", |
| 112 | + ); |
| 113 | + expect(result.exitCode).toBe(0); |
| 114 | + // ripgrep behavior: --quiet --json outputs only the summary |
| 115 | + const msgs = parseJsonLines(result.stdout); |
| 116 | + expect(msgs.length).toBe(1); |
| 117 | + expect(msgs[0].type).toBe("summary"); |
| 118 | + const stats = msgs[0].data.stats as { searches_with_match: number }; |
| 119 | + expect(stats.searches_with_match).toBe(1); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("rg json: multiple matches", () => { |
| 124 | + it("should output all matches with correct submatches", async () => { |
| 125 | + const bash = new Bash({ |
| 126 | + cwd: "/home/user", |
| 127 | + files: { |
| 128 | + "/home/user/test.txt": "foo bar foo baz foo\n", |
| 129 | + }, |
| 130 | + }); |
| 131 | + const result = await bash.exec("rg --json foo test.txt"); |
| 132 | + expect(result.exitCode).toBe(0); |
| 133 | + |
| 134 | + const msgs = parseJsonLines(result.stdout); |
| 135 | + const match = msgs.find((m) => m.type === "match"); |
| 136 | + expect(match).toBeDefined(); |
| 137 | + |
| 138 | + const submatches = match?.data.submatches as Array<{ |
| 139 | + match: { text: string }; |
| 140 | + start: number; |
| 141 | + end: number; |
| 142 | + }>; |
| 143 | + expect(submatches.length).toBe(3); |
| 144 | + expect(submatches[0]).toEqual({ match: { text: "foo" }, start: 0, end: 3 }); |
| 145 | + expect(submatches[1]).toEqual({ |
| 146 | + match: { text: "foo" }, |
| 147 | + start: 8, |
| 148 | + end: 11, |
| 149 | + }); |
| 150 | + expect(submatches[2]).toEqual({ |
| 151 | + match: { text: "foo" }, |
| 152 | + start: 16, |
| 153 | + end: 19, |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it("should output multiple files with begin/end messages", async () => { |
| 158 | + const bash = new Bash({ |
| 159 | + cwd: "/home/user", |
| 160 | + files: { |
| 161 | + "/home/user/a.txt": "hello\n", |
| 162 | + "/home/user/b.txt": "hello\n", |
| 163 | + }, |
| 164 | + }); |
| 165 | + const result = await bash.exec("rg --json hello"); |
| 166 | + expect(result.exitCode).toBe(0); |
| 167 | + |
| 168 | + const msgs = parseJsonLines(result.stdout); |
| 169 | + |
| 170 | + // Should have begin/match/end for each file plus summary |
| 171 | + const begins = msgs.filter((m) => m.type === "begin"); |
| 172 | + const ends = msgs.filter((m) => m.type === "end"); |
| 173 | + const matches = msgs.filter((m) => m.type === "match"); |
| 174 | + const summaries = msgs.filter((m) => m.type === "summary"); |
| 175 | + |
| 176 | + expect(begins.length).toBe(2); |
| 177 | + expect(ends.length).toBe(2); |
| 178 | + expect(matches.length).toBe(2); |
| 179 | + expect(summaries.length).toBe(1); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe("rg json: edge cases", () => { |
| 184 | + it("should output summary even with no matches", async () => { |
| 185 | + const bash = new Bash({ |
| 186 | + cwd: "/home/user", |
| 187 | + files: { |
| 188 | + "/home/user/test.txt": "hello world\n", |
| 189 | + }, |
| 190 | + }); |
| 191 | + const result = await bash.exec("rg --json notfound"); |
| 192 | + expect(result.exitCode).toBe(1); |
| 193 | + |
| 194 | + // Always outputs summary with stats |
| 195 | + const msgs = parseJsonLines(result.stdout); |
| 196 | + const summary = msgs.find((m) => m.type === "summary"); |
| 197 | + expect(summary).toBeDefined(); |
| 198 | + const stats = summary?.data.stats as { searches_with_match: number }; |
| 199 | + expect(stats.searches_with_match).toBe(0); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should handle empty file with summary", async () => { |
| 203 | + const bash = new Bash({ |
| 204 | + cwd: "/home/user", |
| 205 | + files: { |
| 206 | + "/home/user/empty.txt": "", |
| 207 | + }, |
| 208 | + }); |
| 209 | + const result = await bash.exec("rg --json foo empty.txt"); |
| 210 | + expect(result.exitCode).toBe(1); |
| 211 | + |
| 212 | + const msgs = parseJsonLines(result.stdout); |
| 213 | + const summary = msgs.find((m) => m.type === "summary"); |
| 214 | + expect(summary).toBeDefined(); |
| 215 | + const stats = summary?.data.stats as { |
| 216 | + searches_with_match: number; |
| 217 | + bytes_searched: number; |
| 218 | + }; |
| 219 | + expect(stats.searches_with_match).toBe(0); |
| 220 | + expect(stats.bytes_searched).toBe(0); |
| 221 | + }); |
| 222 | +}); |
0 commit comments