P3 测试开关(internal/app/testflags.go)
测试面板顶部加 -race / -bench 会话级 checkbox(不持久化,-race 会拖慢测试)。
参数经 withTestFlags 统一注入结果树 / 输出面板 / 覆盖率三个 go test 入口。
-bench 配 -run=^$,但调用方已有 -run(运行光标处测试)时不覆盖。
勾 -bench 时「全部测试」改走输出面板,-json 会把 ns/op 埋在 output 行里看不清。
4 条参数组合单测。
P4 三层守门
verify.cmd:gofmt / vet / test 一道跑,纯 ASCII(cmd 按 GBK 读 UTF-8 中文
注释会把中文当命令执行,实测踩过)。用 findstr 判 gofmt 输出非空。
build.cmd verify:自检通过才构建;build.cmd 不带参数仍直接构建。
.gitea/workflows/ci.yml:远端是 Gitea 企业版,用 Gitea Actions 兼容语法。
是否真的运行取决于实例启用 Actions + runner,文档里如实写明这个前提,
不声称已生效。GOOS=linux CGO_ENABLED=0 交叉编译已验证通过。
tools/e2e/main.go:自建临时 Go 模块工作区,编译 -tags e2e 二进制,
以工作区目录作为启动参数(openFromArgs),连 automation 插座跑 4 条场景:
启动渲染 / 工作区与 gopls / 诊断到达 / 输出面板右键菜单。
两个坑已处理:全新会话默认底部面板是关的(ensurePanel 先点状态栏「面板」);
「运行」有页签和右上角真会跑程序的图标两个同名按钮(按 X<700 区分)。
连跑两次均 4/4,退出码反映结果。
docs/本地自检与CI.md 记录三层守门用法、Gitea Actions 启用前提、e2e 限制
(原生右键菜单插座测不了,仍靠 ui.Tester 单测)。
待办文档移除 P3/P4,规划只剩 4 项按需。
验证:build.cmd verify 全绿(286 个测试)、go run ./tools/e2e 两次 4/4、
GOOS=linux 交叉编译通过。
This commit is contained in:
1 parent
e408300324
commit
1c2f5e3be0
12 files changed
+899
-32
No files matched your search
@@ -161,6 +161,10 @@ type App struct {
|
||||
diff diffView
|
||||
// test 是测试结果树的状态。
|
||||
test testState
|
||||
// testRace / testBench 是测试面板上的会话级开关,不写进设置文件:
|
||||
// -race 会明显拖慢测试,不该变成「不知什么时候被打开」的持久状态。
|
||||
testRace bool
|
||||
testBench bool
|
||||
// cover 是覆盖率的状态。
|
||||
cover coverState
|
||||
// histView 是全屏历史视图(git / 本地共用)。
|
||||
|
||||
@@ -136,7 +136,8 @@ func (a *App) RunCoverage() {
|
||||
out.Close()
|
||||
defer os.Remove(path)
|
||||
|
||||
t, err := task.Start(a.root, "go", "test", "-coverprofile="+path, "./...")
|
||||
args := a.withTestFlags([]string{"test", "-coverprofile=" + path, "./..."})
|
||||
t, err := task.Start(a.root, "go", args...)
|
||||
if err != nil {
|
||||
a.alert("覆盖率", err.Error())
|
||||
return
|
||||
|
||||
@@ -133,7 +133,8 @@ func mainPkgDir(d *Doc) (string, bool) {
|
||||
|
||||
// RunTests 运行工作区的所有测试。
|
||||
func (a *App) RunTests() {
|
||||
a.startTask("go test ./...", "go", "test", "./...")
|
||||
args := a.withTestFlags([]string{"test", "./..."})
|
||||
a.startTask(a.testCommandLabel("go test ./..."), "go", args...)
|
||||
}
|
||||
|
||||
// StopTask 结束正在运行的命令。
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package app
|
||||
|
||||
import "strings"
|
||||
|
||||
// 测试开关:-race / -bench。
|
||||
//
|
||||
// 三个 go test 入口(结果树、输出面板、覆盖率)都走 withTestFlags 注入,
|
||||
// 免得某个入口漏了、开关只在一半地方生效。
|
||||
|
||||
// testFlags 是纯函数,组合出要插到 "go test" 后面的参数。
|
||||
//
|
||||
// -bench 配 -run=^$ 是 Go 的惯例:跑基准时不重复跑普通测试。但如果调用方
|
||||
// 已经给了 -run(例如「运行光标处这一个测试」),就不能覆盖掉它。
|
||||
func testFlags(race, bench, hasRun bool) []string {
|
||||
var out []string
|
||||
if race {
|
||||
out = append(out, "-race")
|
||||
}
|
||||
if bench {
|
||||
out = append(out, "-bench=.")
|
||||
if !hasRun {
|
||||
out = append(out, "-run=^$")
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// hasRunFlag 判断参数里是否已经指定了 -run / -run=xxx。
|
||||
func hasRunFlag(args []string) bool {
|
||||
for _, a := range args {
|
||||
if a == "-run" || strings.HasPrefix(a, "-run=") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// withTestFlags 把开关插到 "go test" 之后、其余参数之前。
|
||||
// 参数是 go 的完整命令行,args[0] 应为 "test"。
|
||||
func (a *App) withTestFlags(args []string) []string {
|
||||
extra := testFlags(a.testRace, a.testBench, hasRunFlag(args))
|
||||
if len(extra) == 0 || len(args) == 0 {
|
||||
return args
|
||||
}
|
||||
out := make([]string, 0, len(args)+len(extra))
|
||||
out = append(out, args[0])
|
||||
out = append(out, extra...)
|
||||
out = append(out, args[1:]...)
|
||||
return out
|
||||
}
|
||||
|
||||
// testCommandLabel 是输出面板里显示的命令行。
|
||||
func (a *App) testCommandLabel(base string) string {
|
||||
var parts []string
|
||||
if a.testRace {
|
||||
parts = append(parts, "-race")
|
||||
}
|
||||
if a.testBench {
|
||||
parts = append(parts, "-bench=.")
|
||||
}
|
||||
if len(parts) == 0 {
|
||||
return base
|
||||
}
|
||||
return base + " [" + strings.Join(parts, " ") + "]"
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTestFlags(t *testing.T) {
|
||||
cases := []struct {
|
||||
race, bench, hasRun bool
|
||||
want []string
|
||||
}{
|
||||
{false, false, false, nil},
|
||||
{true, false, false, []string{"-race"}},
|
||||
{false, true, false, []string{"-bench=.", "-run=^$"}},
|
||||
{true, true, false, []string{"-race", "-bench=.", "-run=^$"}},
|
||||
// 「运行光标处测试」已给 -run,不能被 -run=^$ 覆盖掉。
|
||||
{false, true, true, []string{"-bench=."}},
|
||||
{true, true, true, []string{"-race", "-bench=."}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := testFlags(c.race, c.bench, c.hasRun)
|
||||
if !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("testFlags(%v,%v,%v) = %v, want %v", c.race, c.bench, c.hasRun, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasRunFlag(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"test", "-json", "-run", "TestFoo", "./pkg"},
|
||||
{"test", "-run=^TestFoo$", "./..."},
|
||||
} {
|
||||
if !hasRunFlag(args) {
|
||||
t.Errorf("该识别出 -run:%v", args)
|
||||
}
|
||||
}
|
||||
if hasRunFlag([]string{"test", "-json", "./..."}) {
|
||||
t.Fatal("没有 -run 却识别成了有")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithTestFlags(t *testing.T) {
|
||||
// 关闭时原样返回,不制造多余分配。
|
||||
a := &App{}
|
||||
in := []string{"test", "-json", "./..."}
|
||||
got := a.withTestFlags(in)
|
||||
if !reflect.DeepEqual(got, in) {
|
||||
t.Fatalf("开关全关时不该改参数:%v", got)
|
||||
}
|
||||
|
||||
a.testRace, a.testBench = true, true
|
||||
got = a.withTestFlags([]string{"test", "-json", "./..."})
|
||||
want := []string{"test", "-race", "-bench=.", "-run=^$", "-json", "./..."}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("注入位置不对\n got: %v\nwant: %v", got, want)
|
||||
}
|
||||
// 已有 -run 时不加 -run=^$。
|
||||
got = a.withTestFlags([]string{"test", "-json", "-run", "TestFoo", "./pkg"})
|
||||
if !reflect.DeepEqual(got[:4], []string{"test", "-race", "-bench=.", "-json"}) {
|
||||
t.Fatalf("不该覆盖已有的 -run:%v", got)
|
||||
}
|
||||
for _, x := range got {
|
||||
if x == "-run=^$" {
|
||||
t.Fatalf("已有 -run 时不该再加 -run=^$:%v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestCommandLabel(t *testing.T) {
|
||||
a := &App{}
|
||||
if got := a.testCommandLabel("go test ./..."); got != "go test ./..." {
|
||||
t.Fatalf("没开关时标签不该变:%q", got)
|
||||
}
|
||||
a.testRace, a.testBench = true, true
|
||||
if got := a.testCommandLabel("go test ./..."); got != "go test ./... [-race -bench=.]" {
|
||||
t.Fatalf("标签没显示开关:%q", got)
|
||||
}
|
||||
}
|
||||
+14
-2
@@ -93,6 +93,7 @@ func (a *App) runOneTest(pkg, name string) {
|
||||
|
||||
// startTest 拉起一次 go test 并把结果树清空重填;args 是 go 的完整参数。
|
||||
func (a *App) startTest(args ...string) {
|
||||
args = a.withTestFlags(args)
|
||||
if a.test.running {
|
||||
a.StopTask()
|
||||
}
|
||||
@@ -281,7 +282,12 @@ func (a *App) testView(c *ui.Context) {
|
||||
ui.Column(c).FillHeight().Children(func() {
|
||||
ui.Row(c).Padding(3, 10).Gap(8).AlignItems(ui.Center).
|
||||
BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() {
|
||||
ui.Textf(c, "%s", a.test.pattern).FontSize(11).TextColor(t.TextMuted).SingleLine()
|
||||
ui.Textf(c, "%s", a.testCommandLabel(a.test.pattern)).FontSize(11).
|
||||
TextColor(t.TextMuted).SingleLine()
|
||||
// 会话级开关:不写进设置文件,重开 IDE 自动回到关闭状态。
|
||||
// -race 会明显拖慢测试,不该变成「不知何时被打开」的持久状态。
|
||||
ui.Checkbox(c, &a.testRace, "-race")
|
||||
ui.Checkbox(c, &a.testBench, "-bench")
|
||||
ui.Box(c).Grow(1)
|
||||
if pass+fail+skip > 0 {
|
||||
ui.Textf(c, "✓ %d ✗ %d ○ %d", pass, fail, skip).FontSize(11).TextColor(t.TextMuted)
|
||||
@@ -290,7 +296,13 @@ func (a *App) testView(c *ui.Context) {
|
||||
ui.Text(c, "运行中…").FontSize(11).TextColor(t.Accent)
|
||||
}
|
||||
if ui.Button(c, "全部测试").Clicked() {
|
||||
a.RunTestTree("")
|
||||
// -json 会把 benchmark 的 ns/op 埋在 output 行里,结果树看不清;
|
||||
// 勾了 -bench 就直接走输出面板,数字原样可见。
|
||||
if a.testBench {
|
||||
a.RunTests()
|
||||
} else {
|
||||
a.RunTestTree("")
|
||||
}
|
||||
}
|
||||
if ui.Button(c, "当前包").Clicked() {
|
||||
a.runCurrentPackageTests()
|
||||
|
||||
Reference in new issue
Block a user