*: create real temporary dirs and files in tests

Improve reliability.
This commit is contained in:
Roman Khimov 2021-07-20 12:32:02 +03:00
parent 3b19b34122
commit 0583f252ab
8 changed files with 77 additions and 59 deletions

View file

@ -58,12 +58,13 @@ func TestCalcHash(t *testing.T) {
"--in", "./testdata/verify.nef123", "--manifest", manifestPath)...)
})
t.Run("invalid file", func(t *testing.T) {
p := path.Join(os.TempDir(), "neogo.calchash.verify.nef")
p, err := ioutil.TempFile("", "neogo.calchash.verify.nef")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(p)
os.Remove(p.Name())
})
require.NoError(t, ioutil.WriteFile(p, src[:4], os.ModePerm))
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...)
require.NoError(t, ioutil.WriteFile(p.Name(), src[:4], os.ModePerm))
e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p.Name(), "--manifest", manifestPath)...)
})
cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath)
@ -83,8 +84,8 @@ func TestCalcHash(t *testing.T) {
}
func TestContractInitAndCompile(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.inittest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.inittest")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -148,8 +149,8 @@ func TestDeployBigContract(t *testing.T) {
// For proper nef generation.
config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.deployfail")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -174,8 +175,8 @@ func TestContractDeployWithData(t *testing.T) {
// For proper nef generation.
config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.deployfail")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -262,8 +263,8 @@ func TestComlileAndInvokeFunction(t *testing.T) {
// For proper nef generation.
config.Version = "0.90.0-test"
tmpDir := path.Join(os.TempDir(), "neogo.test.compileandinvoke")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.compileandinvoke")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -515,8 +516,8 @@ func TestContractInspect(t *testing.T) {
config.Version = "0.90.0-test"
const srcPath = "testdata/deploy/main.go"
tmpDir := path.Join(os.TempDir(), "neogo.test.contract.inspect")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.contract.inspect")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -553,8 +554,6 @@ func TestCompileExamples(t *testing.T) {
// For proper nef generation.
config.Version = "0.90.0-test"
tmpDir := os.TempDir()
e := newExecutor(t, false)
for _, info := range infos {
@ -568,19 +567,21 @@ func TestCompileExamples(t *testing.T) {
require.NoError(t, err)
require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
outPath := path.Join(tmpDir, info.Name()+".nef")
manifestPath := path.Join(tmpDir, info.Name()+".manifest.json")
outF, err := ioutil.TempFile("", info.Name()+".nef")
require.NoError(t, err)
manifestF, err := ioutil.TempFile("", info.Name()+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(outPath)
os.Remove(manifestPath)
os.Remove(outF.Name())
os.Remove(manifestF.Name())
})
cfgName := filterFilename(infos, ".yml")
opts := []string{
"neo-go", "contract", "compile",
"--in", path.Join(examplePath, info.Name()),
"--out", outPath,
"--manifest", manifestPath,
"--out", outF.Name(),
"--manifest", manifestF.Name(),
"--config", path.Join(examplePath, info.Name(), cfgName),
}
e.Run(t, opts...)
@ -590,16 +591,18 @@ func TestCompileExamples(t *testing.T) {
t.Run("invalid events in manifest", func(t *testing.T) {
const dir = "./testdata/"
for _, name := range []string{"invalid1", "invalid2", "invalid3"} {
outPath := path.Join(tmpDir, name+".nef")
manifestPath := path.Join(tmpDir, name+".manifest.json")
outF, err := ioutil.TempFile("", name+".nef")
require.NoError(t, err)
manifestF, err := ioutil.TempFile("", name+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(outPath)
os.Remove(manifestPath)
os.Remove(outF.Name())
os.Remove(manifestF.Name())
})
e.RunWithError(t, "neo-go", "contract", "compile",
"--in", path.Join(dir, name),
"--out", outPath,
"--manifest", manifestPath,
"--out", outF.Name(),
"--manifest", manifestF.Name(),
"--config", path.Join(dir, name, "invalid.yml"),
)
}

View file

@ -12,8 +12,8 @@ import (
)
func TestDBRestore(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.restoretest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.restoretest")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})

View file

@ -2,6 +2,7 @@ package main
import (
"encoding/hex"
"io/ioutil"
"math/big"
"os"
"path"
@ -27,12 +28,12 @@ func TestSignMultisigTx(t *testing.T) {
multisigAddr := address.Uint160ToString(multisigHash)
// Create 2 wallets participating in multisig.
tmpDir := os.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.wallettest")
require.NoError(t, err)
wallet1Path := path.Join(tmpDir, "multiWallet1.json")
wallet2Path := path.Join(tmpDir, "multiWallet2.json")
t.Cleanup(func() {
os.Remove(wallet1Path)
os.Remove(wallet2Path)
os.RemoveAll(tmpDir)
})
addAccount := func(w string, wif string) {

View file

@ -31,9 +31,10 @@ const (
func TestNEP11Import(t *testing.T) {
e := newExecutor(t, true)
tmpDir := os.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.nep11import")
require.NoError(t, err)
walletPath := path.Join(tmpDir, "walletForImport.json")
defer os.Remove(walletPath)
t.Cleanup(func() { os.RemoveAll(tmpDir) })
// deploy NFT NeoNameService contract
nnsContractHash := deployNNSContract(t, e)

View file

@ -2,6 +2,7 @@ package main
import (
"io"
"io/ioutil"
"math/big"
"os"
"path"
@ -260,9 +261,10 @@ func TestNEP17MultiTransfer(t *testing.T) {
func TestNEP17ImportToken(t *testing.T) {
e := newExecutor(t, true)
tmpDir := os.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.nep17import")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tmpDir) })
walletPath := path.Join(tmpDir, "walletForImport.json")
defer os.Remove(walletPath)
neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo)
require.NoError(t, err)

View file

@ -3,6 +3,7 @@ package main
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"math/big"
"os"
"path"
@ -19,8 +20,8 @@ import (
)
func TestWalletInit(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.test.walletinit")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -243,12 +244,13 @@ func TestImportDeployed(t *testing.T) {
e := newExecutor(t, true)
h := deployVerifyContract(t, e)
tmpDir := os.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.importdeployed")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpDir)
})
walletPath := path.Join(tmpDir, "wallet.json")
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath)
t.Cleanup(func() {
os.Remove(walletPath)
})
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
@ -373,8 +375,8 @@ func TestDumpKeys(t *testing.T) {
// Testcase is the wallet of privnet validator.
func TestWalletConvert(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.test.convert")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.test.convert")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})

View file

@ -2,6 +2,7 @@ package core
import (
"errors"
"io/ioutil"
"os"
"path"
"sort"
@ -78,9 +79,11 @@ func TestStateRoot(t *testing.T) {
updateIndex := bc.BlockHeight()
transferTokenFromMultisigAccount(t, bc, h, bc.contracts.GAS.Hash, 1_0000_0000)
tmpDir := path.Join(os.TempDir(), "neogo.initsnz")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
defer os.RemoveAll(tmpDir)
tmpDir, err := ioutil.TempDir("", "neogo.test.stateroot")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass")
cfg := createStateRootConfig(w.Path(), "pass")
srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil)
@ -148,9 +151,11 @@ func TestStateRootInitNonZeroHeight(t *testing.T) {
_, err := persistBlock(bc)
require.NoError(t, err)
tmpDir := path.Join(os.TempDir(), "neogo.initsnz")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
defer os.RemoveAll(tmpDir)
tmpDir, err := ioutil.TempDir("", "neogo.initsnz")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
w := createAndWriteWallet(t, accs[0], path.Join(tmpDir, "w"), "pass")
cfg := createStateRootConfig(w.Path(), "pass")
srv, err := stateroot.New(cfg, zaptest.NewLogger(t), bc, nil)
@ -190,9 +195,11 @@ func createStateRootConfig(walletPath, password string) config.StateRoot {
}
func TestStateRootFull(t *testing.T) {
tmpDir := path.Join(os.TempDir(), "neogo.stateroot4")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
defer os.RemoveAll(tmpDir)
tmpDir, err := ioutil.TempDir("", "neogo.stateroot4")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
bc := newTestChain(t)

View file

@ -162,8 +162,8 @@ func TestLoad(t *testing.T) {
return a * b
}
}`
tmpDir := path.Join(os.TempDir(), "vmcliloadtest")
require.NoError(t, os.Mkdir(tmpDir, os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.vmcliloadtest")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
@ -262,11 +262,13 @@ func TestRunWithDifferentArguments(t *testing.T) {
return arg
}`
filename := path.Join(os.TempDir(), "run_vmtestcontract.go")
require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm))
tmpDir, err := ioutil.TempDir("", "neogo.vmcliruntest")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(filename)
os.RemoveAll(tmpDir)
})
filename := path.Join(tmpDir, "run_vmtestcontract.go")
require.NoError(t, ioutil.WriteFile(filename, []byte(src), os.ModePerm))
e := newTestVMCLI(t)
e.runProg(t,