summaryrefslogtreecommitdiff
path: root/lib/textui/log_test.go
blob: bcd9c39a7d50761bf78a114cd4c5feab0a94c394 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package textui_test

import (
	"context"
	"strings"
	"testing"

	"github.com/datawire/dlib/dlog"
	"github.com/stretchr/testify/assert"

	"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)

func logLineRegexp(inner string) string {
	return `[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{4} ` + inner + ` \(from lib/textui/log_test\.go:[0-9]+\)\n`
}

func TestLogFormat(t *testing.T) {
	t.Parallel()
	var out strings.Builder
	ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelTrace))
	dlog.Debugf(ctx, "foo %d", 12345)
	assert.Regexp(t,
		`^`+logLineRegexp(`DBG : foo 12,345 :`)+`$`,
		out.String())
}

func TestLogLevel(t *testing.T) {
	t.Parallel()
	var out strings.Builder
	ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelInfo))
	dlog.Error(ctx, "Error")
	dlog.Warn(ctx, "Warn")
	dlog.Info(ctx, "Info")
	dlog.Debug(ctx, "Debug")
	dlog.Trace(ctx, "Trace")
	dlog.Trace(ctx, "Trace")
	dlog.Debug(ctx, "Debug")
	dlog.Info(ctx, "Info")
	dlog.Warn(ctx, "Warn")
	dlog.Error(ctx, "Error")
	assert.Regexp(t,
		`^`+
			logLineRegexp(`ERR : Error :`)+
			logLineRegexp(`WRN : Warn :`)+
			logLineRegexp(`INF : Info :`)+
			logLineRegexp(`INF : Info :`)+
			logLineRegexp(`WRN : Warn :`)+
			logLineRegexp(`ERR : Error :`)+
			`$`,
		out.String())
}

func TestLogField(t *testing.T) {
	t.Parallel()
	var out strings.Builder
	ctx := dlog.WithLogger(context.Background(), textui.NewLogger(&out, dlog.LogLevelInfo))
	ctx = dlog.WithField(ctx, "foo", 12345)
	dlog.Info(ctx, "msg")
	assert.Regexp(t,
		`^`+logLineRegexp(`INF : msg : foo=12,345`)+`$`,
		out.String())
}