仰望星辰工作室

gitcat

gitcat/ internal/gitx/query_test.go 6.3 KB · 179 行 原始文件
1package gitx
2
3import (
4 "strings"
5 "testing"
6 "time"
7)
8
9// makeQueryRepo 造一个含两个文件、三次提交的裸仓库,供检索类函数使用。
10func makeQueryRepo(t *testing.T) string {
11 t.Helper()
12 dir := t.TempDir()
13 if err := InitBare(dir, "main"); err != nil {
14 t.Fatalf("InitBare 失败: %v", err)
15 }
16 blob1 := runStdin(t, dir, "HelloWorld\n第二行\n", "hash-object", "-w", "--stdin")
17 blob2 := runStdin(t, dir, "package main\n", "hash-object", "-w", "--stdin")
18 // mktree 不接受带斜杠的路径,子目录要先单独建成一棵子树
19 subTree := runStdin(t, dir, "100644 blob "+blob2+"\tmain.go\n", "mktree")
20 tree := runStdin(t, dir, "100644 blob "+blob1+"\tgreet.txt\n040000 tree "+subTree+"\tsrc\n", "mktree")
21 commit := commitObject(t, dir, tree, "add greet")
22 _, _ = run(dir, "update-ref", "refs/heads/main", commit)
23 return dir
24}
25
26func TestGrepFindsLiteralMatches(t *testing.T) {
27 dir := makeQueryRepo(t)
28
29 out, truncated, err := Grep(dir, "main", "helloworld", 64<<10)
30 if err != nil {
31 t.Fatalf("Grep 失败: %v", err)
32 }
33 if truncated {
34 t.Error("结果很小,不应被截断")
35 }
36 if !strings.Contains(out, "greet.txt") || !strings.Contains(out, "HelloWorld") {
37 t.Errorf("未命中预期内容: %q", out)
38 }
39 // 中文关键词同样能命中(代码里的中文注释很常见)
40 if out, _, err = Grep(dir, "main", "第二行", 64<<10); err != nil || !strings.Contains(out, "greet.txt") {
41 t.Errorf("中文检索失败: %q err=%v", out, err)
42 }
43 // 没有命中是正常结果,不是错误
44 if out, _, err = Grep(dir, "main", "irrelevant_zzz", 64<<10); err != nil || strings.TrimSpace(out) != "" {
45 t.Errorf("无命中应返回空结果,实际 %q err=%v", out, err)
46 }
47 // 以 "-" 开头的关键词不能被当成命令行选项
48 if _, _, err = Grep(dir, "main", "-n", 64<<10); err != nil {
49 t.Errorf("以 - 开头的关键词应被当作字面量处理,实际 %v", err)
50 }
51}
52
53func TestGrepTruncatesHugeResult(t *testing.T) {
54 dir := t.TempDir()
55 if err := InitBare(dir, "main"); err != nil {
56 t.Fatalf("InitBare 失败: %v", err)
57 }
58 var sb strings.Builder
59 for i := 0; i < 4000; i++ {
60 sb.WriteString("needle in a haystack line number ")
61 sb.WriteString(strings.Repeat("x", 20))
62 sb.WriteString("\n")
63 }
64 blob := runStdin(t, dir, sb.String(), "hash-object", "-w", "--stdin")
65 tree := runStdin(t, dir, "100644 blob "+blob+"\tmany.txt\n", "mktree")
66 commit := commitObject(t, dir, tree, "many matches")
67 _, _ = run(dir, "update-ref", "refs/heads/main", commit)
68
69 out, truncated, err := Grep(dir, "main", "needle", 1024)
70 if err != nil {
71 t.Fatalf("Grep 失败: %v", err)
72 }
73 if !truncated {
74 t.Error("命中量远超上限,应标记为截断")
75 }
76 if out == "" {
77 t.Error("截断时应返回已读到的部分结果")
78 }
79}
80
81func TestPathsRespectsLimit(t *testing.T) {
82 dir := makeQueryRepo(t)
83
84 paths, truncated, err := Paths(dir, "main", 0, 64<<10)
85 if err != nil {
86 t.Fatalf("Paths 失败: %v", err)
87 }
88 if truncated {
89 t.Error("文件很少,不应截断")
90 }
91 if len(paths) != 2 {
92 t.Fatalf("应有 2 个文件,实际 %d:%v", len(paths), paths)
93 }
94 for _, want := range []string{"greet.txt", "src/main.go"} {
95 found := false
96 for _, p := range paths {
97 if p == want {
98 found = true
99 }
100 }
101 if !found {
102 t.Errorf("缺少路径 %q:%v", want, paths)
103 }
104 }
105
106 if paths, truncated, err = Paths(dir, "main", 1, 64<<10); err != nil || len(paths) != 1 || !truncated {
107 t.Errorf("条数上限未生效: len=%d truncated=%v err=%v", len(paths), truncated, err)
108 }
109 // 体积上限:刚好装下第一行,第二行被切断(半截路径会被丢弃,不能报给调用方)
110 if paths, truncated, err = Paths(dir, "main", 0, 12); err != nil {
111 t.Fatalf("Paths 失败: %v", err)
112 } else if !truncated || len(paths) != 1 || paths[0] != "greet.txt" {
113 t.Errorf("体积上限未生效: %v truncated=%v err=%v", paths, truncated, err)
114 }
115}
116
117func TestBlobLinesReadsRange(t *testing.T) {
118 dir := makeQueryRepo(t)
119
120 lines, truncated, err := BlobLines(dir, "main", "greet.txt", 1, 0, 64<<10)
121 if err != nil {
122 t.Fatalf("BlobLines 失败: %v", err)
123 }
124 if truncated || len(lines) != 2 || lines[0] != "HelloWorld" || lines[1] != "第二行" {
125 t.Errorf("读到的内容不对: %v truncated=%v", lines, truncated)
126 }
127
128 if lines, _, err = BlobLines(dir, "main", "greet.txt", 2, 2, 64<<10); err != nil || len(lines) != 1 || lines[0] != "第二行" {
129 t.Errorf("行范围未生效: %v err=%v", lines, err)
130 }
131
132 // 上限按字节裁剪:装得下第一行、装不下第二行
133 if lines, truncated, err = BlobLines(dir, "main", "greet.txt", 1, 0, 12); err != nil {
134 t.Fatalf("BlobLines 失败: %v", err)
135 } else if !truncated || len(lines) != 1 || lines[0] != "HelloWorld" {
136 t.Errorf("字节上限未生效: %v truncated=%v", lines, truncated)
137 }
138 // 上限小于单行时一行都放不下,但仍要如实标记截断而不是报错
139 if lines, truncated, err = BlobLines(dir, "main", "greet.txt", 1, 0, 3); err != nil {
140 t.Fatalf("BlobLines 失败: %v", err)
141 } else if !truncated || len(lines) != 0 {
142 t.Errorf("过小的上限: %v truncated=%v", lines, truncated)
143 }
144
145 // 越界行号返回空结果而不是报错
146 if lines, _, err = BlobLines(dir, "main", "greet.txt", 50, 60, 64<<10); err != nil || len(lines) != 0 {
147 t.Errorf("越界行号应返回空结果: %v err=%v", lines, err)
148 }
149
150 // 文件不存在必须报错,否则调用方会拿到一份"看起来正常"的空内容
151 if _, _, err = BlobLines(dir, "main", "nope.txt", 1, 0, 64<<10); err == nil {
152 t.Error("不存在的文件应返回错误")
153 }
154}
155
156// TestBlobLinesNoDeadlock 回归:提前停止读取(超长行 / 触顶)时必须关闭读端,
157// 否则 cmd.Wait 会一直等到命令超时。
158func TestBlobLinesNoDeadlock(t *testing.T) {
159 dir := t.TempDir()
160 if err := InitBare(dir, "main"); err != nil {
161 t.Fatalf("InitBare 失败: %v", err)
162 }
163 huge := strings.Repeat("A", 3<<20) // 单行 3MB,超过单行扫描上限
164 blob := runStdin(t, dir, huge, "hash-object", "-w", "--stdin")
165 tree := runStdin(t, dir, "100644 blob "+blob+"\thuge.txt\n", "mktree")
166 commit := commitObject(t, dir, tree, "huge")
167 _, _ = run(dir, "update-ref", "refs/heads/main", commit)
168
169 done := make(chan struct{})
170 go func() {
171 _, _, _ = BlobLines(dir, "main", "huge.txt", 1, 0, 1024)
172 close(done)
173 }()
174 select {
175 case <-done:
176 case <-time.After(10 * time.Second):
177 t.Fatal("BlobLines 卡死(读端未关闭)")
178 }
179}