github.com/theQRL/go-zond@v0.1.1/cmd/clef/run_test.go (about)

     1  // Copyright 2022 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  	"os"
    22  	"testing"
    23  
    24  	"github.com/docker/docker/pkg/reexec"
    25  	"github.com/theQRL/go-zond/internal/cmdtest"
    26  )
    27  
    28  const registeredName = "clef-test"
    29  
    30  type testproc struct {
    31  	*cmdtest.TestCmd
    32  
    33  	// template variables for expect
    34  	Datadir   string
    35  	Etherbase string
    36  }
    37  
    38  func init() {
    39  	reexec.Register(registeredName, func() {
    40  		if err := app.Run(os.Args); err != nil {
    41  			fmt.Fprintln(os.Stderr, err)
    42  			os.Exit(1)
    43  		}
    44  		os.Exit(0)
    45  	})
    46  }
    47  
    48  func TestMain(m *testing.M) {
    49  	// check if we have been reexec'd
    50  	if reexec.Init() {
    51  		return
    52  	}
    53  	os.Exit(m.Run())
    54  }
    55  
    56  // runClef spawns clef with the given command line args and adds keystore arg.
    57  // This method creates a temporary  keystore folder which will be removed after
    58  // the test exits.
    59  func runClef(t *testing.T, args ...string) *testproc {
    60  	ddir, err := os.MkdirTemp("", "cleftest-*")
    61  	if err != nil {
    62  		return nil
    63  	}
    64  	t.Cleanup(func() {
    65  		os.RemoveAll(ddir)
    66  	})
    67  	return runWithKeystore(t, ddir, args...)
    68  }
    69  
    70  // runWithKeystore spawns clef with the given command line args and adds keystore arg.
    71  // This method does _not_ create the keystore folder, but it _does_ add the arg
    72  // to the args.
    73  func runWithKeystore(t *testing.T, keystore string, args ...string) *testproc {
    74  	args = append([]string{"--keystore", keystore}, args...)
    75  	tt := &testproc{Datadir: keystore}
    76  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
    77  	// Boot "clef". This actually runs the test binary but the TestMain
    78  	// function will prevent any tests from running.
    79  	tt.Run(registeredName, args...)
    80  	return tt
    81  }
    82  
    83  func (proc *testproc) input(text string) *testproc {
    84  	proc.TestCmd.InputLine(text)
    85  	return proc
    86  }
    87  
    88  /*
    89  // waitForEndpoint waits for the rpc endpoint to appear, or
    90  // aborts after 3 seconds.
    91  func (proc *testproc) waitForEndpoint(t *testing.T) *testproc {
    92  	t.Helper()
    93  	timeout := 3 * time.Second
    94  	ipc := filepath.Join(proc.Datadir, "clef.ipc")
    95  
    96  	start := time.Now()
    97  	for time.Since(start) < timeout {
    98  		if _, err := os.Stat(ipc); !errors.Is(err, os.ErrNotExist) {
    99  			t.Logf("endpoint %v opened", ipc)
   100  			return proc
   101  		}
   102  		time.Sleep(200 * time.Millisecond)
   103  	}
   104  	t.Logf("stderr: \n%v", proc.StderrText())
   105  	t.Logf("stdout: \n%v", proc.Output())
   106  	t.Fatal("endpoint", ipc, "did not open within", timeout)
   107  	return proc
   108  }
   109  */