main_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2015 Marc-Antoine Ruel. All rights reserved.
  2. // Use of this source code is governed under the Apache License, Version 2.0
  3. // that can be found in the LICENSE file.
  4. package internal
  5. import (
  6. "bytes"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "notabug.org/themusicgod1/panicparse/stack"
  11. )
  12. var data = []string{
  13. "panic: runtime error: index out of range",
  14. "",
  15. "goroutine 11 [running, 5 minutes, locked to thread]:",
  16. "github.com/luci/luci-go/client/archiver.(*archiver).PushFile(0xc208032410, 0xc20968a3c0, 0x5b, 0xc20988c280, 0x7d, 0x0, 0x0)",
  17. " /gopath/path/to/archiver.go:325 +0x2c4",
  18. "github.com/luci/luci-go/client/isolate.archive(0x7fbdab7a5218, 0xc208032410, 0xc20803b0b0, 0x22, 0xc208046370, 0xc20804666a, 0x17, 0x0, 0x0, 0x0, ...)",
  19. " /gopath/path/to/isolate.go:148 +0x12d2",
  20. "github.com/luci/luci-go/client/isolate.Archive(0x7fbdab7a5218, 0xc208032410, 0xc20803b0b0, 0x22, 0xc208046370, 0x0, 0x0)",
  21. " /gopath/path/to/isolate.go:102 +0xc9",
  22. "main.func·004(0x7fffc3b8f13a, 0x2c)",
  23. " /gopath/path/to/batch_archive.go:166 +0x7cd",
  24. "created by main.(*batchArchiveRun).main",
  25. " /gopath/path/to/batch_archive.go:167 +0x42c",
  26. "",
  27. "goroutine 1 [running]:",
  28. "gopkg.in/yaml%2ev2.handleErr(0xc208033b20)",
  29. " /gopath/src/gopkg.in/yaml.v2/yaml.go:153 +0xc6",
  30. "reflect.Value.assignTo(0x570860, 0xc20803f3e0, 0x15)",
  31. " c:/go/src/reflect/value.go:2125 +0x368",
  32. "main.main()",
  33. " /gopath/src/notabug.org/themusicgod1/pre-commit-go/main.go:428 +0x27",
  34. "",
  35. "goroutine 2 [running, 1 minutes]:",
  36. "gopkg.in/yaml%2ev2.handleErr(0xc208033b20)",
  37. " /gopath/src/gopkg.in/yaml.v2/yaml.go:153 +0xc6",
  38. "reflect.Value.assignTo(0x570860, 0xc20803f3e0, 0x15)",
  39. " c:/go/src/reflect/value.go:2125 +0x368",
  40. "main.main()",
  41. " /gopath/src/notabug.org/themusicgod1/pre-commit-go/main.go:428 +0x27",
  42. "",
  43. }
  44. func TestProcess(t *testing.T) {
  45. out := &bytes.Buffer{}
  46. if err := process(bytes.NewBufferString(strings.Join(data, "\n")), out, &defaultPalette, stack.AnyPointer, false, false, true, "", nil, nil); err != nil {
  47. t.Fatal(err)
  48. }
  49. expected := []string{
  50. "panic: runtime error: index out of range",
  51. "",
  52. "\x1b[1;35m1: running [5 minutes] [locked]\x1b[90m [Created by main.(*batchArchiveRun).main @ batch_archive.go:167]\x1b[39m\x1b[m",
  53. " \x1b[1;39marchiver \x1b[39m\x1b[marchiver.go:325 \x1b[1;31m(*archiver).PushFile\x1b[39m\x1b[m(#1, 0xc20968a3c0, 0x5b, 0xc20988c280, 0x7d, 0, 0)\x1b[39m\x1b[m",
  54. " \x1b[1;39misolate \x1b[39m\x1b[misolate.go:148 \x1b[31marchive\x1b[39m\x1b[m(#4, #1, #2, 0x22, #3, 0xc20804666a, 0x17, 0, 0, 0, ...)\x1b[39m\x1b[m",
  55. " \x1b[1;39misolate \x1b[39m\x1b[misolate.go:102 \x1b[1;31mArchive\x1b[39m\x1b[m(#4, #1, #2, 0x22, #3, 0, 0)\x1b[39m\x1b[m",
  56. " \x1b[1;39mmain \x1b[39m\x1b[mbatch_archive.go:166 \x1b[1;33mfunc·004\x1b[39m\x1b[m(0x7fffc3b8f13a, 0x2c)\x1b[39m\x1b[m",
  57. "2: running [0~1 minutes]\x1b[39m\x1b[m",
  58. " \x1b[1;39myaml.v2 \x1b[39m\x1b[myaml.go:153 \x1b[31mhandleErr\x1b[39m\x1b[m(#5)\x1b[39m\x1b[m",
  59. " \x1b[1;39mreflect \x1b[39m\x1b[mvalue.go:2125 \x1b[32mValue.assignTo\x1b[39m\x1b[m(0x570860, #6, 0x15)\x1b[39m\x1b[m",
  60. " \x1b[1;39mmain \x1b[39m\x1b[mmain.go:428 \x1b[1;33mmain\x1b[39m\x1b[m()\x1b[39m\x1b[m",
  61. "",
  62. }
  63. actual := strings.Split(out.String(), "\n")
  64. compareLines(t, expected, actual)
  65. }
  66. func TestProcessFullPath(t *testing.T) {
  67. out := &bytes.Buffer{}
  68. if err := process(bytes.NewBufferString(strings.Join(data, "\n")), out, &defaultPalette, stack.AnyValue, true, false, true, "", nil, nil); err != nil {
  69. t.Fatal(err)
  70. }
  71. expected := []string{
  72. "panic: runtime error: index out of range",
  73. "",
  74. "\x1b[1;35m1: running [5 minutes] [locked]\x1b[90m [Created by main.(*batchArchiveRun).main @ /gopath/path/to/batch_archive.go:167]\x1b[39m\x1b[m",
  75. " \x1b[1;39marchiver \x1b[39m\x1b[m/gopath/path/to/archiver.go:325 \x1b[1;31m(*archiver).PushFile\x1b[39m\x1b[m(#1, 0xc20968a3c0, 0x5b, 0xc20988c280, 0x7d, 0, 0)\x1b[39m\x1b[m",
  76. " \x1b[1;39misolate \x1b[39m\x1b[m/gopath/path/to/isolate.go:148 \x1b[31marchive\x1b[39m\x1b[m(#4, #1, #2, 0x22, #3, 0xc20804666a, 0x17, 0, 0, 0, ...)\x1b[39m\x1b[m",
  77. " \x1b[1;39misolate \x1b[39m\x1b[m/gopath/path/to/isolate.go:102 \x1b[1;31mArchive\x1b[39m\x1b[m(#4, #1, #2, 0x22, #3, 0, 0)\x1b[39m\x1b[m",
  78. " \x1b[1;39mmain \x1b[39m\x1b[m/gopath/path/to/batch_archive.go:166 \x1b[1;33mfunc·004\x1b[39m\x1b[m(0x7fffc3b8f13a, 0x2c)\x1b[39m\x1b[m",
  79. "2: running [0~1 minutes]\x1b[39m\x1b[m",
  80. " \x1b[1;39myaml.v2 \x1b[39m\x1b[m/gopath/src/gopkg.in/yaml.v2/yaml.go:153 \x1b[31mhandleErr\x1b[39m\x1b[m(#5)\x1b[39m\x1b[m",
  81. " \x1b[1;39mreflect \x1b[39m\x1b[mc:/go/src/reflect/value.go:2125 \x1b[32mValue.assignTo\x1b[39m\x1b[m(0x570860, #6, 0x15)\x1b[39m\x1b[m",
  82. " \x1b[1;39mmain \x1b[39m\x1b[m/gopath/src/notabug.org/themusicgod1/pre-commit-go/main.go:428 \x1b[1;33mmain\x1b[39m\x1b[m()\x1b[39m\x1b[m",
  83. "",
  84. }
  85. actual := strings.Split(out.String(), "\n")
  86. compareLines(t, expected, actual)
  87. }
  88. func TestProcessNoColor(t *testing.T) {
  89. out := &bytes.Buffer{}
  90. if err := process(bytes.NewBufferString(strings.Join(data, "\n")), out, &Palette{}, stack.AnyPointer, false, false, true, "", nil, nil); err != nil {
  91. t.Fatal(err)
  92. }
  93. expected := []string{
  94. "panic: runtime error: index out of range",
  95. "",
  96. "1: running [5 minutes] [locked] [Created by main.(*batchArchiveRun).main @ batch_archive.go:167]",
  97. " archiver archiver.go:325 (*archiver).PushFile(#1, 0xc20968a3c0, 0x5b, 0xc20988c280, 0x7d, 0, 0)",
  98. " isolate isolate.go:148 archive(#4, #1, #2, 0x22, #3, 0xc20804666a, 0x17, 0, 0, 0, ...)",
  99. " isolate isolate.go:102 Archive(#4, #1, #2, 0x22, #3, 0, 0)",
  100. " main batch_archive.go:166 func·004(0x7fffc3b8f13a, 0x2c)",
  101. "2: running [0~1 minutes]",
  102. " yaml.v2 yaml.go:153 handleErr(#5)",
  103. " reflect value.go:2125 Value.assignTo(0x570860, #6, 0x15)",
  104. " main main.go:428 main()",
  105. "",
  106. }
  107. actual := strings.Split(out.String(), "\n")
  108. compareLines(t, expected, actual)
  109. }
  110. func compareLines(t *testing.T, expected, actual []string) {
  111. for i := 0; i < len(actual) && i < len(expected); i++ {
  112. if expected[i] != actual[i] {
  113. t.Fatalf("Different lines #%d:\n- %q\n- %q", i, expected[i], actual[i])
  114. }
  115. }
  116. if len(expected) != len(actual) {
  117. t.Fatalf("different length %d != %d", len(expected), len(actual))
  118. }
  119. }
  120. func TestProcessMatch(t *testing.T) {
  121. out := &bytes.Buffer{}
  122. err := process(bytes.NewBufferString(strings.Join(data, "\n")), out, &Palette{}, stack.AnyPointer,
  123. false, false, true, "", nil, regexp.MustCompile(`batchArchiveRun`))
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. expected := []string{
  128. "panic: runtime error: index out of range",
  129. "",
  130. "1: running [5 minutes] [locked] [Created by main.(*batchArchiveRun).main @ batch_archive.go:167]",
  131. " archiver archiver.go:325 (*archiver).PushFile(#1, 0xc20968a3c0, 0x5b, 0xc20988c280, 0x7d, 0, 0)",
  132. " isolate isolate.go:148 archive(#4, #1, #2, 0x22, #3, 0xc20804666a, 0x17, 0, 0, 0, ...)",
  133. " isolate isolate.go:102 Archive(#4, #1, #2, 0x22, #3, 0, 0)",
  134. " main batch_archive.go:166 func·004(0x7fffc3b8f13a, 0x2c)",
  135. "",
  136. }
  137. actual := strings.Split(out.String(), "\n")
  138. compareLines(t, expected, actual)
  139. }
  140. func TestProcessFilter(t *testing.T) {
  141. out := &bytes.Buffer{}
  142. err := process(bytes.NewBufferString(strings.Join(data, "\n")), out, &Palette{}, stack.AnyPointer,
  143. false, false, true, "", regexp.MustCompile(`batchArchiveRun`), nil)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. expected := []string{
  148. "panic: runtime error: index out of range",
  149. "",
  150. "2: running [0~1 minutes]",
  151. " yaml.v2 yaml.go:153 handleErr(#5)",
  152. " reflect value.go:2125 Value.assignTo(0x570860, #6, 0x15)",
  153. " main main.go:428 main()",
  154. "",
  155. }
  156. actual := strings.Split(out.String(), "\n")
  157. compareLines(t, expected, actual)
  158. }