THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

137 lines
2.8KB

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