Lint is a pipeline step of its own, served by the build seat (ADR 0666 section 7; design 47 *The steps and who serves them* line 571 at hq b229f5c4), so the unit level runs tests alone and the lint step runs gofmt and go vet, with every tag the files name, over each go.mod directory of the artifact's module directory; a module directory holding no go.mod has nothing to lint (prepare finding F12). Milestone 47/G, research 086 area A. Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ (Joined from a46e01a2.) Builds: design 47 G §328, §417 (BLD6) Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
159 lines
5.8 KiB
Go
159 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
)
|
|
|
|
func writeTree(t *testing.T, files map[string]string) string {
|
|
t.Helper()
|
|
tree := t.TempDir()
|
|
for name, body := range files {
|
|
p := filepath.Join(tree, name)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return tree
|
|
}
|
|
|
|
// 1887/D7-go, ADR 0529 §1: an unformatted file fails the unit level naming the file (testdata and vendor are left
|
|
// alone), and go vet runs with no tag and with every tag the files name together, integration and isolation included, so
|
|
// a vet error behind isolation fails the level naming the run.
|
|
func TestTheUnitLevelRunsGofmtAndVetWithEveryTag(t *testing.T) {
|
|
tree := writeTree(t, map[string]string{
|
|
"go.mod": "module x\n\ngo 1.26\n",
|
|
"a.go": "package x\n\nfunc A() {}\n",
|
|
"b.go": "package x\nfunc B() {}\n",
|
|
"in_integration_test.go": "//go:build integration\n\npackage x\n",
|
|
"iso.go": "//go:build integration && isolation && linux\n\npackage x\n",
|
|
"testdata/ugly.go": "package ugly\n",
|
|
"vendor/v/v.go": "package v\n",
|
|
"nested/go.mod": "module x/nested\n",
|
|
"nested/n.go": "package n\n",
|
|
})
|
|
var vetted []string
|
|
run := func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
|
line := strings.Join(args[2:], " ")
|
|
vetted = append(vetted, line)
|
|
if strings.Contains(line, "isolation") {
|
|
return forgekey.Ran{Status: 1, Stderr: "iso.go:3: unreachable code"}
|
|
}
|
|
return forgekey.Ran{}
|
|
}
|
|
failed, err := (&Module{}).lint(context.Background(), run, tree, ".", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(vetted, " | ") != "vet ./... | vet -tags integration,isolation ./..." {
|
|
t.Errorf("vet ran %v", vetted)
|
|
}
|
|
if len(failed) != 2 || failed[0].Test != "gofmt" || !strings.Contains(failed[0].Output, "b.go") ||
|
|
strings.Contains(failed[0].Output, "ugly") || strings.Contains(failed[0].Output, "n.go") || strings.Contains(failed[0].Output, "v.go") {
|
|
t.Fatalf("failed = %+v, want gofmt naming b.go alone", failed)
|
|
}
|
|
if failed[1].Test != "go vet -tags integration,isolation" || !strings.Contains(failed[1].Output, "unreachable") {
|
|
t.Errorf("failed = %+v, want vet with both tags naming its line", failed[1])
|
|
}
|
|
// A formatted directory that vets passes.
|
|
clean := writeTree(t, map[string]string{"go.mod": "module y\n", "a.go": "package y\n"})
|
|
if failed, err := (&Module{}).lint(context.Background(), func(context.Context, []string, string, ...string) forgekey.Ran { return forgekey.Ran{} }, clean, ".", nil); err != nil || len(failed) != 0 {
|
|
t.Errorf("a clean directory: %+v, %v", failed, err)
|
|
}
|
|
}
|
|
|
|
// 086 A-C3 (design 47 *The steps and who serves them*, line 571 at b229f5c4; ADR 0666 §7): gofmt and vet are the lint
|
|
// step's, not the unit level's: a tree holding an unformatted file passes its unit level, and the lint step's body fails
|
|
// naming the file; a module directory holding no go.mod has nothing to lint.
|
|
func TestAnUnformattedFileFailsTheLintStepAndNotTheUnitLevel(t *testing.T) {
|
|
m, states, ws, _ := workspaceFor(t, "empty")
|
|
tree := filepath.Join(m.Work, ws, "tree")
|
|
for name, body := range map[string]string{"go.mod": "module x\n", "x.go": "package x\nfunc X() {}\n"} {
|
|
if err := os.WriteFile(filepath.Join(tree, name), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
res, _ := runUnit(t, m, states, ws)
|
|
for _, f := range res.Failed {
|
|
if f.Test == "gofmt" {
|
|
t.Errorf("the unit level still lints: %+v", res.Failed)
|
|
}
|
|
}
|
|
for _, row := range []struct {
|
|
dir string
|
|
failed bool
|
|
nothing bool
|
|
}{{"", true, false}, {"modules/none", false, true}} {
|
|
out, err := m.lintStep(context.Background(), map[string]any{"workspace": ws, "dir": row.dir})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
la := out.(buildstep.LintAnswer)
|
|
if got := len(la.Failed) == 1 && la.Failed[0].Test == "gofmt" && strings.Contains(la.Failed[0].Output, "x.go"); got != row.failed || la.Nothing != row.nothing {
|
|
t.Errorf("lint of %q = %+v, want failed %v nothing %v", row.dir, la, row.failed, row.nothing)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Issue 1930: the lint of many directories runs side by side up to the level's bound, not one after another, and the
|
|
// failures still join in directory order.
|
|
func TestTheLintOfManyDirectoriesRunsAtOnceUpToTheBound(t *testing.T) {
|
|
files := map[string]string{}
|
|
var dirs []string
|
|
for _, d := range []string{"a", "b", "c", "d", "e", "f"} {
|
|
files[d+"/go.mod"] = "module x/" + d + "\n"
|
|
files[d+"/x.go"] = "package x\n"
|
|
dirs = append(dirs, d)
|
|
}
|
|
tree := writeTree(t, files)
|
|
var mu sync.Mutex
|
|
running, most := 0, 0
|
|
release := make(chan struct{}) // closed once two run at once: they overlap however slowly the machine starts them
|
|
run := func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
|
mu.Lock()
|
|
running++
|
|
most = max(most, running)
|
|
if running >= 2 {
|
|
select {
|
|
case <-release:
|
|
default:
|
|
close(release)
|
|
}
|
|
}
|
|
mu.Unlock()
|
|
select {
|
|
case <-release:
|
|
case <-time.After(30 * time.Second):
|
|
}
|
|
mu.Lock()
|
|
running--
|
|
mu.Unlock()
|
|
if strings.HasSuffix(args[1], "b") || strings.HasSuffix(args[1], "e") {
|
|
return forgekey.Ran{Status: 1, Stderr: "bad " + filepath.Base(args[1])}
|
|
}
|
|
return forgekey.Ran{}
|
|
}
|
|
m := &Module{Parallel: 3}
|
|
failed, err := m.lintAll(context.Background(), run, tree, dirs, func(string) []string { return nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if most < 2 || most > 3 {
|
|
t.Errorf("%d lint commands ran at once, want 2 or 3 (the bound)", most)
|
|
}
|
|
if len(failed) != 2 || failed[0].Package != "b" || failed[1].Package != "e" {
|
|
t.Errorf("failed = %+v, want b's then e's", failed)
|
|
}
|
|
}
|