autogits/bots-common/log_test.go

43 lines
933 B
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package common
import (
2024-08-24 13:32:39 +02:00
"bytes"
2024-07-07 21:08:41 +02:00
"regexp"
"testing"
)
type TestWriter struct {
str string
}
func TestLogging(t *testing.T) {
t.Run("basic logging", func(t *testing.T) {
2024-08-24 13:32:39 +02:00
var strWriter, errWriter *bytes.Buffer
2024-07-07 21:08:41 +02:00
2024-08-24 13:32:39 +02:00
strWriter = bytes.NewBuffer(make([]byte, 0, 10000))
errWriter = bytes.NewBuffer(make([]byte, 0, 10000))
2024-07-07 21:08:41 +02:00
2024-08-24 13:32:39 +02:00
stdLogger, errLogger := CreateStdoutLogger(strWriter, errWriter)
errLogger.Printf("%d\n", 100)
stdLogger.Printf("OKA %d Done\n", 77)
stdLogger.Println("Another line")
2024-07-07 21:08:41 +02:00
2024-08-28 17:53:15 +02:00
const prefixMatch = `\[\d+\] `
2024-07-07 21:08:41 +02:00
2024-08-28 17:53:15 +02:00
errStr := errWriter.String()
if ok, err := regexp.MatchString("^"+prefixMatch+"100\n$", errStr); !ok {
2024-07-07 21:08:41 +02:00
if err != nil {
t.Logf("err: %v", err)
}
2024-08-28 17:53:15 +02:00
t.Fatal(errStr)
2024-07-07 21:08:41 +02:00
}
2024-08-24 13:32:39 +02:00
str := strWriter.String()
if ok, err := regexp.MatchString("^"+prefixMatch+"OKA 77 Done\n"+prefixMatch+"Another line\n$", str); !ok {
2024-07-07 21:08:41 +02:00
if err != nil {
t.Logf("err: %v", err)
}
2024-08-24 13:32:39 +02:00
t.Fatal(str)
2024-07-07 21:08:41 +02:00
}
})
}