summaryrefslogtreecommitdiff
path: root/lib/textui/log_test.go
blob: 44958cb284c00d5c5e6b4912172802c84929ae25 (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
68
69
70
71
72
// 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(includePos bool, inner string) string {
	ret := `[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{4} ` + inner
	if includePos {
		ret += ` : \(from lib/textui/log_test\.go:[0-9]+\)`
	}
	ret += `\n`
	return ret
}

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(true, `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(false, `ERR : Error`)+
			logLineRegexp(false, `WRN : Warn`)+
			logLineRegexp(false, `INF : Info`)+
			logLineRegexp(false, `INF : Info`)+
			logLineRegexp(false, `WRN : Warn`)+
			logLineRegexp(false, `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(false, `INF : msg : foo=12,345`)+`$`,
		out.String())
}