Skip to content

Commit bff406a

Browse files
committed
tfsdklog: Add WithAdditionalLocationOffset unit testing
1 parent d30f5b2 commit bff406a

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

tfsdklog/options_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package tfsdklog_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"strings"
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
"github.com/hashicorp/go-hclog"
11+
"github.com/hashicorp/terraform-plugin-log/internal/loggertest"
12+
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
13+
)
14+
15+
func testSubsystemTraceHelper(ctx context.Context, message string) {
16+
tfsdklog.SubsystemTrace(ctx, testSubsystem, message)
17+
}
18+
19+
func TestWithAdditionalLocationOffset(t *testing.T) {
20+
t.Parallel()
21+
22+
testCases := map[string]struct {
23+
additionalLocationOffset int
24+
logImpl func(context.Context)
25+
expectedOutput []map[string]interface{}
26+
}{
27+
"0-no-helper": {
28+
additionalLocationOffset: 0,
29+
logImpl: func(ctx context.Context) {
30+
tfsdklog.SubsystemTrace(ctx, testSubsystem, "test message")
31+
},
32+
expectedOutput: []map[string]interface{}{
33+
{
34+
// Caller line (number after colon) should match
35+
// tfsdklog.SubsystemTrace() line in test case implementation.
36+
"@caller": "/tfsdklog/options_test.go:30",
37+
"@level": hclog.Trace.String(),
38+
"@message": "test message",
39+
"@module": testSubsystemModule,
40+
},
41+
},
42+
},
43+
"0-one-helper": {
44+
additionalLocationOffset: 0,
45+
logImpl: func(ctx context.Context) {
46+
testSubsystemTraceHelper(ctx, "test message")
47+
},
48+
expectedOutput: []map[string]interface{}{
49+
{
50+
// Caller line (number after colon) should match
51+
// tfsdklog.SubsystemTrace() line in testSubsystemTraceHelper
52+
// function implementation.
53+
"@caller": "/tfsdklog/options_test.go:16",
54+
"@level": hclog.Trace.String(),
55+
"@message": "test message",
56+
"@module": testSubsystemModule,
57+
},
58+
},
59+
},
60+
"1-one-helper": {
61+
additionalLocationOffset: 1,
62+
logImpl: func(ctx context.Context) {
63+
testSubsystemTraceHelper(ctx, "test message")
64+
},
65+
expectedOutput: []map[string]interface{}{
66+
{
67+
// Caller line (number after colon) should match
68+
// testSubsystemTraceHelper() line in test case
69+
// implementation.
70+
"@caller": "/tfsdklog/options_test.go:63",
71+
"@level": hclog.Trace.String(),
72+
"@message": "test message",
73+
"@module": testSubsystemModule,
74+
},
75+
},
76+
},
77+
}
78+
79+
for name, testCase := range testCases {
80+
name, testCase := name, testCase
81+
82+
t.Run(name, func(t *testing.T) {
83+
t.Parallel()
84+
85+
var outputBuffer bytes.Buffer
86+
87+
ctx := context.Background()
88+
ctx = loggertest.SDKRootWithLocation(ctx, &outputBuffer)
89+
ctx = tfsdklog.NewSubsystem(ctx, testSubsystem, tfsdklog.WithAdditionalLocationOffset(testCase.additionalLocationOffset))
90+
91+
testCase.logImpl(ctx)
92+
93+
got, err := loggertest.MultilineJSONDecode(&outputBuffer)
94+
95+
if err != nil {
96+
t.Fatalf("unable to read multiple line JSON: %s", err)
97+
}
98+
99+
// Strip non-deterministic caller information up to this package, e.g.
100+
// /Users/example/src/github.com/hashicorp/terraform-plugin-log/tfsdklog/...
101+
for _, gotEntry := range got {
102+
caller, ok := gotEntry["@caller"].(string)
103+
104+
if !ok {
105+
continue
106+
}
107+
108+
packageIndex := strings.Index(caller, "/tfsdklog/")
109+
110+
if packageIndex == -1 {
111+
continue
112+
}
113+
114+
gotEntry["@caller"] = caller[packageIndex:]
115+
}
116+
117+
if diff := cmp.Diff(testCase.expectedOutput, got); diff != "" {
118+
t.Errorf("unexpected output difference: %s", diff)
119+
}
120+
})
121+
}
122+
}

0 commit comments

Comments
 (0)