github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/quickchain/run_test.go (about) 1 2 package main 3 4 import ( 5 "fmt" 6 "io/ioutil" 7 "os" 8 "testing" 9 10 "github.com/docker/docker/pkg/reexec" 11 "github.com/quickchainproject/quickchain/internal/cmdtest" 12 ) 13 14 func tmpdir(t *testing.T) string { 15 dir, err := ioutil.TempDir("", "geth-test") 16 if err != nil { 17 t.Fatal(err) 18 } 19 return dir 20 } 21 22 type testgeth struct { 23 *cmdtest.TestCmd 24 25 // template variables for expect 26 Datadir string 27 Etherbase string 28 } 29 30 func init() { 31 // Run the app if we've been exec'd as "geth-test" in runGeth. 32 reexec.Register("geth-test", func() { 33 if err := app.Run(os.Args); err != nil { 34 fmt.Fprintln(os.Stderr, err) 35 os.Exit(1) 36 } 37 os.Exit(0) 38 }) 39 } 40 41 func TestMain(m *testing.M) { 42 // check if we have been reexec'd 43 if reexec.Init() { 44 return 45 } 46 os.Exit(m.Run()) 47 } 48 49 // spawns geth with the given command line args. If the args don't set --datadir, the 50 // child g gets a temporary data directory. 51 func runGeth(t *testing.T, args ...string) *testgeth { 52 tt := &testgeth{} 53 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 54 for i, arg := range args { 55 switch { 56 case arg == "-datadir" || arg == "--datadir": 57 if i < len(args)-1 { 58 tt.Datadir = args[i+1] 59 } 60 case arg == "-etherbase" || arg == "--etherbase": 61 if i < len(args)-1 { 62 tt.Etherbase = args[i+1] 63 } 64 } 65 } 66 if tt.Datadir == "" { 67 tt.Datadir = tmpdir(t) 68 tt.Cleanup = func() { os.RemoveAll(tt.Datadir) } 69 args = append([]string{"-datadir", tt.Datadir}, args...) 70 // Remove the temporary datadir if something fails below. 71 defer func() { 72 if t.Failed() { 73 tt.Cleanup() 74 } 75 }() 76 } 77 78 // Boot "geth". This actually runs the test binary but the TestMain 79 // function will prevent any tests from running. 80 tt.Run("geth-test", args...) 81 82 return tt 83 }