-
Notifications
You must be signed in to change notification settings - Fork 43
/
option_test.go
46 lines (37 loc) · 1.27 KB
/
option_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cardinal_test
import (
"io"
"os"
"testing"
"github.com/goccy/go-json"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
)
func TestOptionFunctionSignatures(_ *testing.T) {
// This test is designed to keep API compatibility. If a compile error happens here it means a function signature to
// public facing functions was changed.
cardinal.WithReceiptHistorySize(1)
cardinal.WithCustomLogger(zerolog.New(os.Stdout))
cardinal.WithPort("")
cardinal.WithPrettyLog() //nolint:staticcheck // not applicable.
}
func TestWithPrettyLog_LogIsNotJSONFormatted(t *testing.T) {
cardinal.NewTestFixture(t, nil, cardinal.WithPrettyLog())
// Create a pipe to capture the output
r, w, _ := os.Pipe()
os.Stderr = w
// Test log. WithPrettyLog overwrites the global logger, so this shouild be pretty printed too.
log.Info().Msg("test")
_ = w.Close()
// Read the output and check that it is not JSON formatted (which is what a non-pretty logger would do)
output, err := io.ReadAll(r)
assert.NilError(t, err)
assert.Assert(t, !isValidJSON(output))
}
// isValidJSON tests if a string is valid JSON.
func isValidJSON(bz []byte) bool {
var js map[string]interface{}
return json.Unmarshal(bz, &js) == nil
}