Skip to content

Commit

Permalink
Prefer using raw fixtures in testing (#74)
Browse files Browse the repository at this point in the history
* Prefer the use of raw fixtures during testing

This is to avoid any side-effect from changes that are outside the immediate domain of this library

* Fix typos

* Ignore JetBrains IDEs project files

* Fixed fixtures
  • Loading branch information
Ivan De Marino committed Jul 13, 2022
1 parent 0ba79c9 commit 0f9eafb
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 123 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1,2 +1,6 @@
node_modules
website-preview
website-preview

# Jetbrains IDEs
.idea/
*.iws
17 changes: 8 additions & 9 deletions tflog/options_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-log/internal/loggertest"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand All @@ -33,8 +32,8 @@ func TestWithAdditionalLocationOffset(t *testing.T) {
{
// Caller line (number after colon) should match
// tflog.SubsystemTrace() line in test case implementation.
"@caller": "/tflog/options_test.go:30",
"@level": hclog.Trace.String(),
"@caller": "/tflog/options_test.go:29",
"@level": "trace",
"@message": "test message",
"@module": testSubsystemModule,
},
Expand All @@ -50,8 +49,8 @@ func TestWithAdditionalLocationOffset(t *testing.T) {
// Caller line (number after colon) should match
// tflog.SubsystemTrace() line in testSubsystemTraceHelper
// function implementation.
"@caller": "/tflog/options_test.go:16",
"@level": hclog.Trace.String(),
"@caller": "/tflog/options_test.go:15",
"@level": "trace",
"@message": "test message",
"@module": testSubsystemModule,
},
Expand All @@ -67,8 +66,8 @@ func TestWithAdditionalLocationOffset(t *testing.T) {
// Caller line (number after colon) should match
// testSubsystemTraceHelper() line in test case
// implementation.
"@caller": "/tflog/options_test.go:63",
"@level": hclog.Trace.String(),
"@caller": "/tflog/options_test.go:62",
"@level": "trace",
"@message": "test message",
"@module": testSubsystemModule,
},
Expand Down Expand Up @@ -137,7 +136,7 @@ func TestWithRootFields(t *testing.T) {
logMessage: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": testSubsystemModule,
"test-subsystem-key": "test-subsystem-value",
Expand All @@ -154,7 +153,7 @@ func TestWithRootFields(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": testSubsystemModule,
"test-root-key": "test-root-value",
Expand Down
51 changes: 25 additions & 26 deletions tflog/provider_test.go
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-log/internal/loggertest"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand All @@ -15,19 +14,19 @@ func TestWith(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
key string
value interface{}
logMessage string
logadditionalFields []map[string]interface{}
expectedOutput []map[string]interface{}
key string
value interface{}
logMessage string
additionalFields []map[string]interface{}
expectedOutput []map[string]interface{}
}{
"no-log-fields": {
key: "test-with-key",
value: "test-with-value",
logMessage: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
"test-with-key": "test-with-value",
Expand All @@ -39,7 +38,7 @@ func TestWith(t *testing.T) {
logMessage: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
"unfielded-test-with-key": nil,
Expand All @@ -50,7 +49,7 @@ func TestWith(t *testing.T) {
key: "test-with-key",
value: "test-with-value",
logMessage: "test message",
logadditionalFields: []map[string]interface{}{
additionalFields: []map[string]interface{}{
{
"test-log-key-1": "test-log-value-1",
"test-log-key-2": "test-log-value-2",
Expand All @@ -59,7 +58,7 @@ func TestWith(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
"test-log-key-1": "test-log-value-1",
Expand All @@ -83,7 +82,7 @@ func TestWith(t *testing.T) {
ctx = loggertest.ProviderRoot(ctx, &outputBuffer)
ctx = tflog.With(ctx, testCase.key, testCase.value)

tflog.Trace(ctx, testCase.logMessage, testCase.logadditionalFields...)
tflog.Trace(ctx, testCase.logMessage, testCase.additionalFields...)

got, err := loggertest.MultilineJSONDecode(&outputBuffer)

Expand All @@ -110,7 +109,7 @@ func TestTrace(t *testing.T) {
message: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
},
Expand All @@ -127,7 +126,7 @@ func TestTrace(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1",
Expand All @@ -152,7 +151,7 @@ func TestTrace(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@level": "trace",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1-map2",
Expand Down Expand Up @@ -203,7 +202,7 @@ func TestDebug(t *testing.T) {
message: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Debug.String(),
"@level": "debug",
"@message": "test message",
"@module": "provider",
},
Expand All @@ -220,7 +219,7 @@ func TestDebug(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Debug.String(),
"@level": "debug",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1",
Expand All @@ -245,7 +244,7 @@ func TestDebug(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Debug.String(),
"@level": "debug",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1-map2",
Expand Down Expand Up @@ -296,7 +295,7 @@ func TestInfo(t *testing.T) {
message: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Info.String(),
"@level": "info",
"@message": "test message",
"@module": "provider",
},
Expand All @@ -313,7 +312,7 @@ func TestInfo(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Info.String(),
"@level": "info",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1",
Expand All @@ -338,7 +337,7 @@ func TestInfo(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Info.String(),
"@level": "info",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1-map2",
Expand Down Expand Up @@ -389,7 +388,7 @@ func TestWarn(t *testing.T) {
message: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Warn.String(),
"@level": "warn",
"@message": "test message",
"@module": "provider",
},
Expand All @@ -406,7 +405,7 @@ func TestWarn(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Warn.String(),
"@level": "warn",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1",
Expand All @@ -431,7 +430,7 @@ func TestWarn(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Warn.String(),
"@level": "warn",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1-map2",
Expand Down Expand Up @@ -482,7 +481,7 @@ func TestError(t *testing.T) {
message: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Error.String(),
"@level": "error",
"@message": "test message",
"@module": "provider",
},
Expand All @@ -499,7 +498,7 @@ func TestError(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Error.String(),
"@level": "error",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1",
Expand All @@ -524,7 +523,7 @@ func TestError(t *testing.T) {
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Error.String(),
"@level": "error",
"@message": "test message",
"@module": "provider",
"test-key-1": "test-value-1-map2",
Expand Down

0 comments on commit 0f9eafb

Please sign in to comment.