tests: fix test binary flags for Go 1.13

Calling flag.Parse during package initialization is prohibited
as of Go 1.13 and causes test failures. Call it in TestMain instead.
This commit is contained in:
Felix Lange 2019-09-11 13:19:20 +02:00
parent b4c771f0cd
commit a7cb0c5ab0
2 changed files with 19 additions and 13 deletions

View file

@ -18,6 +18,7 @@ package tests
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
@ -33,6 +34,17 @@ import (
"github.com/ethereum/go-ethereum/params"
)
// Command line flags to configure the interpreters.
var (
testEVM = flag.String("vm.evm", "", "EVM configuration")
testEWASM = flag.String("vm.ewasm", "", "EWASM configuration")
)
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
var (
baseDir = filepath.Join(".", "testdata")
blockTestDir = filepath.Join(baseDir, "BlockchainTests")

View file

@ -19,12 +19,10 @@ package tests
import (
"bufio"
"bytes"
"flag"
"fmt"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/core/vm"
)
@ -71,20 +69,15 @@ func TestState(t *testing.T) {
// Transactions with gasLimit above this value will not get a VM trace on failure.
const traceErrorLimit = 400000
// The VM config for state tests that accepts --vm.* command line arguments.
var testVMConfig = func() vm.Config {
vmconfig := vm.Config{}
flag.StringVar(&vmconfig.EVMInterpreter, utils.EVMInterpreterFlag.Name, utils.EVMInterpreterFlag.Value, utils.EVMInterpreterFlag.Usage)
flag.StringVar(&vmconfig.EWASMInterpreter, utils.EWASMInterpreterFlag.Name, utils.EWASMInterpreterFlag.Value, utils.EWASMInterpreterFlag.Usage)
flag.Parse()
return vmconfig
}()
func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
err := test(testVMConfig)
// Use config from command line arguments.
config := vm.Config{EVMInterpreter: *testEVM, EWASMInterpreter: *testEWASM}
err := test(config)
if err == nil {
return
}
// Test failed, re-run with tracing enabled.
t.Error(err)
if gasLimit > traceErrorLimit {
t.Log("gas limit too high for EVM trace")
@ -93,7 +86,8 @@ func withTrace(t *testing.T, gasLimit uint64, test func(vm.Config) error) {
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
tracer := vm.NewJSONLogger(&vm.LogConfig{DisableMemory: true}, w)
err2 := test(vm.Config{Debug: true, Tracer: tracer})
config.Debug, config.Tracer = true, tracer
err2 := test(config)
if !reflect.DeepEqual(err, err2) {
t.Errorf("different error for second run: %v", err2)
}