github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/run_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/docker/docker/pkg/reexec" 10 "github.com/neatlab/neatio/internal/cmdtest" 11 ) 12 13 func tmpdir(t *testing.T) string { 14 dir, err := ioutil.TempDir("", "neatio-test") 15 if err != nil { 16 t.Fatal(err) 17 } 18 return dir 19 } 20 21 type testneatchain struct { 22 *cmdtest.TestCmd 23 24 Datadir string 25 Etherbase string 26 } 27 28 func init() { 29 30 reexec.Register("neatio-test", func() { 31 if err := app.Run(os.Args); err != nil { 32 fmt.Fprintln(os.Stderr, err) 33 os.Exit(1) 34 } 35 os.Exit(0) 36 }) 37 } 38 39 func TestMain(m *testing.M) { 40 41 if reexec.Init() { 42 return 43 } 44 os.Exit(m.Run()) 45 } 46 47 func runneatchain(t *testing.T, args ...string) *testneatchain { 48 tt := &testneatchain{} 49 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 50 for i, arg := range args { 51 switch { 52 case arg == "-datadir" || arg == "--datadir": 53 if i < len(args)-1 { 54 tt.Datadir = args[i+1] 55 } 56 case arg == "-etherbase" || arg == "--etherbase": 57 if i < len(args)-1 { 58 tt.Etherbase = args[i+1] 59 } 60 } 61 } 62 if tt.Datadir == "" { 63 tt.Datadir = tmpdir(t) 64 tt.Cleanup = func() { os.RemoveAll(tt.Datadir) } 65 args = append([]string{"-datadir", tt.Datadir}, args...) 66 67 defer func() { 68 if t.Failed() { 69 tt.Cleanup() 70 } 71 }() 72 } 73 74 tt.Run("neatio-test", args...) 75 76 return tt 77 }