media: guard medialocal imports on every cross-compiled target
Astra T13 round 2, P2. TestMedialocalIsTestOnly read only three targets; it now reads every target from .amadan/ci.d/20-cross@5's shard lines, plus linux/amd64, the gate's native build, so the lists cannot drift. An unreadable step, or fewer targets than its five shards, fails the test, and TestSupportedTargetsReadTheCrossStep pins the six. A forbidden import in a *_linux_arm64.go file, which the old three targets never build, now fails the guard on linux/arm64.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+74
−15
internal/medialocal/testonly_test.go+74 −15
CI cancelled — superseded by a newer push — run details
diff --git a/internal/medialocal/testonly_test.go b/internal/medialocal/testonly_test.go| index 2d2d5c3e9..2abbe2b2f 100644 |
| --- a/internal/medialocal/testonly_test.go |
| +++ b/internal/medialocal/testonly_test.go |
| @@ -4,9 +4,11 @@ import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "regexp" |
| "runtime" |
| "slices" |
| "strings" |
| + "sync" |
| "testing" |
| ) |
| @@ -15,14 +17,48 @@ const ( |
| selfPath = modulePath + "/internal/medialocal" |
| ) |
| +// crossStep is the CI step that cross-compiles the module, one target per |
| +// shard; its targets are this guard's, so the two lists cannot drift. |
| +const crossStep = "../../.amadan/ci.d/20-cross@5" |
| + |
| +// nativeTarget is what the gate (.amadan/ci.d/10-gate) builds and tests |
| +// on, which the cross step leaves out. |
| +const nativeTarget = "linux/amd64" |
| + |
| +// crossTarget matches one shard's target line: `0) GOOS=darwin GOARCH=arm64 ;;`. |
| +var crossTarget = regexp.MustCompile(`(?m)^\s*[0-9]+\)\s+GOOS=([a-z0-9]+)\s+GOARCH=([a-z0-9]+)\s*;;`) |
| + |
| +// supportedTargets are nativeTarget and every target crossStep builds. A |
| +// step it cannot read, or one with fewer targets than its shard count, |
| +// fails the test: an empty list must not pass. |
| +func supportedTargets(t *testing.T) []string { |
| + t.Helper() |
| + b, err := os.ReadFile(crossStep) |
| + if err != nil { |
| + t.Fatalf("read the cross-compile step: %v", err) |
| + } |
| + targets := []string{nativeTarget} |
| + for _, m := range crossTarget.FindAllStringSubmatch(string(b), -1) { |
| + if target := m[1] + "/" + m[2]; !slices.Contains(targets, target) { |
| + targets = append(targets, target) |
| + } |
| + } |
| + if got, shards := len(targets)-1, 5; got < shards { |
| + t.Fatalf("read %d targets from %s, which deals %d shards: has its target lines' shape changed?", got, crossStep, shards) |
| + } |
| + return targets |
| +} |
| + |
| // TestMedialocalIsTestOnly: no non-test package of the module imports |
| -// this one, on Linux, macOS or Windows. The runner contains the codec, |
| -// not its descendants (package doc): a descendant that leaves the |
| -// codec's process group or session outlives Stop, the deadline and the |
| -// runner. Production containment is the ECS task, or the systemd unit's |
| -// cgroup with RuntimeMaxSec on a deployment without AWS (spec §8), so |
| -// using the runner as a production engine needs that containment first, |
| -// and this guard removed on purpose. |
| +// this one, on any supported target (supportedTargets), so a forbidden |
| +// import in a file built for one target only (say *_linux_arm64.go) is |
| +// caught too. The runner contains the codec, not its descendants |
| +// (package doc): a descendant that leaves the codec's process group or |
| +// session outlives Stop, the deadline and the runner. Production |
| +// containment is the ECS task, or the systemd unit's cgroup with |
| +// RuntimeMaxSec on a deployment without AWS (spec §8), so using the |
| +// runner as a production engine needs that containment first, and this |
| +// guard removed on purpose. |
| func TestMedialocalIsTestOnly(t *testing.T) { |
| gobin := filepath.Join(runtime.GOROOT(), "bin", "go") |
| if _, err := os.Stat(gobin); err != nil { |
| @@ -30,15 +66,29 @@ func TestMedialocalIsTestOnly(t *testing.T) { |
| t.Skip("no go toolchain to list imports with") |
| } |
| } |
| - for _, target := range []string{"linux/amd64", "darwin/arm64", "windows/amd64"} { |
| - goos, goarch, _ := strings.Cut(target, "/") |
| - cmd := exec.Command(gobin, "list", "-e", "-f", "{{.ImportPath}} {{join .Imports \" \"}}", modulePath+"/...") |
| - cmd.Env = append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "CGO_ENABLED=0") |
| - out, err := cmd.Output() |
| - if err != nil { |
| - t.Fatalf("go list (%s): %v", target, err) |
| + targets := supportedTargets(t) |
| + type listing struct { |
| + out []byte |
| + err error |
| + } |
| + results := make([]listing, len(targets)) |
| + var wg sync.WaitGroup |
| + for i, target := range targets { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + goos, goarch, _ := strings.Cut(target, "/") |
| + cmd := exec.Command(gobin, "list", "-e", "-f", "{{.ImportPath}} {{join .Imports \" \"}}", modulePath+"/...") |
| + cmd.Env = append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "CGO_ENABLED=0") |
| + results[i].out, results[i].err = cmd.Output() |
| + }() |
| + } |
| + wg.Wait() |
| + for i, target := range targets { |
| + if results[i].err != nil { |
| + t.Fatalf("go list (%s): %v", target, results[i].err) |
| } |
| - lines := strings.Split(strings.TrimSpace(string(out)), "\n") |
| + lines := strings.Split(strings.TrimSpace(string(results[i].out)), "\n") |
| if len(lines) < 10 { |
| t.Fatalf("go list (%s) answered %d packages", target, len(lines)) |
| } |
| @@ -56,3 +106,12 @@ func TestMedialocalIsTestOnly(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// TestSupportedTargetsReadTheCrossStep: the guard covers the gate's |
| +// target and the cross step's five, read from the step itself. |
| +func TestSupportedTargetsReadTheCrossStep(t *testing.T) { |
| + want := []string{"linux/amd64", "darwin/arm64", "darwin/amd64", "linux/arm64", "windows/amd64", "windows/arm64"} |
| + if got := supportedTargets(t); !slices.Equal(got, want) { |
| + t.Fatalf("supported targets %v, want %v", got, want) |
| + } |
| +} |