From 3af615b9195b8865c99cd86f9aa624221ff265a7 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Tue, 2 Jan 2024 08:53:23 -0800 Subject: [PATCH] Merge pull request #267 from onflow/jribbink/add-logger [Test] Add WithLogger to TestRunner and default to zerolog.Nop() --- test/emulator_backend.go | 10 ++++++---- test/test_framework_provider.go | 3 +++ test/test_framework_test.go | 28 ++++++++++++++++++++++++++-- test/test_runner.go | 8 ++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/test/emulator_backend.go b/test/emulator_backend.go index 9854a78c..f597de84 100644 --- a/test/emulator_backend.go +++ b/test/emulator_backend.go @@ -22,7 +22,6 @@ import ( "context" "encoding/hex" "fmt" - "os" "strings" "time" @@ -148,6 +147,7 @@ var systemContracts = func() []common.AddressLocation { }() func NewEmulatorBackend( + logger zerolog.Logger, fileResolver FileResolver, stdlibHandler stdlib.StandardLibraryHandler, coverageReport *runtime.CoverageReport, @@ -157,11 +157,13 @@ func NewEmulatorBackend( if coverageReport != nil { excludeCommonLocations(coverageReport) blockchain = newBlockchain( + logger, logCollectionHook, emulator.WithCoverageReport(coverageReport), ) } else { blockchain = newBlockchain( + logger, logCollectionHook, ) } @@ -711,18 +713,18 @@ func (e *EmulatorBackend) replaceImports(code string) string { // newBlockchain returns an emulator blockchain for testing. func newBlockchain( + logger zerolog.Logger, hook *logCollectionHook, opts ...emulator.Option, ) *emulator.Blockchain { - output := zerolog.ConsoleWriter{Out: os.Stdout} - logger := zerolog.New(output).With().Timestamp(). + testLogger := logger.With().Timestamp(). Logger().Hook(hook).Level(zerolog.InfoLevel) b, err := emulator.New( append( []emulator.Option{ emulator.WithStorageLimitEnabled(false), - emulator.WithServerLogger(logger), + emulator.WithServerLogger(testLogger), emulator.Contracts(commonContracts), emulator.WithChainID(chain.ChainID()), }, diff --git a/test/test_framework_provider.go b/test/test_framework_provider.go index ee13782f..6a0f4dbe 100644 --- a/test/test_framework_provider.go +++ b/test/test_framework_provider.go @@ -23,6 +23,7 @@ import ( "github.com/onflow/cadence/runtime" "github.com/onflow/cadence/runtime/stdlib" + "github.com/rs/zerolog" ) var _ stdlib.TestFramework = &TestFrameworkProvider{} @@ -67,6 +68,7 @@ func (tf *TestFrameworkProvider) EmulatorBackend() stdlib.Blockchain { } func NewTestFrameworkProvider( + logger zerolog.Logger, fileResolver FileResolver, stdlibHandler stdlib.StandardLibraryHandler, coverageReport *runtime.CoverageReport, @@ -76,6 +78,7 @@ func NewTestFrameworkProvider( stdlibHandler: stdlibHandler, coverageReport: coverageReport, emulatorBackend: NewEmulatorBackend( + logger, fileResolver, stdlibHandler, coverageReport, diff --git a/test/test_framework_test.go b/test/test_framework_test.go index f4870ed7..334e45ed 100644 --- a/test/test_framework_test.go +++ b/test/test_framework_test.go @@ -19,10 +19,12 @@ package test import ( + "bytes" "errors" "fmt" "testing" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -3445,7 +3447,7 @@ func TestReplacingImports(t *testing.T) { func TestReplaceImports(t *testing.T) { t.Parallel() - emulatorBackend := NewEmulatorBackend(nil, nil, nil) + emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil, nil) emulatorBackend.contracts = map[string]common.Address{ "C1": {0, 0, 0, 0, 0, 0, 0, 1}, "C2": {0, 0, 0, 0, 0, 0, 0, 2}, @@ -3696,7 +3698,7 @@ func TestServiceAccount(t *testing.T) { t.Run("retrieve from EmulatorBackend", func(t *testing.T) { t.Parallel() - emulatorBackend := NewEmulatorBackend(nil, nil, nil) + emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil, nil) serviceAccount, err := emulatorBackend.ServiceAccount() @@ -4705,6 +4707,28 @@ func TestRetrieveEmptyLogsFromIntegrationTests(t *testing.T) { } } +func TestWithLogger(t *testing.T) { + t.Parallel() + + const code = ` + access(all) fun testWithLogger() { + log("Hello, world!") + } + ` + + var buf bytes.Buffer + logger := zerolog.New(&buf) + + runner := NewTestRunner().WithLogger(logger) + + result, err := runner.RunTest(code, "testWithLogger") + require.NoError(t, err) + require.NoError(t, result.Error) + + expectedPattern := `{"level":"info","time":"[0-9TZ:.-]+","message":"\\u001b\[1;34mLOG:\\u001b\[0m \\"Hello, world!\\""}` + assert.Regexp(t, expectedPattern, buf.String()) +} + func TestGetEventsFromIntegrationTests(t *testing.T) { t.Parallel() diff --git a/test/test_runner.go b/test/test_runner.go index 99d1ed70..e2f33657 100644 --- a/test/test_runner.go +++ b/test/test_runner.go @@ -120,6 +120,7 @@ type FileResolver func(path string) (string, error) // TestRunner runs tests. type TestRunner struct { + logger zerolog.Logger // importResolver is used to resolve imports of the *test script*. // Note: This doesn't resolve the imports for the code that is being tested. @@ -146,10 +147,16 @@ type TestRunner struct { func NewTestRunner() *TestRunner { return &TestRunner{ + logger: zerolog.Nop(), contracts: baseContracts(), } } +func (r *TestRunner) WithLogger(logger zerolog.Logger) *TestRunner { + r.logger = logger + return r +} + func (r *TestRunner) WithImportResolver(importResolver ImportResolver) *TestRunner { r.importResolver = importResolver return r @@ -455,6 +462,7 @@ func (r *TestRunner) initializeEnvironment() ( r.testRuntime = runtime.NewInterpreterRuntime(config) r.testFramework = NewTestFrameworkProvider( + r.logger, r.fileResolver, env, r.coverageReport,