From 22497ecf45ae4a198a0aa6940549dc21c8f09a10 Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Sat, 13 May 2023 19:59:27 +0200 Subject: [PATCH] internal/fudgepb: Make tests work on other archs --- internal/fudgepb/fudgepb_test.go | 56 +++++++++------ .../TestToProto/fudge sentinel.golden | 36 +++++----- .../TestToProto/fudge with kvs.golden | 54 +++++++------- .../testdata/TestToProto/fudge wrapped.golden | 42 +++++------ .../fudgepb/testdata/TestToProto/fudge.golden | 34 ++++----- .../TestToProto/std sentinel wrapped.golden | 40 +++++------ .../testdata/TestToProto/std sentinel.golden | 8 +-- .../fudgepb/testdata/TestToProto/std.golden | 6 +- .../testdata/TestToProto/with hops.golden | 72 +++++++++---------- internal/stack/stack.go | 2 + 10 files changed, 184 insertions(+), 166 deletions(-) diff --git a/internal/fudgepb/fudgepb_test.go b/internal/fudgepb/fudgepb_test.go index 1801bbe..1a0ba5e 100644 --- a/internal/fudgepb/fudgepb_test.go +++ b/internal/fudgepb/fudgepb_test.go @@ -8,6 +8,7 @@ import ( "github.com/rossmacarthur/fudge" "github.com/rossmacarthur/fudge/errors" + "github.com/rossmacarthur/fudge/internal/stack" "github.com/sebdah/goldie/v2" "github.com/stretchr/testify/require" "google.golang.org/protobuf/encoding/protojson" @@ -16,6 +17,8 @@ import ( var errSentinel = errors.Sentinel("such test", "TEST1234") func TestFromProto(t *testing.T) { + asmFrame := getAsmFrame() + tests := []struct { name string err *Error @@ -51,11 +54,7 @@ func TestFromProto(t *testing.T) { Function: "tRunner", Line: 1576, }, - { - File: "runtime/asm_arm64.s", - Function: "goexit", - Line: 1172, - }, + asmFrame, }, }, }, @@ -80,11 +79,7 @@ func TestFromProto(t *testing.T) { Function: "tRunner", Line: 1576, }, - { - File: "runtime/asm_arm64.s", - Function: "goexit", - Line: 1172, - }, + asmFrame, }, }, { @@ -108,11 +103,7 @@ func TestFromProto(t *testing.T) { Function: "tRunner", Line: 1576, }, - { - File: "runtime/asm_arm64.s", - Function: "goexit", - Line: 1172, - }, + asmFrame, }, }, }, @@ -149,11 +140,7 @@ func TestFromProto(t *testing.T) { Function: "tRunner", Line: 1576, }, - { - File: "runtime/asm_arm64.s", - Function: "goexit", - Line: 1172, - }, + asmFrame, }, }, @@ -178,6 +165,8 @@ func TestFromProto(t *testing.T) { } func TestToProto(t *testing.T) { + dummyFrame := getDummyAsmFrame() + tests := []struct { name string errFn func() error @@ -239,6 +228,11 @@ func TestToProto(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := ToProto(tt.errFn()) + for _, hop := range got.Hops { + if len(hop.Trace) > 0 { + hop.Trace[len(hop.Trace)-1] = dummyFrame + } + } bytes, err := protojson.MarshalOptions{Multiline: true}.Marshal(got) require.Nil(t, err) g.Assert(t, tt.name, bytes) @@ -264,3 +258,25 @@ func TestRoundtrip(t *testing.T) { require.False(t, errors.Is(got, err)) require.False(t, errors.Is(got, errSentinel)) } + +// getAsmFrame calculates the runtime frame to use in tests. +// This is required because the runtime/asm_.s frame depends on the architecture. +func getAsmFrame() *Frame { + + trace := stack.Trace(0) + f := &trace[len(trace)-1] + return &Frame{ + File: f.File, + Function: f.Function, + Line: int32(f.Line), + } +} + +// getDummyAsmFrame returns a dummy frame to use in tests. +func getDummyAsmFrame() *Frame { + return &Frame{ + File: "runtime/asm_arch.s", + Function: "goexit", + Line: 1337, + } +} diff --git a/internal/fudgepb/testdata/TestToProto/fudge sentinel.golden b/internal/fudgepb/testdata/TestToProto/fudge sentinel.golden index 959a73d..f91f887 100644 --- a/internal/fudgepb/testdata/TestToProto/fudge sentinel.golden +++ b/internal/fudgepb/testdata/TestToProto/fudge sentinel.golden @@ -1,30 +1,30 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "message": "such test", - "code": "TEST1234", - "trace": [ + "kind": 2, + "message": "such test", + "code": "TEST1234", + "trace": [ { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func8", - "line": 222, - "message": "very wrap" + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func8", + "line": 211, + "message": "very wrap" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] } diff --git a/internal/fudgepb/testdata/TestToProto/fudge with kvs.golden b/internal/fudgepb/testdata/TestToProto/fudge with kvs.golden index 54af303..cb68324 100644 --- a/internal/fudgepb/testdata/TestToProto/fudge with kvs.golden +++ b/internal/fudgepb/testdata/TestToProto/fudge with kvs.golden @@ -1,47 +1,47 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "binary": "fudgepb.test", - "trace": [ + "kind": 2, + "binary": "fudgepb.test", + "trace": [ { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func7", - "line": 215, - "message": "such test", - "keyValues": [ + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func7", + "line": 204, + "message": "such test", + "keyValues": [ { - "key": "hello", - "value": "world" + "key": "hello", + "value": "world" } ] }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func7", - "line": 216, - "message": "very wrap", - "keyValues": [ + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func7", + "line": 205, + "message": "very wrap", + "keyValues": [ { - "key": "foo", - "value": "bar" + "key": "foo", + "value": "bar" } ] }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] } diff --git a/internal/fudgepb/testdata/TestToProto/fudge wrapped.golden b/internal/fudgepb/testdata/TestToProto/fudge wrapped.golden index 7b9450d..ae66cf3 100644 --- a/internal/fudgepb/testdata/TestToProto/fudge wrapped.golden +++ b/internal/fudgepb/testdata/TestToProto/fudge wrapped.golden @@ -1,35 +1,35 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "binary": "fudgepb.test", - "trace": [ + "kind": 2, + "binary": "fudgepb.test", + "trace": [ { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func6", - "line": 208, - "message": "such test" + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func6", + "line": 197, + "message": "such test" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func6", - "line": 209, - "message": "very wrap" + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func6", + "line": 198, + "message": "very wrap" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] } diff --git a/internal/fudgepb/testdata/TestToProto/fudge.golden b/internal/fudgepb/testdata/TestToProto/fudge.golden index 8f7459c..a67d517 100644 --- a/internal/fudgepb/testdata/TestToProto/fudge.golden +++ b/internal/fudgepb/testdata/TestToProto/fudge.golden @@ -1,29 +1,29 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "binary": "fudgepb.test", - "trace": [ + "kind": 2, + "binary": "fudgepb.test", + "trace": [ { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func5", - "line": 203, - "message": "such test" + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func5", + "line": 192, + "message": "such test" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] } diff --git a/internal/fudgepb/testdata/TestToProto/std sentinel wrapped.golden b/internal/fudgepb/testdata/TestToProto/std sentinel wrapped.golden index b896e81..303f76f 100644 --- a/internal/fudgepb/testdata/TestToProto/std sentinel wrapped.golden +++ b/internal/fudgepb/testdata/TestToProto/std sentinel wrapped.golden @@ -1,36 +1,36 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "binary": "fudgepb.test", - "trace": [ + "kind": 2, + "binary": "fudgepb.test", + "trace": [ { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func4", - "line": 199, - "message": "very wrap" + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func4", + "line": 188, + "message": "very wrap" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] }, { - "kind": 1, - "message": "context canceled", - "code": "context.Canceled" + "kind": 1, + "message": "context canceled", + "code": "context.Canceled" } ] } \ No newline at end of file diff --git a/internal/fudgepb/testdata/TestToProto/std sentinel.golden b/internal/fudgepb/testdata/TestToProto/std sentinel.golden index 29b330e..5c4fee4 100644 --- a/internal/fudgepb/testdata/TestToProto/std sentinel.golden +++ b/internal/fudgepb/testdata/TestToProto/std sentinel.golden @@ -1,9 +1,9 @@ { - "hops": [ + "hops": [ { - "kind": 1, - "message": "context canceled", - "code": "context.Canceled" + "kind": 1, + "message": "context canceled", + "code": "context.Canceled" } ] } \ No newline at end of file diff --git a/internal/fudgepb/testdata/TestToProto/std.golden b/internal/fudgepb/testdata/TestToProto/std.golden index ddf185e..5997c62 100644 --- a/internal/fudgepb/testdata/TestToProto/std.golden +++ b/internal/fudgepb/testdata/TestToProto/std.golden @@ -1,8 +1,8 @@ { - "hops": [ + "hops": [ { - "kind": 1, - "message": "EOF" + "kind": 1, + "message": "EOF" } ] } \ No newline at end of file diff --git a/internal/fudgepb/testdata/TestToProto/with hops.golden b/internal/fudgepb/testdata/TestToProto/with hops.golden index 27c2eb3..f69cdd7 100644 --- a/internal/fudgepb/testdata/TestToProto/with hops.golden +++ b/internal/fudgepb/testdata/TestToProto/with hops.golden @@ -1,57 +1,57 @@ { - "hops": [ + "hops": [ { - "kind": 2, - "binary": "fudgepb.test", - "trace": [ - { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func9", - "line": 228, - "message": "this hop" + "kind": 2, + "binary": "fudgepb.test", + "trace": [ + { + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func9", + "line": 217, + "message": "this hop" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] }, { - "kind": 2, - "message": "such test", - "code": "TEST1234", - "trace": [ - { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func9", - "line": 231, - "message": "very wrap" + "kind": 2, + "message": "such test", + "code": "TEST1234", + "trace": [ + { + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func9", + "line": 220, + "message": "very wrap" }, { - "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", - "function": "TestToProto.func10", - "line": 241 + "file": "github.com/rossmacarthur/fudge/internal/fudgepb/fudgepb_test.go", + "function": "TestToProto.func10", + "line": 230 }, { - "file": "testing/testing.go", - "function": "tRunner", - "line": 1576 + "file": "testing/testing.go", + "function": "tRunner", + "line": 1576 }, { - "file": "runtime/asm_arm64.s", - "function": "goexit", - "line": 1172 + "file": "runtime/asm_arch.s", + "function": "goexit", + "line": 1337 } ] } diff --git a/internal/stack/stack.go b/internal/stack/stack.go index 053dffa..06795ea 100644 --- a/internal/stack/stack.go +++ b/internal/stack/stack.go @@ -12,6 +12,7 @@ type Frame struct { Line int } +// Trace returns a stack trace, skipping the given number of frames func Trace(skip int) []Frame { var pcs [512]uintptr n := runtime.Callers(skip+1, pcs[:]) @@ -29,6 +30,7 @@ func Trace(skip int) []Frame { return trace } +// Call returns the frame of the caller, skipping the given number of frames func Call(skip int) Frame { var pcs [3]uintptr n := runtime.Callers(skip+1, pcs[:])