Skip to content

Commit

Permalink
Introduce a contracts mapping in order to dictate where contracts sho…
Browse files Browse the repository at this point in the history
…uld be deployed
  • Loading branch information
m-Peter committed Sep 12, 2023
1 parent c700a4a commit b1a58de
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 108 deletions.
5 changes: 0 additions & 5 deletions test/test_framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ func (tf *TestFrameworkProvider) ReadFile(path string) (string, error) {

func (tf *TestFrameworkProvider) NewEmulatorBackend() stdlib.Blockchain {
return tf.emulatorBackend
// return NewEmulatorBackend(
// tf.fileResolver,
// tf.stdlibHandler,
// tf.coverageReport,
// )
}

func NewTestFrameworkProvider(
Expand Down
130 changes: 56 additions & 74 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,14 @@ func TestImportContract(t *testing.T) {
return fooContract, nil
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver)
WithFileResolver(fileResolver).
WithContracts(contracts)

result, err := runner.RunTest(code, "test")
require.NoError(t, err)
Expand Down Expand Up @@ -411,9 +416,14 @@ func TestImportContract(t *testing.T) {
return fooContract, nil
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver)
WithFileResolver(fileResolver).
WithContracts(contracts)

result, err := runner.RunTest(code, "test")
require.NoError(t, err)
Expand Down Expand Up @@ -715,9 +725,14 @@ func TestUsingEnv(t *testing.T) {
return fooContract, nil
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver)
WithFileResolver(fileResolver).
WithContracts(contracts)

result, err := runner.RunTest(code, "test")
require.NoError(t, err)
Expand Down Expand Up @@ -3259,6 +3274,10 @@ func TestCoverageReportForUnitTests(t *testing.T) {
return "", fmt.Errorf("cannot find import location: %s", location.ID())
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

coverageReport := runtime.NewCoverageReport()
coverageReport.WithLocationFilter(func(location common.Location) bool {
_, addressLoc := location.(common.AddressLocation)
Expand All @@ -3269,7 +3288,8 @@ func TestCoverageReportForUnitTests(t *testing.T) {
runner := NewTestRunner().
WithFileResolver(fileResolver).
WithImportResolver(importResolver).
WithCoverageReport(coverageReport)
WithCoverageReport(coverageReport).
WithContracts(contracts)

results, err := runner.RunTests(code)

Expand All @@ -3293,7 +3313,7 @@ func TestCoverageReportForUnitTests(t *testing.T) {
assert.EqualValues(
t,
map[int]int{
6: 3, 14: 1, 18: 10, 19: 1, 20: 9, 21: 1, 22: 8, 23: 1,
6: 2, 14: 1, 18: 10, 19: 1, 20: 9, 21: 1, 22: 8, 23: 1,
24: 7, 25: 1, 26: 6, 27: 1, 30: 5, 31: 4, 34: 1,
},
coverage.LineHits,
Expand Down Expand Up @@ -3514,7 +3534,7 @@ func TestCoverageReportForIntegrationTests(t *testing.T) {
assert.EqualValues(
t,
map[int]int{
6: 2, 14: 1, 18: 10, 19: 1, 20: 9, 21: 1, 22: 8, 23: 1,
6: 1, 14: 1, 18: 10, 19: 1, 20: 9, 21: 1, 22: 8, 23: 1,
24: 7, 25: 1, 26: 6, 27: 1, 30: 5, 31: 4, 34: 1,
},
coverage.LineHits,
Expand Down Expand Up @@ -3644,9 +3664,14 @@ func TestRetrieveLogsFromUnitTests(t *testing.T) {
return "", fmt.Errorf("unsupported import %s", location)
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver)
WithFileResolver(fileResolver).
WithContracts(contracts)

results, err := runner.RunTests(code)
require.NoError(t, err)
Expand Down Expand Up @@ -3753,9 +3778,14 @@ func TestRetrieveEmptyLogsFromUnitTests(t *testing.T) {
return "", fmt.Errorf("unsupported import %s", location)
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver)
WithFileResolver(fileResolver).
WithContracts(contracts)

results, err := runner.RunTests(code)
require.NoError(t, err)
Expand Down Expand Up @@ -4174,9 +4204,14 @@ func TestGetEventsFromIntegrationTests(t *testing.T) {
return "", fmt.Errorf("cannot find import location: %s", location.ID())
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithFileResolver(fileResolver).
WithImportResolver(importResolver)
WithImportResolver(importResolver).
WithContracts(contracts)

results, err := runner.RunTests(testCode)
require.NoError(t, err)
Expand Down Expand Up @@ -4526,44 +4561,6 @@ func TestRandomizedTestExecution(t *testing.T) {
assert.Equal(t, expected, resultsStr)
}

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

const code = `
import Test
import BlockchainHelpers
pub fun test() {
let blockchain = Test.newEmulatorBlockchain()
let helpers = BlockchainHelpers(blockchain: blockchain)
let account = blockchain.createAccount()
let typ = CompositeType("flow.AccountCreated")!
let events = blockchain.eventsOfType(typ)
Test.assertEqual(1, events.length)
// let blockchain2 = Test.newEmulatorBlockchain()
// let helpers2 = BlockchainHelpers(blockchain: blockchain2)
// let events2 = blockchain2.eventsOfType(typ)
// Test.assertEqual(0, events2.length)
// Test.assert(
// helpers.getCurrentBlockHeight() > helpers2.getCurrentBlockHeight()
// )
}
`

importResolver := func(location common.Location) (string, error) {
return "", nil
}

runner := NewTestRunner().WithImportResolver(importResolver)
result, err := runner.RunTest(code, "test")
require.NoError(t, err)
require.NoError(t, result.Error)
}

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

Expand Down Expand Up @@ -4676,9 +4673,14 @@ func TestReferenceDeployedContractTypes(t *testing.T) {
return "", fmt.Errorf("cannot find import location: %s", location.ID())
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithFileResolver(fileResolver).
WithImportResolver(importResolver)
WithImportResolver(importResolver).
WithContracts(contracts)

results, err := runner.RunTests(testCode)
require.NoError(t, err)
Expand Down Expand Up @@ -4777,31 +4779,6 @@ func TestReferenceDeployedContractTypes(t *testing.T) {
Test.assertEqual(1729, specialNumber.n)
Test.assertEqual("Harshad", specialNumber.trait)
}
// pub fun testNewDeploymentWithEmptyArgs() {
// let contractCode = Test.readFile("../contracts/FooContract.cdc")
// let blockchain2 = Test.newEmulatorBlockchain()
// let account2 = blockchain2.createAccount()
// let args: {Int: String} = {}
// let err = blockchain2.deployContract(
// name: "FooContract",
// code: contractCode,
// account: account2,
// arguments: [args]
// )
// Test.expect(err, Test.beNil())
// blockchain2.useConfiguration(Test.Configuration({
// "../contracts/FooContract.cdc": account2.address
// }))
// let script = Test.readFile("../scripts/get_special_number.cdc")
// let result = blockchain2.executeScript(script, [])
// Test.expect(result, Test.beSucceeded())
// let specialNumbers = result.returnValue! as! [FooContract.SpecialNumber]
// Test.expect(specialNumbers, Test.beEmpty())
// }
`

fileResolver := func(path string) (string, error) {
Expand Down Expand Up @@ -4830,9 +4807,14 @@ func TestReferenceDeployedContractTypes(t *testing.T) {
return "", fmt.Errorf("cannot find import location: %s", location.ID())
}

contracts := map[string]string{
"FooContract": "0000000000000005",
}

runner := NewTestRunner().
WithFileResolver(fileResolver).
WithImportResolver(importResolver)
WithImportResolver(importResolver).
WithContracts(contracts)

results, err := runner.RunTests(testCode)
require.NoError(t, err)
Expand Down
61 changes: 32 additions & 29 deletions test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ type TestRunner struct {
// blockchain is mainly used to obtain system-defined
// contracts & their exposed types
blockchain *emulator.Blockchain

contracts map[string]string
}

func NewTestRunner() *TestRunner {
Expand Down Expand Up @@ -183,6 +185,7 @@ func NewTestRunner() *TestRunner {
logCollection: logCollectionHook,
logger: logger,
blockchain: blockchain,
contracts: map[string]string{},
}
}

Expand All @@ -206,6 +209,11 @@ func (r *TestRunner) WithRandomSeed(seed int64) *TestRunner {
return r
}

func (r *TestRunner) WithContracts(contracts map[string]string) *TestRunner {
r.contracts = contracts
return r
}

// RunTest runs a single test in the provided test script.
func (r *TestRunner) RunTest(script string, funcName string) (result *Result, err error) {
defer func() {
Expand All @@ -214,7 +222,7 @@ func (r *TestRunner) RunTest(script string, funcName string) (result *Result, er
})
}()

program, inter, err := r.parseCheckAndInterpret(script)
_, inter, err := r.parseCheckAndInterpret(script)
if err != nil {
return nil, err
}
Expand All @@ -224,12 +232,6 @@ func (r *TestRunner) RunTest(script string, funcName string) (result *Result, er
return nil, err
}

script = replaceImports(program.Program, script)
_, inter, err = r.parseCheckAndInterpret(script)
if err != nil {
return nil, err
}

// Run test `setup()` before running the test function.
err = r.runTestSetup(inter)
if err != nil {
Expand Down Expand Up @@ -277,20 +279,8 @@ func (r *TestRunner) RunTests(script string) (results Results, err error) {
return nil, err
}

script = replaceImports(program.Program, script)
program, inter, err = r.parseCheckAndInterpret(script)
if err != nil {
return nil, err
}

results = make(Results, 0)

r.logCollection.Logs = make([]string, 0)
err = r.runTestInit(inter)
if err != nil {
return nil, err
}

// Run test `setup()` before test functions
err = r.runTestSetup(inter)
if err != nil {
Expand Down Expand Up @@ -342,7 +332,7 @@ func (r *TestRunner) RunTests(script string) (results Results, err error) {
return results, err
}

func replaceImports(program *ast.Program, code string) string {
func (r *TestRunner) replaceImports(program *ast.Program, code string) string {
sb := strings.Builder{}
importDeclEnd := 0

Expand All @@ -358,8 +348,7 @@ func replaceImports(program *ast.Program, code string) string {
}

identifier := identifiers[0].Identifier
contract := contractInvocations[identifier]
address := contract.Address
address := r.contracts[identifier]
var addressStr string
if strings.Contains(importDeclaration.String(), "from") {
addressStr = fmt.Sprintf("0x%s", address)
Expand Down Expand Up @@ -485,12 +474,6 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (*interpreter.Program
Location: testScriptLocation,
Environment: env,
}
if r.coverageReport != nil {
r.coverageReport.ExcludeLocation(stdlib.CryptoCheckerLocation)
r.coverageReport.ExcludeLocation(stdlib.TestContractLocation)
r.coverageReport.ExcludeLocation(testScriptLocation)
ctx.CoverageReport = r.coverageReport
}

// Checker configs
env.CheckerConfig.ImportHandler = r.checkerImportHandler(ctx)
Expand Down Expand Up @@ -533,8 +516,28 @@ func (r *TestRunner) parseCheckAndInterpret(script string) (*interpreter.Program
// Set the storage after checking, because `ParseAndCheckProgram` clears the storage.
env.InterpreterConfig.Storage = runtime.NewStorage(ctx.Interface, nil)

script = r.replaceImports(program.Program, script)
newCtx := runtime.Context{
Interface: newScriptEnvironment(r.logger),
Location: testScriptLocation,
Environment: env,
}
if r.coverageReport != nil {
r.coverageReport.ExcludeLocation(stdlib.CryptoCheckerLocation)
r.coverageReport.ExcludeLocation(stdlib.TestContractLocation)
r.coverageReport.ExcludeLocation(testScriptLocation)
newCtx.CoverageReport = r.coverageReport
}
program, err = r.testRuntime.ParseAndCheckProgram([]byte(script), newCtx)
if err != nil {
return nil, nil, err
}

// Set the storage after checking, because `ParseAndCheckProgram` clears the storage.
env.InterpreterConfig.Storage = runtime.NewStorage(newCtx.Interface, nil)

_, inter, err := env.Interpret(
ctx.Location,
newCtx.Location,
program,
nil,
)
Expand Down

0 comments on commit b1a58de

Please sign in to comment.