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.

testlogger.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "os"
  9. "runtime"
  10. "strings"
  11. "sync"
  12. "testing"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. var prefix string
  16. // TestLogger is a logger which will write to the testing log
  17. type TestLogger struct {
  18. log.WriterLogger
  19. }
  20. var writerCloser = &testLoggerWriterCloser{}
  21. type testLoggerWriterCloser struct {
  22. sync.RWMutex
  23. t []*testing.TB
  24. }
  25. func (w *testLoggerWriterCloser) setT(t *testing.TB) {
  26. w.Lock()
  27. w.t = append(w.t, t)
  28. w.Unlock()
  29. }
  30. func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
  31. w.RLock()
  32. var t *testing.TB
  33. if len(w.t) > 0 {
  34. t = w.t[len(w.t)-1]
  35. }
  36. w.RUnlock()
  37. if t != nil && *t != nil {
  38. if len(p) > 0 && p[len(p)-1] == '\n' {
  39. p = p[:len(p)-1]
  40. }
  41. defer func() {
  42. err := recover()
  43. if err == nil {
  44. return
  45. }
  46. var errString string
  47. errErr, ok := err.(error)
  48. if ok {
  49. errString = errErr.Error()
  50. } else {
  51. errString, ok = err.(string)
  52. }
  53. if !ok {
  54. panic(err)
  55. }
  56. if !strings.HasPrefix(errString, "Log in goroutine after ") {
  57. panic(err)
  58. }
  59. }()
  60. (*t).Log(string(p))
  61. return len(p), nil
  62. }
  63. return len(p), nil
  64. }
  65. func (w *testLoggerWriterCloser) Close() error {
  66. w.Lock()
  67. if len(w.t) > 0 {
  68. w.t = w.t[:len(w.t)-1]
  69. }
  70. w.Unlock()
  71. return nil
  72. }
  73. // PrintCurrentTest prints the current test to os.Stdout
  74. func PrintCurrentTest(t testing.TB, skip ...int) func() {
  75. actualSkip := 1
  76. if len(skip) > 0 {
  77. actualSkip = skip[0]
  78. }
  79. _, filename, line, _ := runtime.Caller(actualSkip)
  80. if log.CanColorStdout {
  81. fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", fmt.Formatter(log.NewColoredValue(t.Name())), strings.TrimPrefix(filename, prefix), line)
  82. } else {
  83. fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
  84. }
  85. writerCloser.setT(&t)
  86. return func() {
  87. _ = writerCloser.Close()
  88. }
  89. }
  90. // Printf takes a format and args and prints the string to os.Stdout
  91. func Printf(format string, args ...interface{}) {
  92. if log.CanColorStdout {
  93. for i := 0; i < len(args); i++ {
  94. args[i] = log.NewColoredValue(args[i])
  95. }
  96. }
  97. fmt.Fprintf(os.Stdout, "\t"+format, args...)
  98. }
  99. // NewTestLogger creates a TestLogger as a log.LoggerProvider
  100. func NewTestLogger() log.LoggerProvider {
  101. logger := &TestLogger{}
  102. logger.Colorize = log.CanColorStdout
  103. logger.Level = log.TRACE
  104. return logger
  105. }
  106. // Init inits connection writer with json config.
  107. // json config only need key "level".
  108. func (log *TestLogger) Init(config string) error {
  109. err := json.Unmarshal([]byte(config), log)
  110. if err != nil {
  111. return err
  112. }
  113. log.NewWriterLogger(writerCloser)
  114. return nil
  115. }
  116. // Flush when log should be flushed
  117. func (log *TestLogger) Flush() {
  118. }
  119. // GetName returns the default name for this implementation
  120. func (log *TestLogger) GetName() string {
  121. return "test"
  122. }
  123. func init() {
  124. log.Register("test", NewTestLogger)
  125. _, filename, _, _ := runtime.Caller(0)
  126. prefix = strings.TrimSuffix(filename, "integrations/testlogger.go")
  127. }