github.com/ethereum/go-ethereum@v1.16.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/ethereum/go-ethereum/internal/cmdtest" 25 "github.com/ethereum/go-ethereum/internal/reexec" 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 := t.TempDir() 61 return runWithKeystore(t, ddir, args...) 62 } 63 64 // runWithKeystore spawns clef with the given command line args and adds keystore arg. 65 // This method does _not_ create the keystore folder, but it _does_ add the arg 66 // to the args. 67 func runWithKeystore(t *testing.T, keystore string, args ...string) *testproc { 68 args = append([]string{"--keystore", keystore}, args...) 69 tt := &testproc{Datadir: keystore} 70 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 71 // Boot "clef". This actually runs the test binary but the TestMain 72 // function will prevent any tests from running. 73 tt.Run(registeredName, args...) 74 return tt 75 } 76 77 func (proc *testproc) input(text string) *testproc { 78 proc.TestCmd.InputLine(text) 79 return proc 80 } 81 82 /* 83 // waitForEndpoint waits for the rpc endpoint to appear, or 84 // aborts after 3 seconds. 85 func (proc *testproc) waitForEndpoint(t *testing.T) *testproc { 86 t.Helper() 87 timeout := 3 * time.Second 88 ipc := filepath.Join(proc.Datadir, "clef.ipc") 89 90 start := time.Now() 91 for time.Since(start) < timeout { 92 if _, err := os.Stat(ipc); !errors.Is(err, os.ErrNotExist) { 93 t.Logf("endpoint %v opened", ipc) 94 return proc 95 } 96 time.Sleep(200 * time.Millisecond) 97 } 98 t.Logf("stderr: \n%v", proc.StderrText()) 99 t.Logf("stdout: \n%v", proc.Output()) 100 t.Fatal("endpoint", ipc, "did not open within", timeout) 101 return proc 102 } 103 */