github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/core/log/log_test.go (about) 1 /* 2 * Copyright 2022 The Gremlins Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package log_test 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/spf13/viper" 24 25 "github.com/go-maxhub/gremlins/core/log" 26 ) 27 28 func TestUninitialised(t *testing.T) { 29 out := &bytes.Buffer{} 30 defer out.Reset() 31 log.Reset() 32 33 log.Infof("%s", "test") 34 log.Infoln("test") 35 log.Errorf("%s", "test") 36 log.Errorln("test") 37 38 if out.String() != "" { 39 t.Errorf("expected empty string") 40 } 41 } 42 43 func TestLogInfo(t *testing.T) { 44 out := &bytes.Buffer{} 45 log.Init(out, &bytes.Buffer{}) 46 47 t.Run("Infof", func(t *testing.T) { 48 defer out.Reset() 49 50 log.Infof("test %d", 1) 51 52 got := out.String() 53 54 want := "test 1" 55 if got != want { 56 t.Errorf("want %q, got %q", want, got) 57 } 58 }) 59 60 t.Run("Infoln", func(t *testing.T) { 61 defer out.Reset() 62 63 log.Infoln("test test") 64 65 got := out.String() 66 67 want := "test test\n" 68 if got != want { 69 t.Errorf("want %q, got %q", want, got) 70 } 71 }) 72 log.Reset() 73 } 74 75 func TestLogError(t *testing.T) { 76 out := &bytes.Buffer{} 77 eOut := &bytes.Buffer{} 78 log.Init(out, eOut) 79 defer log.Reset() 80 81 t.Run("Errorf", func(t *testing.T) { 82 defer out.Reset() 83 defer eOut.Reset() 84 85 log.Errorf("test %d", 1) 86 87 got := eOut.String() 88 89 want := "ERROR: test 1" 90 if got != want { 91 t.Errorf("want %q, got %q", want, got) 92 } 93 94 got = out.String() 95 if got != "" { 96 t.Errorf("expected out to be empty, got %s", got) 97 } 98 }) 99 100 t.Run("Errorln", func(t *testing.T) { 101 defer out.Reset() 102 defer eOut.Reset() 103 104 log.Errorln("test test") 105 106 got := eOut.String() 107 108 want := "ERROR: test test\n" 109 if got != want { 110 t.Errorf("want %q, got %q", want, got) 111 } 112 113 got = out.String() 114 if got != "" { 115 t.Errorf("expected out to be empty, got %s", got) 116 } 117 }) 118 } 119 120 func TestSilentMode(t *testing.T) { 121 viper.Set("silent", true) 122 defer viper.Reset() 123 124 sOut := &bytes.Buffer{} 125 defer sOut.Reset() 126 eOut := &bytes.Buffer{} 127 defer eOut.Reset() 128 log.Init(sOut, eOut) 129 defer log.Reset() 130 131 log.Infof("%s", "test") 132 log.Infoln("test") 133 log.Errorf("%s\n", "test") 134 log.Errorln("test") 135 136 if sOut.String() != "" { 137 t.Errorf("expected empty string") 138 } 139 if eOut.String() != "ERROR: test\nERROR: test\n" { 140 t.Log(eOut.String()) 141 t.Errorf("expected errors to be reported") 142 } 143 }