@@ -2,12 +2,29 @@ package logging
22
33import (
44 "bytes"
5+ "strings"
6+ "sync"
57 "testing"
68
79 "github.com/fatih/color"
810 log "github.com/sirupsen/logrus"
911)
1012
13+ var colorNoColorMu sync.Mutex
14+
15+ func setNoColorForTest (t * testing.T , noColor bool ) {
16+ t .Helper ()
17+
18+ colorNoColorMu .Lock ()
19+ prevNoColor := color .NoColor
20+ color .NoColor = noColor
21+
22+ t .Cleanup (func () {
23+ color .NoColor = prevNoColor
24+ colorNoColorMu .Unlock ()
25+ })
26+ }
27+
1128func TestLoggingToStd (t * testing.T ) {
1229 t .Run ("should write log to correct std descriptor" , func (t * testing.T ) {
1330 stdOutMsg := "Log in std out"
@@ -17,11 +34,7 @@ func TestLoggingToStd(t *testing.T) {
1734
1835 var errBuff bytes.Buffer
1936
20- prevNoColor := color .NoColor
21- color .NoColor = true
22- defer func () {
23- color .NoColor = prevNoColor
24- }()
37+ setNoColorForTest (t , true )
2538
2639 InitLogging (& stdBuff , & errBuff )
2740
@@ -39,23 +52,120 @@ func TestLoggingToStd(t *testing.T) {
3952 })
4053}
4154
42- func TestFormatterColorsDebugMessages (t * testing.T ) {
43- prevNoColor := color .NoColor
44- color .NoColor = false
45- defer func () {
46- color .NoColor = prevNoColor
47- }()
55+ func TestFormatterColorsVerboseMessages (t * testing.T ) {
56+ setNoColorForTest (t , false )
4857
49- line , err := (& Formatter {}).Format (& log.Entry {
50- Level : log .DebugLevel ,
51- Message : "debug message" ,
52- })
53- if err != nil {
54- t .Fatalf ("Format() error = %v" , err )
58+ tests := []struct {
59+ name string
60+ level log.Level
61+ }{
62+ {name : "debug" , level : log .DebugLevel },
63+ {name : "trace" , level : log .TraceLevel },
64+ }
65+
66+ for _ , tt := range tests {
67+ t .Run (tt .name , func (t * testing.T ) {
68+ line , err := (& Formatter {}).Format (& log.Entry {
69+ Level : tt .level ,
70+ Message : "debug message" ,
71+ })
72+ if err != nil {
73+ t .Fatalf ("Format() error = %v" , err )
74+ }
75+
76+ expected := color .BlueString ("lets:" ) + " " + color .BlueString ("debug message" ) + "\n "
77+ if string (line ) != expected {
78+ t .Fatalf ("unexpected debug line: %q" , string (line ))
79+ }
80+ })
5581 }
82+ }
83+
84+ func TestFormatterFormatsLevelsAndFields (t * testing.T ) {
85+ setNoColorForTest (t , true )
86+
87+ tests := []struct {
88+ name string
89+ entry * log.Entry
90+ fields []string
91+ }{
92+ {
93+ name : "info_no_data" ,
94+ entry : & log.Entry {
95+ Level : log .InfoLevel ,
96+ Message : "info message" ,
97+ Data : log.Fields {},
98+ },
99+ },
100+ {
101+ name : "warn_with_single_field" ,
102+ entry : & log.Entry {
103+ Level : log .WarnLevel ,
104+ Message : "warn message" ,
105+ Data : log.Fields {
106+ "foo" : "bar" ,
107+ },
108+ },
109+ fields : []string {"foo=bar" },
110+ },
111+ {
112+ name : "error_with_multiple_fields" ,
113+ entry : & log.Entry {
114+ Level : log .ErrorLevel ,
115+ Message : "error message" ,
116+ Data : log.Fields {
117+ "alpha" : "one" ,
118+ "beta" : "two" ,
119+ },
120+ },
121+ fields : []string {"alpha=one" , "beta=two" },
122+ },
123+ }
124+
125+ for _ , tt := range tests {
126+ t .Run (tt .name , func (t * testing.T ) {
127+ lineBytes , err := (& Formatter {}).Format (tt .entry )
128+ if err != nil {
129+ t .Fatalf ("Format() error = %v" , err )
130+ }
131+
132+ line := string (lineBytes )
133+ if ! strings .HasPrefix (line , "lets: " ) {
134+ t .Fatalf ("line does not start with expected prefix: %q" , line )
135+ }
136+
137+ if strings .Contains (line , "\x1b [" ) {
138+ t .Fatalf ("expected non-colorized output for non-debug levels, got: %q" , line )
139+ }
140+
141+ if len (tt .fields ) == 0 {
142+ expected := "lets: " + tt .entry .Message + "\n "
143+ if line != expected {
144+ t .Fatalf ("unexpected formatted line for empty Data.\n expected: %q\n got: %q" , expected , line )
145+ }
146+
147+ return
148+ }
149+
150+ msgIdx := strings .LastIndex (line , tt .entry .Message )
151+ if msgIdx == - 1 {
152+ t .Fatalf ("message %q not found in line: %q" , tt .entry .Message , line )
153+ }
154+
155+ if strings .Contains (line , " " ) {
156+ t .Fatalf ("unexpected double spaces in line: %q" , line )
157+ }
158+
159+ for _ , field := range tt .fields {
160+ fieldIdx := strings .Index (line , field )
161+ if fieldIdx == - 1 {
162+ t .Fatalf ("field %q not found in line: %q" , field , line )
163+ }
56164
57- expected := color .BlueString ("lets:" ) + " " + color .BlueString ("debug message" ) + "\n "
58- if string (line ) != expected {
59- t .Fatalf ("unexpected debug line: %q" , string (line ))
165+ if fieldIdx <= len ("lets:" ) || fieldIdx >= msgIdx {
166+ t .Fatalf ("field %q not positioned between prefix and message in line: %q" , field , line )
167+ }
168+ }
169+ })
60170 }
61171}
0 commit comments