From 1b9c14d5d78599a02d07273785b95fce355582b5 Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Mon, 31 Jul 2023 17:56:24 +0300 Subject: [PATCH 1/3] [#3] Add a custom CompileFile It is a copy of neotest.CompileFile() but it returns debug info in addition to contract info. Signed-off-by: Ekaterina Lebedeva --- covertest/compile.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 covertest/compile.go diff --git a/covertest/compile.go b/covertest/compile.go new file mode 100644 index 0000000..9d67f68 --- /dev/null +++ b/covertest/compile.go @@ -0,0 +1,64 @@ +package covertest + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/cli/smartcontract" + "github.com/nspcc-dev/neo-go/pkg/compiler" + "github.com/nspcc-dev/neo-go/pkg/config" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/neotest" + "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +// ContractWithDebugInfo contains contract info for deployment and debug information for coverage. +type ContractWithDebugInfo struct { + Contract *neotest.Contract + DebugInfo *compiler.DebugInfo +} + +// contracts caches the compiled contracts from FS across multiple tests. +var contracts = make(map[string]*neotest.Contract) + +// CompileFile compiles a contract from the file and returns its NEF, manifest, hash and debug information. +func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath string) *ContractWithDebugInfo { + if c, ok := contracts[srcPath]; ok { + return c + } + + // nef.NewFile() cares about version a lot. + config.Version = "neotest" + + ne, di, err := compiler.CompileWithOptions(srcPath, nil, nil) + require.NoError(t, err) + + conf, err := smartcontract.ParseContractConfig(configPath) + require.NoError(t, err) + + o := &compiler.Options{} + o.Name = conf.Name + o.ContractEvents = conf.Events + o.ContractSupportedStandards = conf.SupportedStandards + o.Permissions = make([]manifest.Permission, len(conf.Permissions)) + for i := range conf.Permissions { + o.Permissions[i] = manifest.Permission(conf.Permissions[i]) + } + o.SafeMethods = conf.SafeMethods + o.Overloads = conf.Overloads + o.SourceURL = conf.SourceURL + m, err := compiler.CreateManifest(di, o) + require.NoError(t, err) + + c := &neotest.Contract{ + Hash: state.CreateContractHash(sender, ne.Checksum, m.Name), + NEF: ne, + Manifest: m, + } + contracts[srcPath] = c + return &ContractWithDebugInfo{ + Contract: c, + DebugInfo: di, + } +} -- 2.45.2 From 45b28b723b92eb9f8f30097bdf35a301c4c82272 Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Tue, 1 Aug 2023 16:04:18 +0300 Subject: [PATCH 2/3] [#3] Remove unnecessary caching in CompileFile At this stage there is no need for such optimization Signed-off-by: Ekaterina Lebedeva --- covertest/compile.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/covertest/compile.go b/covertest/compile.go index 9d67f68..0b9c3b7 100644 --- a/covertest/compile.go +++ b/covertest/compile.go @@ -19,15 +19,8 @@ type ContractWithDebugInfo struct { DebugInfo *compiler.DebugInfo } -// contracts caches the compiled contracts from FS across multiple tests. -var contracts = make(map[string]*neotest.Contract) - // CompileFile compiles a contract from the file and returns its NEF, manifest, hash and debug information. func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath string) *ContractWithDebugInfo { - if c, ok := contracts[srcPath]; ok { - return c - } - // nef.NewFile() cares about version a lot. config.Version = "neotest" @@ -56,7 +49,6 @@ func CompileFile(t testing.TB, sender util.Uint160, srcPath string, configPath s NEF: ne, Manifest: m, } - contracts[srcPath] = c return &ContractWithDebugInfo{ Contract: c, DebugInfo: di, -- 2.45.2 From 6519282570c38ba882555047cccfcb03a05c82ba Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Tue, 1 Aug 2023 16:09:07 +0300 Subject: [PATCH 3/3] [#3] Change neotest to covertest CompileFile in tests Signed-off-by: Ekaterina Lebedeva --- tests/contract_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/contract_test.go b/tests/contract_test.go index ab20fd9..deac7af 100644 --- a/tests/contract_test.go +++ b/tests/contract_test.go @@ -4,6 +4,7 @@ import ( "path" "testing" + "git.frostfs.info/TrueCloudLab/contract-coverage-primer/covertest" "github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest/chain" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -24,7 +25,8 @@ func newExecutor(t *testing.T) *neotest.Executor { func TestContract(t *testing.T) { e := newExecutor(t) - ctr := neotest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "config.yml")) + ctrDI := covertest.CompileFile(t, e.CommitteeHash, ctrPath, path.Join(ctrPath, "config.yml")) + ctr := ctrDI.Contract e.DeployContract(t, ctr, nil) inv := e.CommitteeInvoker(ctr.Hash) -- 2.45.2