File: logflags_test.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (117 lines) | stat: -rw-r--r-- 3,766 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package logflags

import (
	"bytes"
	"io"
	"reflect"
	"testing"

	"github.com/sirupsen/logrus"
)

func TestMakeLogger_usingLoggerFactory(t *testing.T) {
	if loggerFactory != nil {
		t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
	}
	defer func() {
		loggerFactory = nil
	}()
	if logOut != nil {
		t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
	}
	logOut = &bufferWriter{}
	defer func() {
		logOut = nil
	}()

	expectedLogger := &logrusLogger{}
	SetLoggerFactory(func(flag bool, fields Fields, out io.Writer) Logger {
		if flag != true {
			t.Fatalf("expected flag to be <%v>; but was <%v>", true, flag)
		}
		if len(fields) != 1 || fields["foo"] != "bar" {
			t.Fatalf("expected fields to be {'foo':'bar'}; but was <%v>", fields)
		}
		if out != logOut {
			t.Fatalf("expected out to be <%v>; but was <%v>", logOut, out)
		}
		return expectedLogger
	})

	actual := makeLogger(true, Fields{"foo": "bar"})
	if actual != expectedLogger {
		t.Fatalf("expected actual to <%v>; but was <%v>", expectedLogger, actual)
	}
}

func TestMakeLogger_usingDefaultBehavior(t *testing.T) {
	if loggerFactory != nil {
		t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
	}
	if logOut != nil {
		t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
	}
	logOut = &bufferWriter{}
	defer func() {
		logOut = nil
	}()

	actual := makeLogger(false, Fields{"foo": "bar"})

	actualEntry, expectedType := actual.(*logrusLogger)
	if !expectedType {
		t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry))
	}
	if actualEntry.Entry.Logger.Level != logrus.ErrorLevel {
		t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.ErrorLevel, actualEntry.Logger.Level)
	}
	if actualEntry.Entry.Logger.Out != logOut {
		t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out)
	}
	if actualEntry.Entry.Logger.Formatter != textFormatterInstance {
		t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter)
	}
	if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" {
		t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data)
	}
}

func TestMakeLogger_usingDefaultBehaviorAndFlagged(t *testing.T) {
	if loggerFactory != nil {
		t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory)
	}
	if logOut != nil {
		t.Fatalf("expected logOut to be nil; but was <%v>", logOut)
	}
	logOut = &bufferWriter{}
	defer func() {
		logOut = nil
	}()

	actual := makeLogger(true, Fields{"foo": "bar"})

	actualEntry, expectedType := actual.(*logrusLogger)
	if !expectedType {
		t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry))
	}
	if actualEntry.Entry.Logger.Level != logrus.DebugLevel {
		t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.DebugLevel, actualEntry.Logger.Level)
	}
	if actualEntry.Entry.Logger.Out != logOut {
		t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out)
	}
	if actualEntry.Entry.Logger.Formatter != textFormatterInstance {
		t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter)
	}
	if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" {
		t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data)
	}
}

type bufferWriter struct {
	bytes.Buffer
}

func (bw bufferWriter) Close() error {
	return nil
}