Files
MyGolandRelease/internal/app/wscheck_test.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

119 lines
3.3 KiB
Go

package app
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestParseVetOutput(t *testing.T) {
root := filepath.Join("C:", "work", "proj")
out := strings.Join([]string{
"# demo/greeter",
"./greeter/greeter.go:7:6: fmt.Sprintf format %s has arg name of wrong type int",
"a.go:3: printf: non-constant format string",
"vet: something odd",
"",
}, "\n")
ps := parseVetOutput(root, out)
if len(ps) != 2 {
t.Fatalf("应解析出 2 条,实际 %d: %+v", len(ps), ps)
}
if ps[0].Line != 7 || ps[0].Col != 7 || !strings.Contains(ps[0].Text, "fmt.Sprintf") {
t.Fatalf("第一条不对: %+v", ps[0])
}
if ps[0].Path != filepath.Join(root, "greeter", "greeter.go") {
t.Fatalf("相对路径没拼对: %q", ps[0].Path)
}
if ps[0].Source != "vet" {
t.Fatalf("来源应是 vet: %q", ps[0].Source)
}
if ps[1].Line != 3 {
t.Fatalf("第二条行号不对: %+v", ps[1])
}
}
func TestParseVetOutputAbsolute(t *testing.T) {
// 必须用真的绝对路径:filepath.Join("C:", "work") 在 Windows 上是
// "C:work"(盘符相对),并不算绝对路径,会让这条断言失真。
root := t.TempDir()
abs := filepath.Join(root, "x.go")
ps := parseVetOutput(root, abs+":10:2: boom")
if len(ps) != 1 || ps[0].Path != abs {
t.Fatalf("绝对路径应原样保留: %+v", ps)
}
}
// TestWorkspaceCheckRealVet 用真 go vet 跑一遍,确认解析器吃得下真实输出。
func TestWorkspaceCheckRealVet(t *testing.T) {
if testing.Short() {
t.Skip("需要 go 工具链")
}
if _, err := exec.LookPath("go"); err != nil {
t.Skip("没有 go")
}
dir := t.TempDir()
writeTestFile(t, filepath.Join(dir, "go.mod"), "module demo\n\ngo 1.21\n")
writeTestFile(t, filepath.Join(dir, "main.go"), `package main
import "fmt"
func main() {
fmt.Printf("%s", 1)
}
`)
cmd := exec.Command("go", "vet", "./...")
cmd.Dir = dir
out, _ := cmd.CombinedOutput()
ps := parseVetOutput(dir, string(out))
if len(ps) == 0 {
t.Fatalf("go vet 应当报出格式问题,输出:\n%s", out)
}
if !strings.HasSuffix(ps[0].Path, "main.go") {
t.Fatalf("路径不对: %q", ps[0].Path)
}
if ps[0].Line != 6 {
t.Fatalf("行号应是 6,实际 %d", ps[0].Line)
}
if !strings.Contains(ps[0].Text, "Printf") {
t.Fatalf("消息不对: %q", ps[0].Text)
}
}
func writeTestFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
// TestAllProblemsDedup 同一问题既被语言服务器报、又被 vet 报时,只留一条。
func TestAllProblemsDedup(t *testing.T) {
dup := Problem{Path: "x.go", Line: 3, Text: "boom"}
a := &App{
docs: []*Doc{{problems: []Problem{dup}}},
wsState: wsCheckState{items: []Problem{
{Path: "x.go", Line: 3, Text: "boom", Source: "vet"}, // 与上面重复
{Path: "x.go", Line: 9, Text: "只有 vet 报的", Source: "vet"},
}},
}
ps := a.allProblems()
if len(ps) != 2 {
t.Fatalf("去重后应剩 2 条,实际 %d: %+v", len(ps), ps)
}
// 保留的应当是语言服务器那条(没有 Source 标记)
if ps[0].Source != "" {
t.Fatalf("重复项应保留语言服务器版本: %+v", ps[0])
}
if ps[1].Line != 9 {
t.Fatalf("第二条应是 vet 独有的: %+v", ps[1])
}
}