cli/smartcontract: set correct version for pkg/interop

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-01-14 14:18:33 +03:00
parent 492db89576
commit 945b56e350
3 changed files with 30 additions and 10 deletions

View file

@ -13,7 +13,8 @@ DC_FILE=.docker/docker-compose.yml
GOOS ?= $(shell go env GOOS)
REPO ?= "$(shell go list -m)"
VERSION ?= "$(shell git describe --tags 2>/dev/null | sed 's/^v//')$(shell if [ "$(GOOS)" = "windows" ]; then echo "_unsupported"; fi)"
BUILD_FLAGS = "-X '$(REPO)/pkg/config.Version=$(VERSION)'"
MODVERSION ?= "$(shell cat go.mod | cat go.mod | sed -r -n -e 's|.*pkg/interop (.*)|\1|p')"
BUILD_FLAGS = "-X '$(REPO)/pkg/config.Version=$(VERSION)' -X '$(REPO)/cli/smartcontract.ModVersion=$(MODVERSION)'"
IMAGE_REPO=nspccdev/neo-go

View file

@ -5,12 +5,12 @@ import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/nspcc-dev/neo-go/cli/smartcontract"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
@ -83,6 +83,12 @@ func TestCalcHash(t *testing.T) {
}
func TestContractInitAndCompile(t *testing.T) {
// For proper nef generation.
config.Version = "v0.98.1-test"
// For proper contract init. The actual version as it will be replaced.
smartcontract.ModVersion = "v0.0.0"
tmpDir := t.TempDir()
e := newExecutor(t, false)
@ -100,9 +106,6 @@ func TestContractInitAndCompile(t *testing.T) {
e.RunWithError(t, "neo-go", "contract", "init", "--name", ctrPath)
})
// For proper nef generation.
config.Version = "0.90.0-test"
srcPath := filepath.Join(ctrPath, "main.go")
cfgPath := filepath.Join(ctrPath, "neo-go.yml")
nefPath := filepath.Join(tmpDir, "testcontract.nef")
@ -121,10 +124,16 @@ func TestContractInitAndCompile(t *testing.T) {
e.RunWithError(t, append(cmd, "--config", cfgName)...)
})
// FIXME too bad
c := exec.Command("go", "mod", "tidy")
c.Dir = ctrPath
require.NoError(t, c.Run())
// Replace `pkg/interop` in go.mod to avoid getting an actual module version.
goMod := filepath.Join(ctrPath, "go.mod")
data, err := ioutil.ReadFile(goMod)
require.NoError(t, err)
wd, err := os.Getwd()
require.NoError(t, err)
data = append(data, "\nreplace github.com/nspcc-dev/neo-go/pkg/interop => "...)
data = append(data, filepath.Join(wd, "../pkg/interop")...)
require.NoError(t, ioutil.WriteFile(goMod, data, os.ModePerm))
cmd = append(cmd, "--config", cfgPath)
e.Run(t, cmd...)

View file

@ -69,6 +69,10 @@ var (
}
)
// ModVersion contains `pkg/interop` module version
// suitable to be used in go.mod.
var ModVersion string
const (
// smartContractTmpl is written to a file when used with `init` command.
// %s is parsed to be the smartContractName.
@ -449,9 +453,15 @@ func initSmartContract(ctx *cli.Context) error {
if err := ioutil.WriteFile(filepath.Join(basePath, "neo-go.yml"), b, 0644); err != nil {
return cli.NewExitError(err, 1)
}
ver := ModVersion
if ver == "" {
ver = "latest"
}
gm := []byte("module " + contractName + `
require (
github.com/nspcc-dev/neo-go/pkg/interop latest
github.com/nspcc-dev/neo-go/pkg/interop ` + ver + `
)`)
if err := ioutil.WriteFile(filepath.Join(basePath, "go.mod"), gm, 0644); err != nil {
return cli.NewExitError(err, 1)