github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/cmd/geth/run_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "os" 23 "testing" 24 25 "github.com/ShyftNetwork/go-empyrean/internal/cmdtest" 26 "github.com/docker/docker/pkg/reexec" 27 ) 28 29 func tmpdir(t *testing.T) string { 30 dir, err := ioutil.TempDir("", "geth-test") 31 if err != nil { 32 t.Fatal(err) 33 } 34 return dir 35 } 36 37 type testgeth struct { 38 *cmdtest.TestCmd 39 40 // template variables for expect 41 Datadir string 42 Etherbase string 43 } 44 45 func init() { 46 // Run the app if we've been exec'd as "geth-test" in runGeth. 47 reexec.Register("geth-test", func() { 48 if err := app.Run(os.Args); err != nil { 49 fmt.Fprintln(os.Stderr, err) 50 os.Exit(1) 51 } 52 os.Exit(0) 53 }) 54 } 55 56 // spawns geth with the given command line args. If the args don't set --datadir, the 57 // child g gets a temporary data directory. 58 func runGeth(t *testing.T, args ...string) *testgeth { 59 tt := &testgeth{} 60 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 61 for i, arg := range args { 62 switch { 63 case arg == "-datadir" || arg == "--datadir": 64 if i < len(args)-1 { 65 tt.Datadir = args[i+1] 66 } 67 case arg == "-etherbase" || arg == "--etherbase": 68 if i < len(args)-1 { 69 tt.Etherbase = args[i+1] 70 } 71 } 72 } 73 if tt.Datadir == "" { 74 tt.Datadir = tmpdir(t) 75 tt.Cleanup = func() { os.RemoveAll(tt.Datadir) } 76 args = append([]string{"-datadir", tt.Datadir}, args...) 77 // Remove the temporary datadir if something fails below. 78 defer func() { 79 if t.Failed() { 80 tt.Cleanup() 81 } 82 }() 83 } 84 85 // Boot "geth". This actually runs the test binary but the TestMain 86 // function will prevent any tests from running. 87 tt.Run("geth-test", args...) 88 89 return tt 90 }