*: make tests use TempDir(), fix #1319

Simplify things, drop TempFile at the same time (refs. #1764)
This commit is contained in:
Roman Khimov 2021-08-25 22:17:37 +03:00
parent f4ba21a41a
commit 6d074a96e9
16 changed files with 93 additions and 308 deletions

View file

@ -28,6 +28,7 @@ import (
) )
func TestCalcHash(t *testing.T) { func TestCalcHash(t *testing.T) {
tmpDir := t.TempDir()
e := newExecutor(t, false) e := newExecutor(t, false)
nefPath := "./testdata/verify.nef" nefPath := "./testdata/verify.nef"
@ -58,13 +59,9 @@ func TestCalcHash(t *testing.T) {
"--in", "./testdata/verify.nef123", "--manifest", manifestPath)...) "--in", "./testdata/verify.nef123", "--manifest", manifestPath)...)
}) })
t.Run("invalid file", func(t *testing.T) { t.Run("invalid file", func(t *testing.T) {
p, err := ioutil.TempFile("", "neogo.calchash.verify.nef") p := path.Join(tmpDir, "neogo.calchash.verify.nef")
require.NoError(t, err) require.NoError(t, ioutil.WriteFile(p, src[:4], os.ModePerm))
t.Cleanup(func() { e.RunWithError(t, append(cmd, "--sender", sender.StringLE(), "--in", p, "--manifest", manifestPath)...)
os.Remove(p.Name())
})
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) cmd = append(cmd, "--in", nefPath, "--manifest", manifestPath)
@ -84,12 +81,7 @@ func TestCalcHash(t *testing.T) {
} }
func TestContractInitAndCompile(t *testing.T) { func TestContractInitAndCompile(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "neogo.inittest") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
e := newExecutor(t, false) e := newExecutor(t, false)
t.Run("no path is provided", func(t *testing.T) { t.Run("no path is provided", func(t *testing.T) {
@ -148,12 +140,7 @@ func TestDeployBigContract(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
nefName := path.Join(tmpDir, "deploy.nef") nefName := path.Join(tmpDir, "deploy.nef")
manifestName := path.Join(tmpDir, "deploy.manifest.json") manifestName := path.Join(tmpDir, "deploy.manifest.json")
@ -174,12 +161,7 @@ func TestContractDeployWithData(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
nefName := path.Join(tmpDir, "deploy.nef") nefName := path.Join(tmpDir, "deploy.nef")
manifestName := path.Join(tmpDir, "deploy.manifest.json") manifestName := path.Join(tmpDir, "deploy.manifest.json")
@ -232,12 +214,7 @@ func TestContractManifestGroups(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.test.deployfail")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
w, err := wallet.NewWalletFromFile(testWalletPath) w, err := wallet.NewWalletFromFile(testWalletPath)
require.NoError(t, err) require.NoError(t, err)
@ -273,11 +250,7 @@ func deployVerifyContract(t *testing.T, e *executor) util.Uint160 {
} }
func deployContract(t *testing.T, e *executor, inPath, configPath, wallet, address, pass string) util.Uint160 { func deployContract(t *testing.T, e *executor, inPath, configPath, wallet, address, pass string) util.Uint160 {
tmpDir, err := ioutil.TempDir(os.TempDir(), "neogo.test.deploycontract*") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
nefName := path.Join(tmpDir, "contract.nef") nefName := path.Join(tmpDir, "contract.nef")
manifestName := path.Join(tmpDir, "contract.manifest.json") manifestName := path.Join(tmpDir, "contract.manifest.json")
e.Run(t, "neo-go", "contract", "compile", e.Run(t, "neo-go", "contract", "compile",
@ -303,12 +276,7 @@ func TestComlileAndInvokeFunction(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.test.compileandinvoke")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
nefName := path.Join(tmpDir, "deploy.nef") nefName := path.Join(tmpDir, "deploy.nef")
manifestName := path.Join(tmpDir, "deploy.manifest.json") manifestName := path.Join(tmpDir, "deploy.manifest.json")
@ -556,12 +524,7 @@ func TestContractInspect(t *testing.T) {
// For proper nef generation. // For proper nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
const srcPath = "testdata/deploy/main.go" const srcPath = "testdata/deploy/main.go"
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir("", "neogo.test.contract.inspect")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
nefName := path.Join(tmpDir, "deploy.nef") nefName := path.Join(tmpDir, "deploy.nef")
manifestName := path.Join(tmpDir, "deploy.manifest.json") manifestName := path.Join(tmpDir, "deploy.manifest.json")
@ -588,6 +551,7 @@ func TestContractInspect(t *testing.T) {
} }
func TestCompileExamples(t *testing.T) { func TestCompileExamples(t *testing.T) {
tmpDir := t.TempDir()
const examplePath = "../examples" const examplePath = "../examples"
infos, err := ioutil.ReadDir(examplePath) infos, err := ioutil.ReadDir(examplePath)
require.NoError(t, err) require.NoError(t, err)
@ -608,21 +572,15 @@ func TestCompileExamples(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it") require.False(t, len(infos) == 0, "detected smart contract folder with no contract in it")
outF, err := ioutil.TempFile("", info.Name()+".nef") outF := path.Join(tmpDir, info.Name()+".nef")
require.NoError(t, err) manifestF := path.Join(tmpDir, info.Name()+".manifest.json")
manifestF, err := ioutil.TempFile("", info.Name()+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(outF.Name())
os.Remove(manifestF.Name())
})
cfgName := filterFilename(infos, ".yml") cfgName := filterFilename(infos, ".yml")
opts := []string{ opts := []string{
"neo-go", "contract", "compile", "neo-go", "contract", "compile",
"--in", path.Join(examplePath, info.Name()), "--in", path.Join(examplePath, info.Name()),
"--out", outF.Name(), "--out", outF,
"--manifest", manifestF.Name(), "--manifest", manifestF,
"--config", path.Join(examplePath, info.Name(), cfgName), "--config", path.Join(examplePath, info.Name(), cfgName),
} }
e.Run(t, opts...) e.Run(t, opts...)
@ -632,18 +590,12 @@ func TestCompileExamples(t *testing.T) {
t.Run("invalid events in manifest", func(t *testing.T) { t.Run("invalid events in manifest", func(t *testing.T) {
const dir = "./testdata/" const dir = "./testdata/"
for _, name := range []string{"invalid1", "invalid2", "invalid3"} { for _, name := range []string{"invalid1", "invalid2", "invalid3"} {
outF, err := ioutil.TempFile("", name+".nef") outF := path.Join(tmpDir, name+".nef")
require.NoError(t, err) manifestF := path.Join(tmpDir, name+".manifest.json")
manifestF, err := ioutil.TempFile("", name+".manifest.json")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(outF.Name())
os.Remove(manifestF.Name())
})
e.RunWithError(t, "neo-go", "contract", "compile", e.RunWithError(t, "neo-go", "contract", "compile",
"--in", path.Join(dir, name), "--in", path.Join(dir, name),
"--out", outF.Name(), "--out", outF,
"--manifest", manifestF.Name(), "--manifest", manifestF,
"--config", path.Join(dir, name, "invalid.yml"), "--config", path.Join(dir, name, "invalid.yml"),
) )
} }

View file

@ -12,11 +12,7 @@ import (
) )
func TestDBRestore(t *testing.T) { func TestDBRestore(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "neogo.restoretest") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
chainPath := path.Join(tmpDir, "neogotestchain") chainPath := path.Join(tmpDir, "neogotestchain")
cfg, err := config.LoadFile("../config/protocol.unit_testnet.yml") cfg, err := config.LoadFile("../config/protocol.unit_testnet.yml")

View file

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

View file

@ -6,7 +6,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"os"
"path" "path"
"strings" "strings"
"testing" "testing"
@ -31,10 +30,8 @@ const (
func TestNEP11Import(t *testing.T) { func TestNEP11Import(t *testing.T) {
e := newExecutor(t, true) e := newExecutor(t, true)
tmpDir, err := ioutil.TempDir("", "neogo.nep11import") tmpDir := t.TempDir()
require.NoError(t, err)
walletPath := path.Join(tmpDir, "walletForImport.json") walletPath := path.Join(tmpDir, "walletForImport.json")
t.Cleanup(func() { os.RemoveAll(tmpDir) })
// deploy NFT NeoNameService contract // deploy NFT NeoNameService contract
nnsContractHash := deployNNSContract(t, e) nnsContractHash := deployNNSContract(t, e)
@ -93,12 +90,7 @@ func TestNEP11Import(t *testing.T) {
func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) { func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
e := newExecutor(t, true) e := newExecutor(t, true)
tmpDir := t.TempDir()
tmpDir, err := ioutil.TempDir(os.TempDir(), "neogo.test.nftwallet*")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
// copy wallet to temp dir in order not to overwrite the original file // copy wallet to temp dir in order not to overwrite the original file
bytesRead, err := ioutil.ReadFile(nftOwnerWallet) bytesRead, err := ioutil.ReadFile(nftOwnerWallet)

View file

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

View file

@ -1,8 +1,6 @@
package server package server
import ( import (
"io/ioutil"
"os"
"path" "path"
"testing" "testing"
@ -10,12 +8,7 @@ import (
) )
func TestGetPath(t *testing.T) { func TestGetPath(t *testing.T) {
testPath, err := ioutil.TempDir("./", "") testPath := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(testPath)
require.NoError(t, err)
})
actual, err := getPath(testPath, 123) actual, err := getPath(testPath, 123)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, path.Join(testPath, "/BlockStorage_100000/dump-block-1000.json"), actual) require.Equal(t, path.Join(testPath, "/BlockStorage_100000/dump-block-1000.json"), actual)

View file

@ -2,8 +2,8 @@ package server
import ( import (
"flag" "flag"
"io/ioutil"
"os" "os"
"path"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
@ -16,6 +16,17 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// serverTestWD is the default working directory for server tests.
var serverTestWD string
func init() {
var err error
serverTestWD, err = os.Getwd()
if err != nil {
panic("can't get current working directory")
}
}
func TestGetConfigFromContext(t *testing.T) { func TestGetConfigFromContext(t *testing.T) {
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("config-path", "../../config", "") set.String("config-path", "../../config", "")
@ -27,17 +38,14 @@ func TestGetConfigFromContext(t *testing.T) {
} }
func TestHandleLoggingParams(t *testing.T) { func TestHandleLoggingParams(t *testing.T) {
testLog, err := ioutil.TempFile("./", "*.log") d := t.TempDir()
require.NoError(t, err) testLog := path.Join(d, "file.log")
t.Cleanup(func() {
require.NoError(t, os.Remove(testLog.Name()))
})
t.Run("default", func(t *testing.T) { t.Run("default", func(t *testing.T) {
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
ctx := cli.NewContext(cli.NewApp(), set, nil) ctx := cli.NewContext(cli.NewApp(), set, nil)
cfg := config.ApplicationConfiguration{ cfg := config.ApplicationConfiguration{
LogPath: testLog.Name(), LogPath: testLog,
} }
logger, err := handleLoggingParams(ctx, cfg) logger, err := handleLoggingParams(ctx, cfg)
require.NoError(t, err) require.NoError(t, err)
@ -50,7 +58,7 @@ func TestHandleLoggingParams(t *testing.T) {
set.Bool("debug", true, "") set.Bool("debug", true, "")
ctx := cli.NewContext(cli.NewApp(), set, nil) ctx := cli.NewContext(cli.NewApp(), set, nil)
cfg := config.ApplicationConfiguration{ cfg := config.ApplicationConfiguration{
LogPath: testLog.Name(), LogPath: testLog,
} }
logger, err := handleLoggingParams(ctx, cfg) logger, err := handleLoggingParams(ctx, cfg)
require.NoError(t, err) require.NoError(t, err)
@ -60,18 +68,13 @@ func TestHandleLoggingParams(t *testing.T) {
} }
func TestInitBCWithMetrics(t *testing.T) { func TestInitBCWithMetrics(t *testing.T) {
d, err := ioutil.TempDir("./", "") d := t.TempDir()
err := os.Chdir(d)
require.NoError(t, err) require.NoError(t, err)
err = os.Chdir(d) t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
require.NoError(t, err)
t.Cleanup(func() {
err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d)
})
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("config-path", "../../../config", "") set.String("config-path", path.Join(serverTestWD, "../../config"), "")
set.Bool("testnet", true, "") set.Bool("testnet", true, "")
set.Bool("debug", true, "") set.Bool("debug", true, "")
ctx := cli.NewContext(cli.NewApp(), set, nil) ctx := cli.NewContext(cli.NewApp(), set, nil)
@ -90,19 +93,15 @@ func TestInitBCWithMetrics(t *testing.T) {
} }
func TestDumpDB(t *testing.T) { func TestDumpDB(t *testing.T) {
testDump := "file.acc"
t.Run("too low chain", func(t *testing.T) { t.Run("too low chain", func(t *testing.T) {
d, err := ioutil.TempDir("./", "") d := t.TempDir()
err := os.Chdir(d)
require.NoError(t, err) require.NoError(t, err)
err = os.Chdir(d) t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
require.NoError(t, err)
t.Cleanup(func() {
err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d)
})
testDump := "file.acc"
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("config-path", "../../../config", "") set.String("config-path", path.Join(serverTestWD, "../../config"), "")
set.Bool("privnet", true, "") set.Bool("privnet", true, "")
set.Bool("debug", true, "") set.Bool("debug", true, "")
set.Int("start", 0, "") set.Int("start", 0, "")
@ -114,18 +113,12 @@ func TestDumpDB(t *testing.T) {
}) })
t.Run("positive", func(t *testing.T) { t.Run("positive", func(t *testing.T) {
d, err := ioutil.TempDir("./", "") d := t.TempDir()
err := os.Chdir(d)
require.NoError(t, err) require.NoError(t, err)
err = os.Chdir(d) t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
require.NoError(t, err)
t.Cleanup(func() {
err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d)
})
testDump := "file.acc"
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("config-path", "../../../config", "") set.String("config-path", path.Join(serverTestWD, "../../config"), "")
set.Bool("privnet", true, "") set.Bool("privnet", true, "")
set.Bool("debug", true, "") set.Bool("debug", true, "")
set.Int("start", 0, "") set.Int("start", 0, "")
@ -138,21 +131,16 @@ func TestDumpDB(t *testing.T) {
} }
func TestRestoreDB(t *testing.T) { func TestRestoreDB(t *testing.T) {
d, err := ioutil.TempDir("./", "") d := t.TempDir()
require.NoError(t, err)
testDump := "file1.acc" testDump := "file1.acc"
saveDump := "file2.acc" saveDump := "file2.acc"
err = os.Chdir(d) err := os.Chdir(d)
require.NoError(t, err) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d)
})
//dump first //dump first
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("config-path", "../../../config", "") set.String("config-path", path.Join(serverTestWD, "../../config"), "")
set.Bool("privnet", true, "") set.Bool("privnet", true, "")
set.Bool("debug", true, "") set.Bool("debug", true, "")
set.Int("start", 0, "") set.Int("start", 0, "")

View file

@ -16,15 +16,12 @@ import (
) )
func TestInitSmartContract(t *testing.T) { func TestInitSmartContract(t *testing.T) {
d, err := ioutil.TempDir("./", "") d := t.TempDir()
testWD, err := os.Getwd()
require.NoError(t, err) require.NoError(t, err)
err = os.Chdir(d) err = os.Chdir(d)
require.NoError(t, err) require.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() { require.NoError(t, os.Chdir(testWD)) })
err = os.Chdir("..")
require.NoError(t, err)
os.RemoveAll(d)
})
contractName := "testContract" contractName := "testContract"
set := flag.NewFlagSet("flagSet", flag.ExitOnError) set := flag.NewFlagSet("flagSet", flag.ExitOnError)

View file

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"os"
"path" "path"
"strings" "strings"
"testing" "testing"
@ -20,12 +19,7 @@ import (
) )
func TestWalletAccountRemove(t *testing.T) { func TestWalletAccountRemove(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
e := newExecutor(t, false) e := newExecutor(t, false)
walletPath := path.Join(tmpDir, "wallet.json") walletPath := path.Join(tmpDir, "wallet.json")
@ -52,12 +46,7 @@ func TestWalletAccountRemove(t *testing.T) {
} }
func TestWalletInit(t *testing.T) { func TestWalletInit(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "neogo.test.walletinit") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
e := newExecutor(t, false) e := newExecutor(t, false)
walletPath := path.Join(tmpDir, "wallet.json") walletPath := path.Join(tmpDir, "wallet.json")
@ -268,15 +257,11 @@ func TestClaimGas(t *testing.T) {
} }
func TestImportDeployed(t *testing.T) { func TestImportDeployed(t *testing.T) {
tmpDir := t.TempDir()
e := newExecutor(t, true) e := newExecutor(t, true)
h := deployVerifyContract(t, e) h := deployVerifyContract(t, e)
tmpDir, err := ioutil.TempDir("", "neogo.importdeployed")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpDir)
})
walletPath := path.Join(tmpDir, "wallet.json") walletPath := path.Join(tmpDir, "wallet.json")
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath) e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath)
priv, err := keys.NewPrivateKey() priv, err := keys.NewPrivateKey()
@ -402,12 +387,7 @@ func TestDumpKeys(t *testing.T) {
// Testcase is the wallet of privnet validator. // Testcase is the wallet of privnet validator.
func TestWalletConvert(t *testing.T) { func TestWalletConvert(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "neogo.test.convert") tmpDir := t.TempDir()
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
e := newExecutor(t, false) e := newExecutor(t, false)
outPath := path.Join(tmpDir, "wallet.json") outPath := path.Join(tmpDir, "wallet.json")

View file

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

View file

@ -1,33 +1,13 @@
package storage package storage
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type tempBadgerDB struct {
*BadgerDBStore
dir string
}
func (tbdb *tempBadgerDB) Close() error {
err := tbdb.BadgerDBStore.Close()
// Make test fail if failed to cleanup, even though technically it's
// not a BadgerDBStore problem.
osErr := os.RemoveAll(tbdb.dir)
if osErr != nil {
return osErr
}
return err
}
func newBadgerDBForTesting(t *testing.T) Store { func newBadgerDBForTesting(t *testing.T) Store {
bdbDir, err := ioutil.TempDir(os.TempDir(), "testbadgerdb") bdbDir := t.TempDir()
require.Nil(t, err, "failed to setup temporary directory")
dbConfig := DBConfiguration{ dbConfig := DBConfiguration{
Type: "badgerdb", Type: "badgerdb",
BadgerDBOptions: BadgerDBOptions{ BadgerDBOptions: BadgerDBOptions{
@ -36,9 +16,5 @@ func newBadgerDBForTesting(t *testing.T) Store {
} }
newBadgerStore, err := NewBadgerDBStore(dbConfig.BadgerDBOptions) newBadgerStore, err := NewBadgerDBStore(dbConfig.BadgerDBOptions)
require.Nil(t, err, "NewBadgerDBStore error") require.Nil(t, err, "NewBadgerDBStore error")
tbdb := &tempBadgerDB{ return newBadgerStore
BadgerDBStore: newBadgerStore,
dir: bdbDir,
}
return tbdb
} }

View file

@ -1,23 +1,16 @@
package storage package storage
import ( import (
"io/ioutil" "path"
"os"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func newBoltStoreForTesting(t *testing.T) Store { func newBoltStoreForTesting(t *testing.T) Store {
testFileName := "test_bolt_db" d := t.TempDir()
file, err := ioutil.TempFile("", testFileName) testFileName := path.Join(d, "test_bolt_db")
t.Cleanup(func() { boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName})
err := os.RemoveAll(file.Name())
require.NoError(t, err)
})
require.NoError(t, err)
require.NoError(t, file.Close())
boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: file.Name()})
require.NoError(t, err) require.NoError(t, err)
return boltDBStore return boltDBStore
} }

View file

@ -1,33 +1,13 @@
package storage package storage
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type tempLevelDB struct {
LevelDBStore
dir string
}
func (tldb *tempLevelDB) Close() error {
err := tldb.LevelDBStore.Close()
// Make test fail if failed to cleanup, even though technically it's
// not a LevelDBStore problem.
osErr := os.RemoveAll(tldb.dir)
if osErr != nil {
return osErr
}
return err
}
func newLevelDBForTesting(t *testing.T) Store { func newLevelDBForTesting(t *testing.T) Store {
ldbDir, err := ioutil.TempDir(os.TempDir(), "testleveldb") ldbDir := t.TempDir()
require.Nil(t, err, "failed to setup temporary directory")
dbConfig := DBConfiguration{ dbConfig := DBConfiguration{
Type: "leveldb", Type: "leveldb",
LevelDBOptions: LevelDBOptions{ LevelDBOptions: LevelDBOptions{
@ -36,6 +16,5 @@ func newLevelDBForTesting(t *testing.T) Store {
} }
newLevelStore, err := NewLevelDBStore(dbConfig.LevelDBOptions) newLevelStore, err := NewLevelDBStore(dbConfig.LevelDBOptions)
require.Nil(t, err, "NewLevelDBStore error") require.Nil(t, err, "NewLevelDBStore error")
tldb := &tempLevelDB{LevelDBStore: *newLevelStore, dir: ldbDir} return newLevelStore
return tldb
} }

View file

@ -1,7 +1,6 @@
package io package io
import ( import (
"io/ioutil"
"os" "os"
"path" "path"
"testing" "testing"
@ -10,34 +9,22 @@ import (
) )
func TestMakeDirForFile_HappyPath(t *testing.T) { func TestMakeDirForFile_HappyPath(t *testing.T) {
tempDir, err := ioutil.TempDir("", "test") tempDir := t.TempDir()
require.NoError(t, err) filePath := path.Join(tempDir, "testDir/testFile.test")
err := MakeDirForFile(filePath, "test")
filePath := tempDir + "/testDir/testFile.test"
err = MakeDirForFile(filePath, "test")
t.Cleanup(func() {
removeDir(t, tempDir)
})
require.NoError(t, err) require.NoError(t, err)
_, errChDir := os.Create(filePath) _, errChDir := os.Create(filePath)
require.NoError(t, errChDir) require.NoError(t, errChDir)
} }
func removeDir(t *testing.T, dirName string) {
err := os.RemoveAll(dirName)
require.NoError(t, err)
}
func TestMakeDirForFile_Negative(t *testing.T) { func TestMakeDirForFile_Negative(t *testing.T) {
file, err := ioutil.TempFile("", "test") tempDir := t.TempDir()
filePath := path.Join(tempDir, "testFile.test")
_, err := os.Create(filePath)
require.NoError(t, err) require.NoError(t, err)
filePath := file.Name() + "/error" filePath = path.Join(filePath, "error")
dir := path.Dir(filePath)
err = MakeDirForFile(filePath, "test") err = MakeDirForFile(filePath, "test")
t.Cleanup(func() {
removeDir(t, dir)
})
require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath) require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath)
} }

View file

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

View file

@ -2,8 +2,6 @@ package wallet
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"os"
"path" "path"
"testing" "testing"
@ -75,10 +73,8 @@ func TestPath(t *testing.T) {
} }
func TestSave(t *testing.T) { func TestSave(t *testing.T) {
file, err := ioutil.TempFile("", walletTemplate) wallet := checkWalletConstructor(t)
require.NoError(t, err)
wallet, err := NewWallet(file.Name())
require.NoError(t, err)
wallet.AddAccount(&Account{ wallet.AddAccount(&Account{
privateKey: nil, privateKey: nil,
publicKey: nil, publicKey: nil,
@ -91,9 +87,6 @@ func TestSave(t *testing.T) {
Default: false, Default: false,
}) })
t.Cleanup(func() {
removeWallet(t, file.Name())
})
errForSave := wallet.Save() errForSave := wallet.Save()
require.NoError(t, errForSave) require.NoError(t, errForSave)
@ -130,21 +123,13 @@ func TestJSONMarshallUnmarshal(t *testing.T) {
} }
func checkWalletConstructor(t *testing.T) *Wallet { func checkWalletConstructor(t *testing.T) *Wallet {
file, err := ioutil.TempFile("", walletTemplate) tmpDir := t.TempDir()
require.NoError(t, err) file := path.Join(tmpDir, walletTemplate)
wallet, err := NewWallet(file.Name()) wallet, err := NewWallet(file)
t.Cleanup(func() {
removeWallet(t, file.Name())
})
require.NoError(t, err) require.NoError(t, err)
return wallet return wallet
} }
func removeWallet(t *testing.T, walletPath string) {
err := os.RemoveAll(walletPath)
require.NoError(t, err)
}
func TestWallet_AddToken(t *testing.T) { func TestWallet_AddToken(t *testing.T) {
w := checkWalletConstructor(t) w := checkWalletConstructor(t)
tok := NewToken(util.Uint160{1, 2, 3}, "Rubl", "RUB", 2, manifest.NEP17StandardName) tok := NewToken(util.Uint160{1, 2, 3}, "Rubl", "RUB", 2, manifest.NEP17StandardName)