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.

152 lines
3.2 KiB

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