From ce0a61ae59dc9d992bec77e7a21e9148435d4c9e Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sun, 9 Aug 2026 14:36:31 -0400 Subject: [PATCH] test: bring failure and buildinfo tests under shape limits --- internal/buildinfo/info_test.go | 153 +++++++++++++++++--------------- internal/failure/error_test.go | 149 +++++++++++++++++-------------- 2 files changed, 163 insertions(+), 139 deletions(-) diff --git a/internal/buildinfo/info_test.go b/internal/buildinfo/info_test.go index 23397e3..dbd7bf2 100644 --- a/internal/buildinfo/info_test.go +++ b/internal/buildinfo/info_test.go @@ -6,81 +6,90 @@ import ( ) func TestBuildInformation(t *testing.T) { - t.Run("development defaults", func(t *testing.T) { - snapshot := FromValues("dev", "unknown", "unknown") - if snapshot.Version != "dev" { - t.Fatalf("version = %q", snapshot.Version) - } - if snapshot.Commit != "unknown" { - t.Fatalf("commit = %q", snapshot.Commit) - } - if snapshot.Timestamp != "unknown" { - t.Fatalf("timestamp = %q", snapshot.Timestamp) - } - if snapshot.HasTimestamp { - t.Fatal("development timestamp must not parse") - } - if !snapshot.BuiltAt.IsZero() { - t.Fatal("development built-at must be zero") - } - }) + scenarios := []struct { + name string + run func(*testing.T) + }{ + {"development defaults", testDevelopmentDefaults}, + {"injected release values", testReleaseValues}, + {"malformed timestamp is safe", testMalformedTimestamp}, + {"empty timestamp is unknown", testEmptyTimestamp}, + {"runtime fields populated", testRuntimeFields}, + {"current reads package defaults", testCurrentReadsDefaults}, + } + for _, scenario := range scenarios { + t.Run(scenario.name, scenario.run) + } +} - t.Run("injected release values", func(t *testing.T) { - snapshot := FromValues("v1.2.3", "abcdef1234567890", "2026-08-09T12:00:00Z") - if snapshot.Version != "v1.2.3" { - t.Fatalf("version = %q", snapshot.Version) - } - if snapshot.Commit != "abcdef1234567890" { - t.Fatalf("commit = %q", snapshot.Commit) - } - if !snapshot.HasTimestamp { - t.Fatal("RFC3339 timestamp must parse") - } - if year := snapshot.BuiltAt.Year(); year != 2026 { - t.Fatalf("built-at year = %d", year) - } - if snapshot.BuiltAt.Location().String() != "UTC" { - t.Fatalf("location = %q", snapshot.BuiltAt.Location()) - } - }) +func testDevelopmentDefaults(t *testing.T) { + snapshot := FromValues("dev", "unknown", "unknown") + if snapshot.Version != "dev" { + t.Fatalf("version = %q", snapshot.Version) + } + if snapshot.Commit != "unknown" { + t.Fatalf("commit = %q", snapshot.Commit) + } + if snapshot.Timestamp != "unknown" { + t.Fatalf("timestamp = %q", snapshot.Timestamp) + } + if snapshot.HasTimestamp { + t.Fatal("development timestamp must not parse") + } + if !snapshot.BuiltAt.IsZero() { + t.Fatal("development built-at must be zero") + } +} - t.Run("malformed timestamp is safe", func(t *testing.T) { - snapshot := FromValues("v1.0.0", "deadbeef", "not-a-date") - if snapshot.HasTimestamp { - t.Fatal("malformed timestamp must not parse as present") - } - if !snapshot.BuiltAt.IsZero() { - t.Fatal("malformed timestamp must yield zero time") - } - if snapshot.Timestamp != "not-a-date" { - t.Fatalf("raw timestamp must be preserved: %q", snapshot.Timestamp) - } - }) +func testReleaseValues(t *testing.T) { + snapshot := FromValues("v1.2.3", "abcdef1234567890", "2026-08-09T12:00:00Z") + if snapshot.Version != "v1.2.3" { + t.Fatalf("version = %q", snapshot.Version) + } + if !snapshot.HasTimestamp { + t.Fatal("RFC3339 timestamp must parse") + } + if year := snapshot.BuiltAt.Year(); year != 2026 { + t.Fatalf("built-at year = %d", year) + } + if snapshot.BuiltAt.Location().String() != "UTC" { + t.Fatalf("location = %q", snapshot.BuiltAt.Location()) + } +} - t.Run("empty timestamp is unknown", func(t *testing.T) { - snapshot := FromValues("v1.0.0", "deadbeef", "") - if snapshot.HasTimestamp { - t.Fatal("empty timestamp must not parse") - } - }) +func testMalformedTimestamp(t *testing.T) { + snapshot := FromValues("v1.0.0", "deadbeef", "not-a-date") + if snapshot.HasTimestamp { + t.Fatal("malformed timestamp must not parse as present") + } + if !snapshot.BuiltAt.IsZero() { + t.Fatal("malformed timestamp must yield zero time") + } +} - t.Run("runtime fields populated", func(t *testing.T) { - snapshot := FromValues("dev", "unknown", "unknown") - if snapshot.GoVersion != runtime.Version() { - t.Fatalf("go version = %q", snapshot.GoVersion) - } - if snapshot.OperatingSystem != runtime.GOOS { - t.Fatalf("os = %q", snapshot.OperatingSystem) - } - if snapshot.Architecture != runtime.GOARCH { - t.Fatalf("arch = %q", snapshot.Architecture) - } - }) +func testEmptyTimestamp(t *testing.T) { + snapshot := FromValues("v1.0.0", "deadbeef", "") + if snapshot.HasTimestamp { + t.Fatal("empty timestamp must not parse") + } +} + +func testRuntimeFields(t *testing.T) { + snapshot := FromValues("dev", "unknown", "unknown") + if snapshot.GoVersion != runtime.Version() { + t.Fatalf("go version = %q", snapshot.GoVersion) + } + if snapshot.OperatingSystem != runtime.GOOS { + t.Fatalf("os = %q", snapshot.OperatingSystem) + } + if snapshot.Architecture != runtime.GOARCH { + t.Fatalf("arch = %q", snapshot.Architecture) + } +} - t.Run("current reads package defaults", func(t *testing.T) { - snapshot := Current() - if snapshot.Version != Version { - t.Fatalf("current version = %q", snapshot.Version) - } - }) +func testCurrentReadsDefaults(t *testing.T) { + snapshot := Current() + if snapshot.Version != Version { + t.Fatalf("current version = %q", snapshot.Version) + } } diff --git a/internal/failure/error_test.go b/internal/failure/error_test.go index e7778e4..b3b2d01 100644 --- a/internal/failure/error_test.go +++ b/internal/failure/error_test.go @@ -7,76 +7,91 @@ import ( ) func TestFailureContract(t *testing.T) { - t.Run("new wraps cause and message", func(t *testing.T) { - leaf := errors.New("disk full") - err := New(Operational, "write target", leaf) - if err.Kind != Operational { - t.Fatalf("kind = %q, want Operational", err.Kind) - } - if !errors.Is(err, leaf) { - t.Fatal("errors.Is must reach wrapped cause") - } - var target *Error - if !errors.As(err, &target) { - t.Fatal("errors.As must match *Error") - } - if target.Message != "write target" { - t.Fatalf("message = %q", target.Message) - } - }) + scenarios := []struct { + name string + run func(*testing.T) + }{ + {"new wraps cause and message", testNewWrapsCause}, + {"nil cause renders message alone", testNilCauseMessage}, + {"nil pointer error is empty", testNilPointerEmpty}, + {"hasKind traverses joins", testHasKindJoins}, + {"hasKind false for plain error", testHasKindPlain}, + {"interruption signal cause", testInterruptionCause}, + } + for _, scenario := range scenarios { + t.Run(scenario.name, scenario.run) + } +} - t.Run("nil cause renders message alone", func(t *testing.T) { - err := New(InvalidInput, "bad path", nil) - if err.Error() != "bad path" { - t.Fatalf("error = %q", err.Error()) - } - if err.Unwrap() != nil { - t.Fatal("unwrap of nil cause must be nil") - } - }) +func testNewWrapsCause(t *testing.T) { + leaf := errors.New("disk full") + err := New(Operational, "write target", leaf) + if err.Kind != Operational { + t.Fatalf("kind = %q, want Operational", err.Kind) + } + if !errors.Is(err, leaf) { + t.Fatal("errors.Is must reach wrapped cause") + } + var target *Error + if !errors.As(err, &target) { + t.Fatal("errors.As must match *Error") + } + if target.Message != "write target" { + t.Fatalf("message = %q", target.Message) + } +} - t.Run("nil pointer error is empty", func(t *testing.T) { - var err *Error - if err.Error() != "" { - t.Fatalf("nil error = %q", err.Error()) - } - if err.Unwrap() != nil { - t.Fatal("nil unwrap must be nil") - } - }) +func testNilCauseMessage(t *testing.T) { + err := New(InvalidInput, "bad path", nil) + if err.Error() != "bad path" { + t.Fatalf("error = %q", err.Error()) + } + if err.Unwrap() != nil { + t.Fatal("unwrap of nil cause must be nil") + } +} - t.Run("hasKind traverses joins", func(t *testing.T) { - joined := errors.Join( - New(Difference, "drift", nil), - fmt.Errorf("unrelated"), - New(Hook, "after hook", nil), - ) - kind, ok := HasKind(joined) - if !ok { - t.Fatal("HasKind must find categorized failure in join") - } - if kind != Difference { - t.Fatalf("kind = %q, want first categorized Difference", kind) - } - }) +func testNilPointerEmpty(t *testing.T) { + var err *Error + if err.Error() != "" { + t.Fatalf("nil error = %q", err.Error()) + } + if err.Unwrap() != nil { + t.Fatal("nil unwrap must be nil") + } +} - t.Run("hasKind false for plain error", func(t *testing.T) { - if _, ok := HasKind(errors.New("plain")); ok { - t.Fatal("HasKind must be false for non-failure error") - } - }) +func testHasKindJoins(t *testing.T) { + joined := errors.Join( + New(Difference, "drift", nil), + fmt.Errorf("unrelated"), + New(Hook, "after hook", nil), + ) + kind, ok := HasKind(joined) + if !ok { + t.Fatal("HasKind must find categorized failure in join") + } + if kind != Difference { + t.Fatalf("kind = %q, want first categorized Difference", kind) + } +} + +func testHasKindPlain(t *testing.T) { + if _, ok := HasKind(errors.New("plain")); ok { + t.Fatal("HasKind must be false for non-failure error") + } +} - t.Run("interruption signal cause", func(t *testing.T) { - interrupt := NewInterruption(Interrupt) - var cause *Interruption - if !errors.As(errors.Join(interrupt), &cause) { - t.Fatal("errors.As must find Interruption in join") - } - if cause.Signal != Interrupt { - t.Fatalf("signal = %q", cause.Signal) - } - if cause.Error() == "" { - t.Fatal("interruption error must render") - } - }) +func testInterruptionCause(t *testing.T) { + interrupt := NewInterruption(Interrupt) + var cause *Interruption + if !errors.As(errors.Join(interrupt), &cause) { + t.Fatal("errors.As must find Interruption in join") + } + if cause.Signal != Interrupt { + t.Fatalf("signal = %q", cause.Signal) + } + if cause.Error() == "" { + t.Fatal("interruption error must render") + } } -- 2.51.2