github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/logflags/logflags_test.go (about) 1 package logflags 2 3 import ( 4 "bytes" 5 "io" 6 "reflect" 7 "testing" 8 9 "github.com/sirupsen/logrus" 10 ) 11 12 func TestMakeLogger_usingLoggerFactory(t *testing.T) { 13 if loggerFactory != nil { 14 t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) 15 } 16 defer func() { 17 loggerFactory = nil 18 }() 19 if logOut != nil { 20 t.Fatalf("expected logOut to be nil; but was <%v>", logOut) 21 } 22 logOut = &bufferWriter{} 23 defer func() { 24 logOut = nil 25 }() 26 27 expectedLogger := &logrusLogger{} 28 SetLoggerFactory(func(flag bool, fields Fields, out io.Writer) Logger { 29 if flag != true { 30 t.Fatalf("expected flag to be <%v>; but was <%v>", true, flag) 31 } 32 if len(fields) != 1 || fields["foo"] != "bar" { 33 t.Fatalf("expected fields to be {'foo':'bar'}; but was <%v>", fields) 34 } 35 if out != logOut { 36 t.Fatalf("expected out to be <%v>; but was <%v>", logOut, out) 37 } 38 return expectedLogger 39 }) 40 41 actual := makeLogger(true, Fields{"foo": "bar"}) 42 if actual != expectedLogger { 43 t.Fatalf("expected actual to <%v>; but was <%v>", expectedLogger, actual) 44 } 45 } 46 47 func TestMakeLogger_usingDefaultBehavior(t *testing.T) { 48 if loggerFactory != nil { 49 t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) 50 } 51 if logOut != nil { 52 t.Fatalf("expected logOut to be nil; but was <%v>", logOut) 53 } 54 logOut = &bufferWriter{} 55 defer func() { 56 logOut = nil 57 }() 58 59 actual := makeLogger(false, Fields{"foo": "bar"}) 60 61 actualEntry, expectedType := actual.(*logrusLogger) 62 if !expectedType { 63 t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry)) 64 } 65 if actualEntry.Entry.Logger.Level != logrus.ErrorLevel { 66 t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.ErrorLevel, actualEntry.Logger.Level) 67 } 68 if actualEntry.Entry.Logger.Out != logOut { 69 t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out) 70 } 71 if actualEntry.Entry.Logger.Formatter != textFormatterInstance { 72 t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter) 73 } 74 if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" { 75 t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data) 76 } 77 } 78 79 func TestMakeLogger_usingDefaultBehaviorAndFlagged(t *testing.T) { 80 if loggerFactory != nil { 81 t.Fatalf("expected loggerFactory to be nil; but was <%v>", loggerFactory) 82 } 83 if logOut != nil { 84 t.Fatalf("expected logOut to be nil; but was <%v>", logOut) 85 } 86 logOut = &bufferWriter{} 87 defer func() { 88 logOut = nil 89 }() 90 91 actual := makeLogger(true, Fields{"foo": "bar"}) 92 93 actualEntry, expectedType := actual.(*logrusLogger) 94 if !expectedType { 95 t.Fatalf("expected actual to be of type <%v>; but was <%v>", reflect.TypeOf((*logrus.Entry)(nil)), reflect.TypeOf(actualEntry)) 96 } 97 if actualEntry.Entry.Logger.Level != logrus.DebugLevel { 98 t.Fatalf("expected actualEntry.Entry.Logger.Level to be <%v>; but was <%v>", logrus.DebugLevel, actualEntry.Logger.Level) 99 } 100 if actualEntry.Entry.Logger.Out != logOut { 101 t.Fatalf("expected actualEntry.Entry.Logger.Out to be <%v>; but was <%v>", logOut, actualEntry.Logger.Out) 102 } 103 if actualEntry.Entry.Logger.Formatter != textFormatterInstance { 104 t.Fatalf("expected actualEntry.Entry.Logger.Formatter to be <%v>; but was <%v>", textFormatterInstance, actualEntry.Logger.Formatter) 105 } 106 if len(actualEntry.Entry.Data) != 1 || actualEntry.Entry.Data["foo"] != "bar" { 107 t.Fatalf("expected actualEntry.Entry.Data to be {'foo':'bar'}; but was <%v>", actualEntry.Data) 108 } 109 } 110 111 type bufferWriter struct { 112 bytes.Buffer 113 } 114 115 func (bw bufferWriter) Close() error { 116 return nil 117 }