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

testlogger.go 3.3 kB

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