Skip to content

Commit

Permalink
internal/fudgepb: Make tests work on other archs
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed May 13, 2023
1 parent 67f5fae commit 22497ec
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 166 deletions.
56 changes: 36 additions & 20 deletions internal/fudgepb/fudgepb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -51,11 +54,7 @@ func TestFromProto(t *testing.T) {
Function: "tRunner",
Line: 1576,
},
{
File: "runtime/asm_arm64.s",
Function: "goexit",
Line: 1172,
},
asmFrame,
},
},
},
Expand All @@ -80,11 +79,7 @@ func TestFromProto(t *testing.T) {
Function: "tRunner",
Line: 1576,
},
{
File: "runtime/asm_arm64.s",
Function: "goexit",
Line: 1172,
},
asmFrame,
},
},
{
Expand All @@ -108,11 +103,7 @@ func TestFromProto(t *testing.T) {
Function: "tRunner",
Line: 1576,
},
{
File: "runtime/asm_arm64.s",
Function: "goexit",
Line: 1172,
},
asmFrame,
},
},
},
Expand Down Expand Up @@ -149,11 +140,7 @@ func TestFromProto(t *testing.T) {
Function: "tRunner",
Line: 1576,
},
{
File: "runtime/asm_arm64.s",
Function: "goexit",
Line: 1172,
},
asmFrame,
},
},

Expand All @@ -178,6 +165,8 @@ func TestFromProto(t *testing.T) {
}

func TestToProto(t *testing.T) {
dummyFrame := getDummyAsmFrame()

tests := []struct {
name string
errFn func() error
Expand Down Expand Up @@ -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)
Expand All @@ -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_<arch>.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,
}
}
36 changes: 18 additions & 18 deletions internal/fudgepb/testdata/TestToProto/fudge sentinel.golden
Original file line number Diff line number Diff line change
@@ -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
}
]
}
Expand Down
54 changes: 27 additions & 27 deletions internal/fudgepb/testdata/TestToProto/fudge with kvs.golden
Original file line number Diff line number Diff line change
@@ -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
}
]
}
Expand Down
42 changes: 21 additions & 21 deletions internal/fudgepb/testdata/TestToProto/fudge wrapped.golden
Original file line number Diff line number Diff line change
@@ -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
}
]
}
Expand Down
34 changes: 17 additions & 17 deletions internal/fudgepb/testdata/TestToProto/fudge.golden
Original file line number Diff line number Diff line change
@@ -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
}
]
}
Expand Down
Loading

0 comments on commit 22497ec

Please sign in to comment.