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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "testing"
  12. "code.gitea.io/gitea/modules/log"
  13. )
  14. var prefix string
  15. // TestLogger is a logger which will write to the testing log
  16. type TestLogger struct {
  17. log.WriterLogger
  18. }
  19. var writerCloser = &testLoggerWriterCloser{}
  20. type testLoggerWriterCloser struct {
  21. t testing.TB
  22. }
  23. func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
  24. if w.t != nil {
  25. if len(p) > 0 && p[len(p)-1] == '\n' {
  26. p = p[:len(p)-1]
  27. }
  28. w.t.Log(string(p))
  29. return len(p), nil
  30. }
  31. return len(p), nil
  32. }
  33. func (w *testLoggerWriterCloser) Close() error {
  34. return nil
  35. }
  36. // PrintCurrentTest prints the current test to os.Stdout
  37. func PrintCurrentTest(t testing.TB, skip ...int) {
  38. actualSkip := 1
  39. if len(skip) > 0 {
  40. actualSkip = skip[0]
  41. }
  42. _, filename, line, _ := runtime.Caller(actualSkip)
  43. if log.CanColorStdout {
  44. fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", log.NewColoredValue(t.Name()), strings.TrimPrefix(filename, prefix), line)
  45. } else {
  46. fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
  47. }
  48. writerCloser.t = t
  49. }
  50. // Printf takes a format and args and prints the string to os.Stdout
  51. func Printf(format string, args ...interface{}) {
  52. if log.CanColorStdout {
  53. for i := 0; i < len(args); i++ {
  54. args[i] = log.NewColoredValue(args[i])
  55. }
  56. }
  57. fmt.Fprintf(os.Stdout, "\t"+format, args...)
  58. }
  59. // NewTestLogger creates a TestLogger as a log.LoggerProvider
  60. func NewTestLogger() log.LoggerProvider {
  61. logger := &TestLogger{}
  62. logger.Colorize = log.CanColorStdout
  63. logger.Level = log.TRACE
  64. return logger
  65. }
  66. // Init inits connection writer with json config.
  67. // json config only need key "level".
  68. func (log *TestLogger) Init(config string) error {
  69. err := json.Unmarshal([]byte(config), log)
  70. if err != nil {
  71. return err
  72. }
  73. log.NewWriterLogger(writerCloser)
  74. return nil
  75. }
  76. // Flush when log should be flushed
  77. func (log *TestLogger) Flush() {
  78. }
  79. // GetName returns the default name for this implementation
  80. func (log *TestLogger) GetName() string {
  81. return "test"
  82. }
  83. func init() {
  84. log.Register("test", NewTestLogger)
  85. _, filename, _, _ := runtime.Caller(0)
  86. prefix = strings.TrimSuffix(filename, "integrations/testlogger.go")
  87. }