mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
0078399788
This makes test code more clear and reduces the number of code lines by 500.
This also simplifies debugging tests. See https://itnext.io/f-tests-as-a-replacement-for-table-driven-tests-in-go-8814a8b19e9e
While at it, consistently use t.Fatal* instead of t.Error* across tests, since t.Error*
requires more boilerplate code, which can result in additional bugs inside tests.
While t.Error* allows writing logging errors for the same, this doesn't simplify fixing
broken tests most of the time.
This is a follow-up for a9525da8a4
54 lines
940 B
Go
54 lines
940 B
Go
package log
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
)
|
|
|
|
func TestOutput(t *testing.T) {
|
|
testOutput := &bytes.Buffer{}
|
|
logger.SetOutputForTests(testOutput)
|
|
defer logger.ResetOutputForTest()
|
|
|
|
log := &Logger{}
|
|
|
|
mustMatch := func(exp string) {
|
|
t.Helper()
|
|
|
|
if exp == "" {
|
|
if testOutput.String() != "" {
|
|
t.Fatalf("expected output to be empty; got %q", testOutput.String())
|
|
}
|
|
}
|
|
if !strings.Contains(testOutput.String(), exp) {
|
|
t.Fatalf("output %q should contain %q", testOutput.String(), exp)
|
|
}
|
|
fmt.Println(testOutput.String())
|
|
testOutput.Reset()
|
|
}
|
|
|
|
log.Warnf("foo")
|
|
mustMatch("foo")
|
|
|
|
log.Infof("info %d", 2)
|
|
mustMatch("info 2")
|
|
|
|
log.Errorf("error %s %d", "baz", 5)
|
|
mustMatch("error baz 5")
|
|
|
|
log.Suppress(true)
|
|
|
|
log.Warnf("foo")
|
|
mustMatch("")
|
|
|
|
log.Infof("info %d", 2)
|
|
mustMatch("")
|
|
|
|
log.Errorf("error %q %d", "baz", 5)
|
|
mustMatch("")
|
|
|
|
}
|