Skip to content

Commit d50d729

Browse files
authored
tflog+tfsdklog: Add root and subsystem unit testing (#33)
Reference: #31 Backfill unit testing for the following functions: - `With()` / `SubsystemWith()` - `Trace()`/ `SubsystemTrace()` - `Debug()`/ `SubsystemDebug()` - `Info()`/ `SubsystemInfo()` - `Warn()`/ `SubsystemWarn()` - `Error()`/ `SubsystemError()`
1 parent 942943d commit d50d729

File tree

10 files changed

+2026
-1
lines changed

10 files changed

+2026
-1
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ linters:
1414
- ineffassign
1515
- makezero
1616
- nilerr
17-
- paralleltest
17+
# - paralleltest # Reference: https://github.com/kunwardeep/paralleltest/issues/14
1818
- predeclared
1919
- staticcheck
2020
# - tenv # TODO: Enable when upgrading Go 1.16 to 1.17

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/hashicorp/terraform-plugin-log
33
go 1.16
44

55
require (
6+
github.com/google/go-cmp v0.5.7
67
github.com/hashicorp/go-hclog v1.1.0
78
github.com/mitchellh/go-testing-interface v1.14.1
89
github.com/stretchr/testify v1.3.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
55
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
6+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
7+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
68
github.com/hashicorp/go-hclog v1.1.0 h1:QsGcniKx5/LuX2eYoeL+Np3UKYPNaN7YKpTh29h8rbw=
79
github.com/hashicorp/go-hclog v1.1.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
810
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
@@ -21,3 +23,5 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
2123
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2224
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
2325
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
26+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
27+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/loggertest/json_decode.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package loggertest
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
)
8+
9+
func MultilineJSONDecode(data io.Reader) ([]map[string]interface{}, error) {
10+
var result []map[string]interface{}
11+
var entry map[string]interface{}
12+
13+
dec := json.NewDecoder(data)
14+
15+
for {
16+
err := dec.Decode(&entry)
17+
18+
if err == io.EOF {
19+
break
20+
}
21+
22+
if err != nil {
23+
return result, fmt.Errorf("unable to decode JSON: %s", err)
24+
}
25+
26+
result = append(result, entry)
27+
}
28+
29+
return result, nil
30+
}

internal/loggertest/provider.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package loggertest
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/hashicorp/go-hclog"
8+
"github.com/hashicorp/terraform-plugin-log/internal/logging"
9+
)
10+
11+
func ProviderRoot(ctx context.Context, output io.Writer) context.Context {
12+
loggerOptions := &hclog.LoggerOptions{
13+
DisableTime: true,
14+
JSONFormat: true,
15+
Level: hclog.Trace,
16+
Output: output,
17+
}
18+
19+
ctx = logging.SetProviderRootLogger(ctx, hclog.New(loggerOptions))
20+
21+
return ctx
22+
}

internal/loggertest/sdk.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package loggertest
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/hashicorp/go-hclog"
8+
"github.com/hashicorp/terraform-plugin-log/internal/logging"
9+
)
10+
11+
func SDKRoot(ctx context.Context, output io.Writer) context.Context {
12+
loggerOptions := &hclog.LoggerOptions{
13+
DisableTime: true,
14+
JSONFormat: true,
15+
Level: hclog.Trace,
16+
Output: output,
17+
}
18+
19+
ctx = logging.SetSDKRootLogger(ctx, hclog.New(loggerOptions))
20+
21+
return ctx
22+
}

0 commit comments

Comments
 (0)