Files
MyGolandRelease/internal/app/ignore_test.go
T
zerorigin 4ace9c9fb6 fix: 按评估优先级修四项(标签右键/UTF-8 截断/覆盖率输出/.gitignore)
1. 标签页右键菜单接回(回归修复)
   tabmenu.go 的 160 行实现全库零调用点,tabBar 只处理 Clicked。
   补 RightClicked → openPop(tabEntries(path)),右键时先设 active
   (菜单无标题,靠标签高亮传达「关闭左边」相对谁算)。
   新增 tabmenu_test.go 8 个单测。

2. 中文按字节截断导致乱码
   palette.go trimmed[:160]、rename.go s[:140] 会把多字节字符切断。
   trimTo 从 gitlog.go 提到 strutil.go(三个调用方共用),两处改用它;
   n<=0 返回空串。strutil_test.go 6 个单测(含真实 linePreview 路径)。

3. 覆盖率运行不再吞测试输出
   原来 lines 收集后 _ = lines 丢弃,测试失败只看到覆盖率数字。
   改用 ExitCode() 判失败,只回放 FAIL/--- FAIL/panic/# 包/file.go:12 行
   (上限 200),覆盖率页顶部给红底摘要 + 「查看测试输出」按钮。
   coverageFailLines/Summary 纯函数 + 5 个单测。

4. 索引与搜索感知 .gitignore
   新增 ignore.go:dir/、*.ext、/rootonly、带路径、**、! 取反、注释。
   内置跳过扩到 vendor/dist/build/out/target/.next(文件树仍用保守 skipDir)。
   indexState 加 truncated,快速打开底部提示「索引已截断至 N 个文件」。
   ignore_test.go 10 个单测 + 真实遍历集成测试。

验证:go vet ./... 干净;go test ./internal/... 7 包全绿;
e2e 实例 tools/tree 确认菜单 5 项齐全 + tools/shot 截图核对。
2026-11-05 15:43:33 +08:00

199 lines
5.4 KiB
Go

package app
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// TestIgnoreDirRule `dir/` 忽略任意层级下的同名目录,但不动同名文件。
func TestIgnoreDirRule(t *testing.T) {
r := parseIgnoreRules("dist/\n")
if r == nil {
t.Fatal("规则解析为空")
}
if !r.match("dist", true) {
t.Fatal("根下的 dist 目录应被忽略")
}
if !r.match("web/dist", true) {
t.Fatal("任意层级的 dist 目录都应被忽略")
}
if r.match("dist", false) {
t.Fatal("dirOnly 规则不该命中同名文件")
}
if r.match("dist2", true) {
t.Fatal("不该命中前缀相同的目录")
}
}
// TestIgnoreSuffixRule `*.log` 任意层级。
func TestIgnoreSuffixRule(t *testing.T) {
r := parseIgnoreRules("*.log\n")
for _, p := range []string{"a.log", "logs/b.log", "x/y/z.log"} {
if !r.match(p, false) {
t.Fatalf("%s 应被忽略", p)
}
}
if r.match("a.logx", false) {
t.Fatal("不该命中 a.logx")
}
// git 的通配不区分文件与目录:叫 a.log 的目录同样被忽略。
if !r.match("a.log", true) {
t.Fatal("*.log 也应命中同名目录(与 git 一致)")
}
}
// TestIgnoreRootOnly `/build` 只匹配根。
func TestIgnoreRootOnly(t *testing.T) {
r := parseIgnoreRules("/build\n")
if !r.match("build", true) {
t.Fatal("根下的 build 应被忽略")
}
if r.match("sub/build", true) {
t.Fatal("/build 不该命中子目录里的 build")
}
}
// TestIgnorePathRule 带路径的模式按段对齐。
func TestIgnorePathRule(t *testing.T) {
r := parseIgnoreRules("internal/gen/x.go\n")
if !r.match("internal/gen/x.go", false) {
t.Fatal("精确路径应命中")
}
if r.match("other/internal/gen/x.go", false) {
t.Fatal("不该后缀对齐到别处")
}
}
// TestIgnoreDoubleStar `**` 跨层通配。
func TestIgnoreDoubleStar(t *testing.T) {
r := parseIgnoreRules("**/*.pb.go\n")
for _, p := range []string{"a.pb.go", "api/a.pb.go", "api/v1/a.pb.go"} {
if !r.match(p, false) {
t.Fatalf("%s 应被 ** 命中", p)
}
}
r2 := parseIgnoreRules("testdata/**/golden.txt\n")
if !r2.match("testdata/a/golden.txt", false) {
t.Fatal("testdata/**/golden.txt 应命中深层文件")
}
}
// TestIgnoreNegate `!` 取反:后出现的规则覆盖先前的。
func TestIgnoreNegate(t *testing.T) {
r := parseIgnoreRules("*.log\n!keep.log\n")
if !r.match("a.log", false) {
t.Fatal("a.log 应被忽略")
}
if r.match("keep.log", false) {
t.Fatal("keep.log 被 ! 取反,应保留")
}
}
// TestIgnoreComments 注释与空行跳过;全是注释时返回 nil。
func TestIgnoreComments(t *testing.T) {
if r := parseIgnoreRules("# 只有注释\n\n \n"); r != nil {
t.Fatalf("全是注释应返回 nil,得到 %+v", r.rules)
}
r := parseIgnoreRules("# 注释\n*.tmp\n")
if !r.match("x.tmp", false) {
t.Fatal("注释行之后的规则应生效")
}
}
// TestIgnoreNilSafe 没有 .gitignore 时调用不能 panic。
func TestIgnoreNilSafe(t *testing.T) {
var r *ignoreRules
if r.match("a.go", false) {
t.Fatal("nil 规则应一律不忽略")
}
if loadIgnoreRules("") != nil {
t.Fatal("空 root 应返回 nil")
}
if loadIgnoreRules(filepath.Join(t.TempDir(), "nope")) != nil {
t.Fatal("目录不存在应返回 nil")
}
}
// TestLoadIgnoreRulesReal 从磁盘读 .gitignore。
func TestLoadIgnoreRulesReal(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, ".gitignore"),
[]byte("bin/\n*.exe\n"), 0o644); err != nil {
t.Fatal(err)
}
r := loadIgnoreRules(dir)
if r == nil {
t.Fatal("应读到规则")
}
if !r.match("bin", true) || !r.match("tool.exe", false) {
t.Fatal("规则未生效")
}
}
// TestBuiltinSkipDirs 内置清单:vendor 必须在里面。
func TestBuiltinSkipDirs(t *testing.T) {
for _, d := range []string{".git", "node_modules", "vendor", "dist", "build"} {
if !isBuiltinSkipDir(d) {
t.Fatalf("%s 应在内置跳过清单里", d)
}
}
if isBuiltinSkipDir("internal") || isBuiltinSkipDir("cmd") {
t.Fatal("普通源码目录不该被跳过")
}
}
// TestBuildIndexRespectsIgnores 真实遍历:vendor 与 .gitignore 里的目录都该消失。
func TestBuildIndexRespectsIgnores(t *testing.T) {
dir := t.TempDir()
mk := func(rel, content string) {
p := filepath.Join(dir, filepath.FromSlash(rel))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
mk("keep.go", "package x")
mk("internal/a.go", "package internal")
mk("vendor/github.com/x/y.go", "package y")
mk("dist/bundle.js", "// built")
mk("logs/run.txt", "log")
mk(".gitignore", "dist/\nlogs/\n")
a := &App{index: indexState{root: dir}}
a.buildIndex(dir)
// buildIndex 是异步的,等它 ready(最多 2 秒)。
deadline := time.Now().Add(2 * time.Second)
var files []string
for time.Now().Before(deadline) {
var ready bool
files, ready = a.indexSnapshot()
if ready {
break
}
time.Sleep(10 * time.Millisecond)
}
if len(files) == 0 {
t.Fatal("索引没建起来")
}
got := strings.Join(files, ",")
if strings.Contains(got, "vendor/") {
t.Fatalf("vendor 不该进索引:%v", files)
}
if strings.Contains(got, "dist/") {
t.Fatalf("dist 被 .gitignore 忽略,不该进索引:%v", files)
}
if strings.Contains(got, "logs/") {
t.Fatalf("logs 被 .gitignore 忽略,不该进索引:%v", files)
}
if !strings.Contains(got, "keep.go") || !strings.Contains(got, "internal/a.go") {
t.Fatalf("正常源码文件被误杀:%v", files)
}
}