run_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package main
  17. import (
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "testing"
  22. "github.com/docker/docker/pkg/reexec"
  23. "github.com/ethereum/go-ethereum/internal/cmdtest"
  24. )
  25. func tmpdir(t *testing.T) string {
  26. dir, err := ioutil.TempDir("", "geth-test")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. return dir
  31. }
  32. type testgeth struct {
  33. *cmdtest.TestCmd
  34. // template variables for expect
  35. Datadir string
  36. Etherbase string
  37. }
  38. func init() {
  39. // Run the app if we've been exec'd as "geth-test" in runGeth.
  40. reexec.Register("geth-test", func() {
  41. if err := app.Run(os.Args); err != nil {
  42. fmt.Fprintln(os.Stderr, err)
  43. os.Exit(1)
  44. }
  45. os.Exit(0)
  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. // spawns geth with the given command line args. If the args don't set --datadir, the
  56. // child g gets a temporary data directory.
  57. func runGeth(t *testing.T, args ...string) *testgeth {
  58. tt := &testgeth{}
  59. tt.TestCmd = cmdtest.NewTestCmd(t, tt)
  60. for i, arg := range args {
  61. switch {
  62. case arg == "-datadir" || arg == "--datadir":
  63. if i < len(args)-1 {
  64. tt.Datadir = args[i+1]
  65. }
  66. case arg == "-etherbase" || arg == "--etherbase":
  67. if i < len(args)-1 {
  68. tt.Etherbase = args[i+1]
  69. }
  70. }
  71. }
  72. if tt.Datadir == "" {
  73. tt.Datadir = tmpdir(t)
  74. tt.Cleanup = func() { os.RemoveAll(tt.Datadir) }
  75. args = append([]string{"-datadir", tt.Datadir}, args...)
  76. // Remove the temporary datadir if something fails below.
  77. defer func() {
  78. if t.Failed() {
  79. tt.Cleanup()
  80. }
  81. }()
  82. }
  83. // Boot "geth". This actually runs the test binary but the TestMain
  84. // function will prevent any tests from running.
  85. tt.Run("geth-test", args...)
  86. return tt
  87. }