wrapper.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package ginkgowrapper wraps Ginkgo Fail and Skip functions to panic
  14. // with structured data instead of a constant string.
  15. package ginkgowrapper
  16. import (
  17. "bufio"
  18. "bytes"
  19. "regexp"
  20. "runtime"
  21. "runtime/debug"
  22. "strings"
  23. "github.com/onsi/ginkgo"
  24. )
  25. // FailurePanic is the value that will be panicked from Fail.
  26. type FailurePanic struct {
  27. Message string // The failure message passed to Fail
  28. Filename string // The filename that is the source of the failure
  29. Line int // The line number of the filename that is the source of the failure
  30. FullStackTrace string // A full stack trace starting at the source of the failure
  31. }
  32. // String makes FailurePanic look like the old Ginkgo panic when printed.
  33. func (FailurePanic) String() string { return ginkgo.GINKGO_PANIC }
  34. // Fail wraps ginkgo.Fail so that it panics with more useful
  35. // information about the failure. This function will panic with a
  36. // FailurePanic.
  37. func Fail(message string, callerSkip ...int) {
  38. skip := 1
  39. if len(callerSkip) > 0 {
  40. skip += callerSkip[0]
  41. }
  42. _, file, line, _ := runtime.Caller(skip)
  43. fp := FailurePanic{
  44. Message: message,
  45. Filename: file,
  46. Line: line,
  47. FullStackTrace: pruneStack(skip),
  48. }
  49. defer func() {
  50. e := recover()
  51. if e != nil {
  52. panic(fp)
  53. }
  54. }()
  55. ginkgo.Fail(message, skip)
  56. }
  57. // SkipPanic is the value that will be panicked from Skip.
  58. type SkipPanic struct {
  59. Message string // The failure message passed to Fail
  60. Filename string // The filename that is the source of the failure
  61. Line int // The line number of the filename that is the source of the failure
  62. FullStackTrace string // A full stack trace starting at the source of the failure
  63. }
  64. // String makes SkipPanic look like the old Ginkgo panic when printed.
  65. func (SkipPanic) String() string { return ginkgo.GINKGO_PANIC }
  66. // Skip wraps ginkgo.Skip so that it panics with more useful
  67. // information about why the test is being skipped. This function will
  68. // panic with a SkipPanic.
  69. func Skip(message string, callerSkip ...int) {
  70. skip := 1
  71. if len(callerSkip) > 0 {
  72. skip += callerSkip[0]
  73. }
  74. _, file, line, _ := runtime.Caller(skip)
  75. sp := SkipPanic{
  76. Message: message,
  77. Filename: file,
  78. Line: line,
  79. FullStackTrace: pruneStack(skip),
  80. }
  81. defer func() {
  82. e := recover()
  83. if e != nil {
  84. panic(sp)
  85. }
  86. }()
  87. ginkgo.Skip(message, skip)
  88. }
  89. // ginkgo adds a lot of test running infrastructure to the stack, so
  90. // we filter those out
  91. var stackSkipPattern = regexp.MustCompile(`onsi/ginkgo`)
  92. func pruneStack(skip int) string {
  93. skip += 2 // one for pruneStack and one for debug.Stack
  94. stack := debug.Stack()
  95. scanner := bufio.NewScanner(bytes.NewBuffer(stack))
  96. var prunedStack []string
  97. // skip the top of the stack
  98. for i := 0; i < 2*skip+1; i++ {
  99. scanner.Scan()
  100. }
  101. for scanner.Scan() {
  102. if stackSkipPattern.Match(scanner.Bytes()) {
  103. scanner.Scan() // these come in pairs
  104. } else {
  105. prunedStack = append(prunedStack, scanner.Text())
  106. scanner.Scan() // these come in pairs
  107. prunedStack = append(prunedStack, scanner.Text())
  108. }
  109. }
  110. return strings.Join(prunedStack, "\n")
  111. }