github.com/blend/go-sdk@v1.20220411.3/logger/option_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package logger 9 10 import ( 11 "bytes" 12 "testing" 13 "time" 14 15 "github.com/blend/go-sdk/assert" 16 "github.com/blend/go-sdk/env" 17 ) 18 19 func TestOptConfig(t *testing.T) { 20 assert := assert.New(t) 21 22 log := None() 23 assert.Nil(OptConfig(Config{ 24 Flags: []string{"foo", "bar"}, 25 Writable: []string{"foo"}, 26 Format: FormatJSON, 27 })(log)) 28 29 assert.Any(log.Flags.Flags(), func(v interface{}) bool { return v.(string) == "foo" }) 30 assert.Any(log.Flags.Flags(), func(v interface{}) bool { return v.(string) == "bar" }) 31 assert.Any(log.Writable.Flags(), func(v interface{}) bool { return v.(string) == "foo" }) 32 assert.None(log.Writable.Flags(), func(v interface{}) bool { return v.(string) == "bar" }) 33 } 34 35 func TestOptConfigFromEnv(t *testing.T) { 36 assert := assert.New(t) 37 38 defer env.Restore() 39 env.Env().Set("LOG_FLAGS", "foo,bar") 40 env.Env().Set("LOG_WRITABLE", "foo") 41 env.Env().Set("LOG_HIDE_TIMESTAMP", "true") 42 env.Env().Set("LOG_HIDE_FIELDS", "true") 43 env.Env().Set("LOG_TIME_FORMAT", time.Kitchen) 44 env.Env().Set("NO_COLOR", "true") 45 46 log := None() 47 assert.Nil(OptConfigFromEnv()(log)) 48 49 assert.Any(log.Flags.Flags(), func(v interface{}) bool { return v.(string) == "foo" }) 50 assert.Any(log.Flags.Flags(), func(v interface{}) bool { return v.(string) == "bar" }) 51 assert.Any(log.Writable.Flags(), func(v interface{}) bool { return v.(string) == "foo" }) 52 assert.None(log.Writable.Flags(), func(v interface{}) bool { return v.(string) == "bar" }) 53 assert.True(log.Formatter.(*TextOutputFormatter).HideTimestamp) 54 assert.True(log.Formatter.(*TextOutputFormatter).HideFields) 55 assert.True(log.Formatter.(*TextOutputFormatter).NoColor) 56 assert.Equal(time.Kitchen, log.Formatter.(*TextOutputFormatter).TimeFormat) 57 } 58 59 func TestOptOutput(t *testing.T) { 60 assert := assert.New(t) 61 62 log := None() 63 64 buf := new(bytes.Buffer) 65 assert.Nil(OptOutput(buf)(log)) 66 67 typed, ok := log.Output.(*InterlockedWriter) 68 assert.True(ok) 69 assert.NotNil(typed.Output) 70 } 71 72 func TestOptions(t *testing.T) { 73 assert := assert.New(t) 74 75 log := None() 76 77 assert.Nil(log.Output) 78 assert.Nil(OptOutput(new(bytes.Buffer))(log)) 79 assert.NotNil(log.Output) 80 81 assert.Nil(log.Formatter) 82 assert.Nil(OptText(OptTextNoColor())(log)) 83 assert.NotNil(log.Formatter) 84 assert.True(log.Formatter.(*TextOutputFormatter).NoColor) 85 86 assert.Nil(OptJSON(OptJSONPretty())(log)) 87 assert.NotNil(log.Formatter) 88 assert.True(log.Formatter.(*JSONOutputFormatter).Pretty) 89 90 assert.True(log.Flags.None()) 91 assert.Nil(OptFlags(NewFlags("test1", "test2"))(log)) 92 assert.False(log.Flags.None()) 93 assert.True(log.Flags.IsEnabled("test1")) 94 assert.True(log.Flags.IsEnabled("test2")) 95 96 assert.False(log.Flags.IsEnabled("foo")) 97 assert.Nil(OptEnabled("foo")(log)) 98 assert.True(log.Flags.IsEnabled("foo")) 99 assert.Nil(OptDisabled("foo")(log)) 100 assert.False(log.Flags.IsEnabled("foo")) 101 102 assert.False(log.Flags.All()) 103 assert.Nil(OptAll()(log)) 104 assert.True(log.Flags.All()) 105 }