Skip to content

Commit

Permalink
Add bridgetest and bridgetest.InitLogging
Browse files Browse the repository at this point in the history
This allows modules outside of the bridge to test code that uses
`tfbridge.GetLogger`. This will be used by `dynamic`. It may also be used by AWS, which
currently has test code which uses the now moved-to-internal logging.InitContext.
  • Loading branch information
iwahbe committed Dec 13, 2024
1 parent f7f43b6 commit 4f8782f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/bridgetest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# bridgetest

The `bridgetest` module adds facilities for providers to use when testing bridge
integrations. Nothing in this module should be depended on for runtime functionality.
39 changes: 39 additions & 0 deletions pkg/bridgetest/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package bridgetest

import (
"context"
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"

"github.com/pulumi/pulumi-terraform-bridge/v3/internal/logging"
)

// A sink that [tfbridge.GetLogger] can write to.
type LoggingSink interface {
Log(context context.Context, sev diag.Severity, urn resource.URN, msg string) error
LogStatus(context context.Context, sev diag.Severity, urn resource.URN, msg string) error
}

type discardSink struct{}

func (*discardSink) Log(context.Context, diag.Severity, resource.URN, string) error {
return nil
}

func (*discardSink) LogStatus(context.Context, diag.Severity, resource.URN, string) error {
return nil
}

// InitLogging equips ctx with a logger usable by [tfbridge.GetLogger].
//
//nolint:revive // Let t come before ctx.
func InitLogging(t *testing.T, ctx context.Context, sink LoggingSink) context.Context {
contract.Assertf(t != nil, "t cannot be nil")
if sink == nil {
sink = &discardSink{}
}
return logging.InitLogging(ctx, logging.LogOptions{LogSink: sink})
}

0 comments on commit 4f8782f

Please sign in to comment.