You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alt_exit_test.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package logrus
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestRegister(t *testing.T) {
  14. current := len(handlers)
  15. var results []string
  16. h1 := func() { results = append(results, "first") }
  17. h2 := func() { results = append(results, "second") }
  18. RegisterExitHandler(h1)
  19. RegisterExitHandler(h2)
  20. if len(handlers) != current+2 {
  21. t.Fatalf("expected %d handlers, got %d", current+2, len(handlers))
  22. }
  23. runHandlers()
  24. if len(results) != 2 {
  25. t.Fatalf("expected 2 handlers to be run, ran %d", len(results))
  26. }
  27. if results[0] != "first" {
  28. t.Fatal("expected handler h1 to be run first, but it wasn't")
  29. }
  30. if results[1] != "second" {
  31. t.Fatal("expected handler h2 to be run second, but it wasn't")
  32. }
  33. }
  34. func TestDefer(t *testing.T) {
  35. current := len(handlers)
  36. var results []string
  37. h1 := func() { results = append(results, "first") }
  38. h2 := func() { results = append(results, "second") }
  39. DeferExitHandler(h1)
  40. DeferExitHandler(h2)
  41. if len(handlers) != current+2 {
  42. t.Fatalf("expected %d handlers, got %d", current+2, len(handlers))
  43. }
  44. runHandlers()
  45. if len(results) != 2 {
  46. t.Fatalf("expected 2 handlers to be run, ran %d", len(results))
  47. }
  48. if results[0] != "second" {
  49. t.Fatal("expected handler h2 to be run first, but it wasn't")
  50. }
  51. if results[1] != "first" {
  52. t.Fatal("expected handler h1 to be run second, but it wasn't")
  53. }
  54. }
  55. func TestHandler(t *testing.T) {
  56. testprog := testprogleader
  57. testprog = append(testprog, getPackage()...)
  58. testprog = append(testprog, testprogtrailer...)
  59. tempDir, err := ioutil.TempDir("", "test_handler")
  60. if err != nil {
  61. log.Fatalf("can't create temp dir. %q", err)
  62. }
  63. defer os.RemoveAll(tempDir)
  64. gofile := filepath.Join(tempDir, "gofile.go")
  65. if err := ioutil.WriteFile(gofile, testprog, 0666); err != nil {
  66. t.Fatalf("can't create go file. %q", err)
  67. }
  68. outfile := filepath.Join(tempDir, "outfile.out")
  69. arg := time.Now().UTC().String()
  70. err = exec.Command("go", "run", gofile, outfile, arg).Run()
  71. if err == nil {
  72. t.Fatalf("completed normally, should have failed")
  73. }
  74. data, err := ioutil.ReadFile(outfile)
  75. if err != nil {
  76. t.Fatalf("can't read output file %s. %q", outfile, err)
  77. }
  78. if string(data) != arg {
  79. t.Fatalf("bad data. Expected %q, got %q", data, arg)
  80. }
  81. }
  82. // getPackage returns the name of the current package, which makes running this
  83. // test in a fork simpler
  84. func getPackage() []byte {
  85. pc, _, _, _ := runtime.Caller(0)
  86. fullFuncName := runtime.FuncForPC(pc).Name()
  87. idx := strings.LastIndex(fullFuncName, ".")
  88. return []byte(fullFuncName[:idx]) // trim off function details
  89. }
  90. var testprogleader = []byte(`
  91. // Test program for atexit, gets output file and data as arguments and writes
  92. // data to output file in atexit handler.
  93. package main
  94. import (
  95. "`)
  96. var testprogtrailer = []byte(
  97. `"
  98. "flag"
  99. "fmt"
  100. "io/ioutil"
  101. )
  102. var outfile = ""
  103. var data = ""
  104. func handler() {
  105. ioutil.WriteFile(outfile, []byte(data), 0666)
  106. }
  107. func badHandler() {
  108. n := 0
  109. fmt.Println(1/n)
  110. }
  111. func main() {
  112. flag.Parse()
  113. outfile = flag.Arg(0)
  114. data = flag.Arg(1)
  115. logrus.RegisterExitHandler(handler)
  116. logrus.RegisterExitHandler(badHandler)
  117. logrus.Fatal("Bye bye")
  118. }
  119. `)