package testutils import ( "bytes" "encoding/json" "strconv" "strings" "testing" . "github.com/sirupsen/logrus" "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) for _, kv := range strings.Split(strings.TrimRight(buffer.String(), "\n"), " ") { 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) }