github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/geth/run_test.go (about)

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