Part of BLD2 (Go module): the state detected-steps asked by the template state, one key per artifact, answered for the same fingerprint, replaced by a newer one and by again. The drop of a key and the Node and image modules wait (drop: question to the lead; the others follow in BLD7 and BLD8). Builds: design 47 G §326, §414 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
90 lines
3.7 KiB
Go
90 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// Design 47 G §22, §307 (BLD1): the verb steps answers an artifact's lint, unit and integration steps by Go's
|
|
// convention and leaves out a level with no test.
|
|
func TestTheStepsVerbAnswersTheStepsOfAFixtureArtifact(t *testing.T) {
|
|
f := &fakeStep{files: map[string]string{
|
|
"go.mod": "module example.test/mod\n\ngo 1.26\n",
|
|
"a/a_test.go": "package a\n",
|
|
"a/b_integration_test.go": "//go:build integration\n\npackage a\n",
|
|
"b/b_test.go": "//go:build !windows\n\npackage b\n",
|
|
"c/c_test.go": "//go:build integration\n\npackage c\n",
|
|
"d/d_test.go": "//go:build windows\n\npackage d\n",
|
|
"testdata/x/x_test.go": "package x\n",
|
|
"e/e.go": "package e\n",
|
|
}}
|
|
m, _ := fetchModule(t, f)
|
|
var run func(map[string]any) (any, error)
|
|
for _, tool := range m.SeatTools(m.Puller(nil, buildstep.NewFakeAsks())) {
|
|
if tool.Name == Seat+".steps" {
|
|
run = tool.Run
|
|
}
|
|
}
|
|
if run == nil {
|
|
t.Fatal("the module serves no verb steps")
|
|
}
|
|
got, err := run(map[string]any{"module": "mod", "artifact": "mod:mod", "fingerprint": "fp1",
|
|
"fetch": map[string]any{"repository": "novox/mod", "commit": commitSHA}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []DetectedStep{{Step: "lint"},
|
|
{Step: "unit", Packages: []string{"example.test/mod/a", "example.test/mod/b"}},
|
|
{Step: "integration", Packages: []string{"example.test/mod/a", "example.test/mod/c"}}}
|
|
if a := got.(StepsAnswer); !reflect.DeepEqual(a.Steps, want) || a.Artifact != "mod:mod" {
|
|
t.Fatalf("steps = %+v, want %+v", a, want)
|
|
}
|
|
// A module with no test answers lint alone: its unit and integration levels have nothing to run.
|
|
f.files = map[string]string{"go.mod": "module example.test/mod\n", "mod/e/e.go": "package e\n"}
|
|
got, err = run(map[string]any{"module": "mod", "artifact": "mod:mod",
|
|
"fetch": map[string]any{"repository": "novox/mod", "commit": commitSHA}})
|
|
if err != nil || !reflect.DeepEqual(got.(StepsAnswer).Steps, []DetectedStep{{Step: "lint"}}) {
|
|
t.Fatalf("a module with no test: %+v, %v", got, err)
|
|
}
|
|
}
|
|
|
|
// Design 47 G §326 (BLD2): steps answers from the kept key for the same fingerprint, a newer fingerprint and again
|
|
// replace it.
|
|
func TestTheStepsVerbKeepsItsAnswerByFingerprint(t *testing.T) {
|
|
f := &fakeStep{files: map[string]string{"go.mod": "module example.test/mod\n", "a/a_test.go": "package a\n"}}
|
|
m, _ := fetchModule(t, f)
|
|
kept := map[string]json.RawMessage{}
|
|
m.Detected = func(key string, v any) (uint64, error) {
|
|
raw, err := json.Marshal(v)
|
|
kept[key] = raw
|
|
return 1, err
|
|
}
|
|
m.ReadDetected = func(key string) (json.RawMessage, error) { return kept[key], nil }
|
|
call := func(fp string, again bool) StepsAnswer {
|
|
t.Helper()
|
|
got, err := m.steps(context.Background(), map[string]any{"module": "mod", "artifact": "mod:prog", "fingerprint": fp, "again": again,
|
|
"fetch": map[string]any{"repository": "novox/mod", "commit": commitSHA}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got.(StepsAnswer)
|
|
}
|
|
if a := call("fp1", false); a.Kept || len(kept) != 1 || kept["mod.prog"] == nil {
|
|
t.Fatalf("first ask: %+v, keys %v", a, kept)
|
|
}
|
|
f.files = map[string]string{"go.mod": "module example.test/mod\n"} // the tree changed, the kept answer still answers fp1
|
|
if a := call("fp1", false); !a.Kept || len(a.Steps) != 2 {
|
|
t.Fatalf("same fingerprint: %+v", a)
|
|
}
|
|
if a := call("fp1", true); a.Kept || len(a.Steps) != 1 {
|
|
t.Fatalf("again: %+v", a)
|
|
}
|
|
f.files = map[string]string{"go.mod": "module example.test/mod\n", "a/a_test.go": "package a\n"}
|
|
if a := call("fp2", false); a.Kept || len(a.Steps) != 2 {
|
|
t.Fatalf("newer fingerprint: %+v", a)
|
|
}
|
|
}
|