Skip to content

Commit

Permalink
Merge pull request #270 from onflow/jribbink/test-v0.14-logger
Browse files Browse the repository at this point in the history
[Test] Merge pull request #267 from onflow/jribbink/add-logger (test/v0.14)
  • Loading branch information
jribbink authored Jan 4, 2024
2 parents 5b04c6e + 3af615b commit d56a79c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
10 changes: 6 additions & 4 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"encoding/hex"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -148,6 +147,7 @@ var systemContracts = func() []common.AddressLocation {
}()

func NewEmulatorBackend(
logger zerolog.Logger,
fileResolver FileResolver,
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
Expand All @@ -157,11 +157,13 @@ func NewEmulatorBackend(
if coverageReport != nil {
excludeCommonLocations(coverageReport)
blockchain = newBlockchain(
logger,
logCollectionHook,
emulator.WithCoverageReport(coverageReport),
)
} else {
blockchain = newBlockchain(
logger,
logCollectionHook,
)
}
Expand Down Expand Up @@ -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()),
},
Expand Down
3 changes: 3 additions & 0 deletions test/test_framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/rs/zerolog"
)

var _ stdlib.TestFramework = &TestFrameworkProvider{}
Expand Down Expand Up @@ -67,6 +68,7 @@ func (tf *TestFrameworkProvider) EmulatorBackend() stdlib.Blockchain {
}

func NewTestFrameworkProvider(
logger zerolog.Logger,
fileResolver FileResolver,
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
Expand All @@ -76,6 +78,7 @@ func NewTestFrameworkProvider(
stdlibHandler: stdlibHandler,
coverageReport: coverageReport,
emulatorBackend: NewEmulatorBackend(
logger,
fileResolver,
stdlibHandler,
coverageReport,
Expand Down
28 changes: 26 additions & 2 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -455,6 +462,7 @@ func (r *TestRunner) initializeEnvironment() (
r.testRuntime = runtime.NewInterpreterRuntime(config)

r.testFramework = NewTestFrameworkProvider(
r.logger,
r.fileResolver,
env,
r.coverageReport,
Expand Down

0 comments on commit d56a79c

Please sign in to comment.