Files
zip/layout_test.go
zeekayandHanzo Dev cda55906cf Call the generator Layouts, not Codecs
A codec is a runtime encode/decode step, which is the thing ZAP removes: a
field IS its offset, and a read is a pointer into the bytes that arrived.
Nothing here runs on the served path — Layouts reflects at BUILD time and emits
constant offsets. Naming it for the layer it exists to delete told readers the
opposite of what it does.

LayoutOf already names one type's shape; this emits those as source.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:34:08 -07:00

51 lines
1.6 KiB
Go

package zip_test
import (
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
"github.com/zap-proto/zip"
"github.com/zap-proto/zip/internal/layout"
)
// TestTheLayoutIsCheckedIn regenerates internal/layout/zap_gen.go and requires the
// file on disk to match. The generated codec is the wire those types speak, so a
// change to the types that nobody regenerated is a wire that moved under its
// readers — and this is where that is noticed, not in a rolling deploy.
func TestTheLayoutIsCheckedIn(t *testing.T) {
got, err := zip.Layouts(reflect.TypeOf(layout.Trunk{}), reflect.TypeOf(layout.Ided{}))
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("got %d packages, want 1", len(got))
}
path := filepath.Join("internal", "layout", "zap_gen.go")
want, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(got[0].Source) != string(want) {
if os.Getenv("WRITE") != "" {
if err := os.WriteFile(path, got[0].Source, 0o644); err != nil {
t.Fatal(err)
}
t.Fatal("wrote " + path + "; run again")
}
t.Errorf("%s is stale; regenerate with WRITE=1 go test -run TestTheLayoutIsCheckedIn ./...", path)
}
}
// TestTheEmittedSourceCompiles builds the package the emitter wrote. format.Source
// only proves it PARSES; a codec that names a field that moved, or converts a
// value the type will not take, parses and does not build.
func TestTheEmittedSourceCompiles(t *testing.T) {
out, err := exec.Command("go", "build", "./internal/layout").CombinedOutput()
if err != nil {
t.Fatalf("go build ./internal/layout: %v\n%s", err, out)
}
}