mirror of
https://gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud
synced 2024-11-15 06:16:24 +00:00
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
|
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(buffer.String(), " ") {
|
||
|
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)
|
||
|
}
|