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 截图核对。
182 lines
5.4 KiB
Go
182 lines
5.4 KiB
Go
package app
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
)
|
|
|
|
func themeForTest() *ui.Theme { return ui.DarkTheme() }
|
|
|
|
const sampleProfile = `mode: set
|
|
testdemo/calc.go:5.13,7.2 1 1
|
|
testdemo/calc.go:10.6,14.2 3 0
|
|
testdemo/util.go:3.20,5.2 2 5
|
|
`
|
|
|
|
// TestParseCoverProfile 解析 coverprofile 的块与统计。
|
|
func TestParseCoverProfile(t *testing.T) {
|
|
files := parseCoverProfile(sampleProfile)
|
|
if len(files) != 2 {
|
|
t.Fatalf("应解析出 2 个文件,实际 %d: %v", len(files), files)
|
|
}
|
|
calc := files["testdemo/calc.go"]
|
|
if len(calc.Blocks) != 2 {
|
|
t.Fatalf("calc.go 应有 2 个块,实际 %d", len(calc.Blocks))
|
|
}
|
|
// 一块命中(1 语句)、一块没命中(3 语句) → 1/4
|
|
if calc.Total != 4 || calc.Hit != 1 {
|
|
t.Fatalf("统计不对: total=%d hit=%d", calc.Total, calc.Hit)
|
|
}
|
|
if got := calc.percent(); got != 25 {
|
|
t.Fatalf("覆盖率应为 25%%,实际 %v", got)
|
|
}
|
|
if b := calc.Blocks[0]; b.Start != 5 || b.End != 7 || b.Count != 1 {
|
|
t.Fatalf("块解析不对: %+v", b)
|
|
}
|
|
}
|
|
|
|
// TestParseCoverProfileSkipsMode 第一行 mode 要跳过,空行也不能算块。
|
|
func TestParseCoverProfileSkipsMode(t *testing.T) {
|
|
files := parseCoverProfile("mode: atomic\n\n")
|
|
if len(files) != 0 {
|
|
t.Fatalf("没有块时应为空: %v", files)
|
|
}
|
|
}
|
|
|
|
// TestCoverForSuffix 覆盖报告的路径可能带模块前缀,要能按后缀对上。
|
|
func TestCoverForSuffix(t *testing.T) {
|
|
a := &App{}
|
|
a.cover.files = parseCoverProfile(sampleProfile)
|
|
if _, ok := a.coverFor(`D:\WorkSpaces\testdemo\calc.go`); !ok {
|
|
t.Fatal("应该按「包目录/文件名」后缀匹配上")
|
|
}
|
|
if _, ok := a.coverFor(`D:\WorkSpaces\other\calc.go`); ok {
|
|
t.Fatal("不同目录的同名文件不该匹配")
|
|
}
|
|
}
|
|
|
|
// TestCoverBackgrounds 命中的行绿、没命中的行红,且行号从 0 起。
|
|
func TestCoverBackgrounds(t *testing.T) {
|
|
a := &App{th: themeForTest()}
|
|
a.cover.files = parseCoverProfile(`mode: set
|
|
testdemo/calc.go:3.1,3.10 1 1
|
|
testdemo/calc.go:4.1,4.10 1 0
|
|
`)
|
|
d := a.newDoc(`D:\WorkSpaces\testdemo\calc.go`, "a\nb\nc\nd\n")
|
|
bgs := a.coverBackgrounds(d)
|
|
if len(bgs) != 2 {
|
|
t.Fatalf("应有 2 行底色,实际 %d", len(bgs))
|
|
}
|
|
byLine := map[int]int{}
|
|
for _, bg := range bgs {
|
|
byLine[bg.First] = 1
|
|
}
|
|
if _, ok := byLine[2]; !ok {
|
|
t.Fatalf("第 3 行(0 起是 2)应有着色: %+v", bgs)
|
|
}
|
|
if _, ok := byLine[3]; !ok {
|
|
t.Fatalf("第 4 行(0 起是 3)应有着色: %+v", bgs)
|
|
}
|
|
}
|
|
|
|
// TestCoverStatText 状态栏摘要。
|
|
func TestCoverStatText(t *testing.T) {
|
|
a := &App{}
|
|
a.cover.files = parseCoverProfile(sampleProfile)
|
|
if got := a.coverStatText(`D:\WorkSpaces\testdemo\calc.go`); got != "覆盖 25%" {
|
|
t.Fatalf("摘要不对: %q", got)
|
|
}
|
|
if got := a.coverStatText(`D:\WorkSpaces\testdemo\none.go`); got != "" {
|
|
t.Fatalf("没有数据的文件不该显示: %q", got)
|
|
}
|
|
}
|
|
|
|
// sampleFailOut 是一段真实的 `go test ./...` 失败输出(混合了成功包)。
|
|
const sampleFailOut = `ok testdemo/util 0.004s
|
|
--- FAIL: TestAdd (0.00s)
|
|
calc_test.go:12: 2+2 = 5, want 4
|
|
--- FAIL: TestSub/deep (0.00s)
|
|
calc_test.go:20: 1-1 = 1, want 0
|
|
FAIL
|
|
FAIL testdemo 0.006s
|
|
FAIL
|
|
`
|
|
|
|
// TestCoverageFailLines 失败行筛选:只留能说明问题的,丢掉 ok 与空行。
|
|
func TestCoverageFailLines(t *testing.T) {
|
|
lines := strings.Split(sampleFailOut, "\n")
|
|
got := coverageFailLines(lines)
|
|
|
|
var hasCase, hasLoc, hasOk bool
|
|
for _, s := range got {
|
|
if strings.HasPrefix(s, "--- FAIL: TestAdd") {
|
|
hasCase = true
|
|
}
|
|
if strings.Contains(s, "calc_test.go:12:") {
|
|
hasLoc = true
|
|
}
|
|
if strings.HasPrefix(s, "ok ") {
|
|
hasOk = true
|
|
}
|
|
}
|
|
if !hasCase || !hasLoc {
|
|
t.Fatalf("应保留用例行与断言行:%v", got)
|
|
}
|
|
if hasOk {
|
|
t.Fatalf("不该回放 ok 行:%v", got)
|
|
}
|
|
}
|
|
|
|
// TestCoverageFailLinesBuildError 编译失败也要能带出来(# 包头 + 位置行)。
|
|
func TestCoverageFailLinesBuildError(t *testing.T) {
|
|
out := "# testdemo\n./main.go:6:2: undefined: fmtx\nFAIL\ttestdemo [build failed]\n"
|
|
got := coverageFailLines(strings.Split(out, "\n"))
|
|
joined := strings.Join(got, "\n")
|
|
if !strings.Contains(joined, "undefined: fmtx") {
|
|
t.Fatalf("编译错误被丢掉:%v", got)
|
|
}
|
|
if !strings.Contains(joined, "# testdemo") {
|
|
t.Fatalf("包名行被丢掉:%v", got)
|
|
}
|
|
}
|
|
|
|
// TestCoverageFailLinesCap 超量时截断,并追加提示。
|
|
func TestCoverageFailLinesCap(t *testing.T) {
|
|
var lines []string
|
|
for i := 0; i <= coverFailCap+10; i++ {
|
|
lines = append(lines, "--- FAIL: TestX")
|
|
}
|
|
got := coverageFailLines(lines)
|
|
if len(got) > coverFailCap+1 {
|
|
t.Fatalf("未按上限截断,%d 行", len(got))
|
|
}
|
|
last := got[len(got)-1]
|
|
if !strings.Contains(last, "已截断") {
|
|
t.Fatalf("最后一行应为截断提示,得到 %q", last)
|
|
}
|
|
}
|
|
|
|
// TestCoverageFailSummary 摘要:数包、数用例、带退出码。
|
|
func TestCoverageFailSummary(t *testing.T) {
|
|
got := coverageFailSummary(1, strings.Split(sampleFailOut, "\n"))
|
|
if !strings.Contains(got, "1 个包失败") {
|
|
t.Fatalf("包计数不对:%q", got)
|
|
}
|
|
if !strings.Contains(got, "2 个用例失败") {
|
|
t.Fatalf("用例计数不对:%q", got)
|
|
}
|
|
if !strings.Contains(got, "退出码 1") {
|
|
t.Fatalf("缺退出码:%q", got)
|
|
}
|
|
}
|
|
|
|
// TestCoverageFailSummaryNoDetail 输出里什么都没有时也要给一句可读的话。
|
|
func TestCoverageFailSummaryNoDetail(t *testing.T) {
|
|
got := coverageFailSummary(2, nil)
|
|
if !strings.Contains(got, "go test 未通过") || !strings.Contains(got, "退出码 2") {
|
|
t.Fatalf("兜底文案不对:%q", got)
|
|
}
|
|
}
|