2018-10-28 16:07:57 +03:00
|
|
|
package testutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-21 15:59:06 +03:00
|
|
|
. "git.internal/re/logrus"
|
2018-10-28 16:07:57 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fields)) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var fields Fields
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &buffer
|
|
|
|
logger.Formatter = new(JSONFormatter)
|
|
|
|
|
|
|
|
log(logger)
|
|
|
|
|
|
|
|
err := json.Unmarshal(buffer.Bytes(), &fields)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
assertions(fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields map[string]string)) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
logger := New()
|
|
|
|
logger.Out = &buffer
|
|
|
|
logger.Formatter = &TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
log(logger)
|
|
|
|
|
|
|
|
fields := make(map[string]string)
|
2019-02-27 15:34:02 +03:00
|
|
|
for _, kv := range strings.Split(strings.TrimRight(buffer.String(), "\n"), " ") {
|
2018-10-28 16:07:57 +03:00
|
|
|
if !strings.Contains(kv, "=") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
kvArr := strings.Split(kv, "=")
|
|
|
|
key := strings.TrimSpace(kvArr[0])
|
|
|
|
val := kvArr[1]
|
|
|
|
if kvArr[1][0] == '"' {
|
|
|
|
var err error
|
|
|
|
val, err = strconv.Unquote(val)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
fields[key] = val
|
|
|
|
}
|
|
|
|
assertions(fields)
|
|
|
|
}
|