github.com/aquanetwork/aquachain@v1.7.8/cmd/aquachain/run_test.go (about)

     1  // Copyright 2016 The aquachain Authors
     2  // This file is part of aquachain.
     3  //
     4  // aquachain 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  // aquachain 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 aquachain. 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/docker/docker/pkg/reexec"
    26  	"gitlab.com/aquachain/aquachain/internal/cmdtest"
    27  )
    28  
    29  func tmpdir(t *testing.T) string {
    30  	dir, err := ioutil.TempDir("", "aquachain-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  	Aquabase string
    43  }
    44  
    45  func init() {
    46  	// Run the app if we've been exec'd as "aquachain-test" in runAquaChain.
    47  	reexec.Register("aquachain-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  func TestMain(m *testing.M) {
    57  	// check if we have been reexec'd
    58  	if reexec.Init() {
    59  		return
    60  	}
    61  	os.Exit(m.Run())
    62  }
    63  
    64  // spawns aquachain with the given command line args. If the args don't set --datadir, the
    65  // child g gets a temporary data directory.
    66  func runAquaChain(t *testing.T, args ...string) *testgeth {
    67  	tt := &testgeth{}
    68  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
    69  	for i, arg := range args {
    70  		switch {
    71  		case arg == "-datadir" || arg == "--datadir":
    72  			if i < len(args)-1 {
    73  				tt.Datadir = args[i+1]
    74  			}
    75  		case arg == "-aquabase" || arg == "--aquabase":
    76  			if i < len(args)-1 {
    77  				tt.Aquabase = args[i+1]
    78  			}
    79  		}
    80  	}
    81  	if tt.Datadir == "" {
    82  		tt.Datadir = tmpdir(t)
    83  		tt.Cleanup = func() { os.RemoveAll(tt.Datadir) }
    84  		args = append([]string{"-datadir", tt.Datadir}, args...)
    85  		// Remove the temporary datadir if something fails below.
    86  		defer func() {
    87  			if t.Failed() {
    88  				tt.Cleanup()
    89  			}
    90  		}()
    91  	}
    92  
    93  	// Boot "aquachain". This actually runs the test binary but the TestMain
    94  	// function will prevent any tests from running.
    95  	tt.Run("aquachain-test", args...)
    96  
    97  	return tt
    98  }