Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Enable storage limit for emulator backend #271

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func NewEmulatorBackend(
logger zerolog.Logger,
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
storageLimitEnabled bool,
) *EmulatorBackend {
logCollectionHook := newLogCollectionHook()
var blockchain *emulator.Blockchain
Expand All @@ -160,11 +161,13 @@ func NewEmulatorBackend(
logger,
logCollectionHook,
emulator.WithCoverageReport(coverageReport),
emulator.WithStorageLimitEnabled(storageLimitEnabled),
)
} else {
blockchain = newBlockchain(
logger,
logCollectionHook,
emulator.WithStorageLimitEnabled(storageLimitEnabled),
)
}
clock := newSystemClock()
Expand Down Expand Up @@ -741,7 +744,6 @@ func newBlockchain(
b, err := emulator.New(
append(
[]emulator.Option{
emulator.WithStorageLimitEnabled(false),
emulator.WithServerLogger(testLogger),
emulator.Contracts(commonContracts),
emulator.WithChainID(chain.ChainID()),
Expand Down
2 changes: 2 additions & 0 deletions test/test_framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func NewTestFrameworkProvider(
fileResolver FileResolver,
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
storageLimitEnabled bool,
) stdlib.TestFramework {
return &TestFrameworkProvider{
fileResolver: fileResolver,
Expand All @@ -81,6 +82,7 @@ func NewTestFrameworkProvider(
logger,
stdlibHandler,
coverageReport,
storageLimitEnabled,
),
}
}
59 changes: 52 additions & 7 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3556,7 +3556,7 @@ func TestReplacingImports(t *testing.T) {
func TestReplaceImports(t *testing.T) {
t.Parallel()

emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil)
emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil, false)
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 @@ -3801,7 +3801,7 @@ func TestServiceAccount(t *testing.T) {
t.Run("retrieve from EmulatorBackend", func(t *testing.T) {
t.Parallel()

emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil)
emulatorBackend := NewEmulatorBackend(zerolog.Nop(), nil, nil, false)

serviceAccount, err := emulatorBackend.ServiceAccount()

Expand Down Expand Up @@ -4831,9 +4831,10 @@ func TestWithLogger(t *testing.T) {
t.Parallel()

const code = `
access(all) fun testWithLogger() {
log("Hello, world!")
}
access(all)
fun testWithLogger() {
log("Hello, world!")
}
`

var buf bytes.Buffer
Expand All @@ -4845,8 +4846,8 @@ func TestWithLogger(t *testing.T) {
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())
assert.Contains(t, buf.String(), "Hello, world!")
assert.Equal(t, []string{"Hello, world!"}, runner.Logs())
}

func TestGetEventsFromIntegrationTests(t *testing.T) {
Expand Down Expand Up @@ -5799,3 +5800,47 @@ func TestGetTests(t *testing.T) {

assert.ElementsMatch(t, []string{"test1", "test2", "test3"}, tests)
}

func TestWithStorageLimitEnabled(t *testing.T) {
t.Parallel()

const code = `
#storageLimitEnabled

import Test
import BlockchainHelpers

access(all)
fun test() {
let acc = getAccount(0x10)
Test.assertEqual(0.001, acc.balance)

let account = Test.serviceAccount()
var balance = getFlowBalance(account: account)
Test.assertEqual(999999999.977, balance)

let admin = Test.createAccount()

balance = getFlowBalance(account: admin)
Test.assertEqual(0.001, balance)

mintFlow(to: admin, amount: 1000.0)

balance = getFlowBalance(account: admin)
Test.assertEqual(1000.001, balance)

let txResult = burnFlow(from: admin, amount: 1000.001)
Test.expect(txResult, Test.beFailed())
Test.assertError(txResult, errorMessage: "storage limit check failed")

balance = getFlowBalance(account: admin)
Test.assertEqual(1000.001, balance)
}
`

runner := NewTestRunner()
result, err := runner.RunTest(code, "test")

require.NoError(t, err)
require.NoError(t, result.Error)
}
23 changes: 18 additions & 5 deletions test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const beforeEachFunctionName = "beforeEach"

const afterEachFunctionName = "afterEach"

const storageLimitEnabledPragmaIdentifier = "#storageLimitEnabled"

var testScriptLocation = common.NewScriptLocation(nil, []byte("test"))

var quotedLog = regexp.MustCompile("\"(.*)\"")
Expand Down Expand Up @@ -148,7 +150,7 @@ type TestRunner struct {

func NewTestRunner() *TestRunner {
return &TestRunner{
logger: zerolog.Nop(),
logger: zerolog.Nop(),
contracts: baseContracts(),
}
}
Expand Down Expand Up @@ -403,9 +405,6 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (
*interpreter.Interpreter,
error,
) {
// TODO: move this eventually to the `NewTestRunner`
env, ctx := r.initializeEnvironment()

astProgram, err := parser.ParseProgram(nil, []byte(script), parser.Config{})
if err != nil {
return nil, nil, err
Expand All @@ -427,6 +426,19 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (
}
}

storageLimitEnabled := false
for _, pragmaDecl := range astProgram.PragmaDeclarations() {
pragmaIdentifier := pragmaDecl.String()

if pragmaIdentifier == storageLimitEnabledPragmaIdentifier {
storageLimitEnabled = true
break
}
}

// TODO: move this eventually to the `NewTestRunner`
env, ctx := r.initializeEnvironment(storageLimitEnabled)

script = r.replaceImports(script)

program, err := env.ParseAndCheckProgram([]byte(script), ctx.Location, false)
Expand All @@ -447,7 +459,7 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (
return program, inter, nil
}

func (r *TestRunner) initializeEnvironment() (
func (r *TestRunner) initializeEnvironment(storageLimitEnabled bool) (
runtime.Environment,
runtime.Context,
) {
Expand All @@ -465,6 +477,7 @@ func (r *TestRunner) initializeEnvironment() (
r.fileResolver,
env,
r.coverageReport,
storageLimitEnabled,
)
backend, ok := r.testFramework.EmulatorBackend().(*EmulatorBackend)
if !ok {
Expand Down
Loading