github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/bard/logger_test.go (about) 1 /* 2 * Copyright 2018-2023 the original author or 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 * https://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 bard_test 18 19 import ( 20 "bytes" 21 "fmt" 22 "os" 23 "testing" 24 25 . "github.com/onsi/gomega" 26 "github.com/sclevine/spec" 27 28 "github.com/BarDweller/libpak/bard" 29 ) 30 31 func testLogger(t *testing.T, context spec.G, it spec.S) { 32 var ( 33 Expect = NewWithT(t).Expect 34 35 b *bytes.Buffer 36 l bard.Logger 37 ) 38 39 it.Before(func() { 40 b = bytes.NewBuffer(nil) 41 }) 42 43 context("without BP_DEBUG", func() { 44 it.Before(func() { 45 l = bard.NewLogger(b) 46 }) 47 48 it("does not configure debug", func() { 49 Expect(l.IsDebugEnabled()).To(BeFalse()) 50 }) 51 }) 52 53 context("with BP_DEBUG", func() { 54 it.Before(func() { 55 //libcnb defines BP_DEBUG as enabled if it has _any_ value 56 //this does not include empty string as previously tested here. 57 Expect(os.Setenv("BP_DEBUG", "true")).To(Succeed()) 58 l = bard.NewLogger(b) 59 }) 60 61 it.After(func() { 62 Expect(os.Unsetenv("BP_DEBUG")).To(Succeed()) 63 }) 64 65 it("configures debug", func() { 66 Expect(l.IsDebugEnabled()).To(BeTrue()) 67 }) 68 }) 69 70 context("with BP_LOG_LEVEL set to DEBUG", func() { 71 it.Before(func() { 72 Expect(os.Setenv("BP_LOG_LEVEL", "DEBUG")).To(Succeed()) 73 l = bard.NewLogger(b) 74 }) 75 76 it.After(func() { 77 Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed()) 78 }) 79 80 it("configures debug", func() { 81 Expect(l.IsDebugEnabled()).To(BeTrue()) 82 }) 83 }) 84 85 context("with debug disabled", func() { 86 it.Before(func() { 87 Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed()) 88 l = bard.NewLoggerWithOptions(b) 89 }) 90 91 it("does not write debug log", func() { 92 l.Debug("test-message") 93 Expect(b.String()).To(Equal("")) 94 }) 95 96 it("does not write debug formatted log", func() { 97 l.Debugf("test-%s", "message") 98 Expect(b.String()).To(Equal("")) 99 }) 100 101 it("does not return debug writer", func() { 102 Expect(l.DebugWriter()).To(BeNil()) 103 }) 104 105 it("indicates that debug is not enabled", func() { 106 Expect(l.IsDebugEnabled()).To(BeFalse()) 107 }) 108 }) 109 110 context("with debug enabled", func() { 111 it.Before(func() { 112 Expect(os.Setenv("BP_LOG_LEVEL", "debug")).To(Succeed()) 113 l = bard.NewLogger(b) 114 }) 115 it.After(func() { 116 Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed()) 117 }) 118 119 it("writes body log", func() { 120 l.Body("test-message-1\ntest-message-2") 121 Expect(b.String()).To(Equal("\x1b[2m test-message-1\x1b[0m\n\x1b[2m test-message-2\x1b[0m\n")) 122 }) 123 124 it("writes body formatted log", func() { 125 l.Bodyf("test-%s\ntest-%s", "message-1", "message-2") 126 Expect(b.String()).To(Equal("\x1b[2m test-message-1\x1b[0m\n\x1b[2m test-message-2\x1b[0m\n")) 127 }) 128 129 it("returns body writer", func() { 130 Expect(l.BodyWriter()).NotTo(BeNil()) 131 }) 132 133 it("indicates that body is enabled", func() { 134 Expect(l.IsBodyEnabled()).To(BeTrue()) 135 }) 136 137 it("writes debug log", func() { 138 l.Debug("test-message") 139 Expect(b.String()).To(Equal("test-message\n")) 140 }) 141 142 it("writes debug formatted log", func() { 143 l.Debugf("test-%s", "message") 144 Expect(b.String()).To(Equal("test-message\n")) 145 }) 146 147 it("returns debug writer", func() { 148 Expect(l.DebugWriter()).NotTo(BeNil()) 149 }) 150 151 it("indicates that debug is enabled", func() { 152 Expect(l.IsDebugEnabled()).To(BeTrue()) 153 }) 154 155 it("writes header log", func() { 156 l.Header("test-message-1\ntest-message-2") 157 Expect(b.String()).To(Equal(" test-message-1\n test-message-2\n")) 158 }) 159 160 it("writes header formatted log", func() { 161 l.Headerf("test-%s\ntest-%s", "message-1", "message-2") 162 Expect(b.String()).To(Equal(" test-message-1\n test-message-2\n")) 163 }) 164 165 it("returns header writer", func() { 166 Expect(l.HeaderWriter()).NotTo(BeNil()) 167 }) 168 169 it("writes terminal error", func() { 170 l.TerminalError(bard.IdentifiableError{Name: "test-name", Description: "test-description", Err: fmt.Errorf("test-error")}) 171 Expect(b.String()).To(Equal("\x1b[31m\x1b[0m\n\x1b[31m\x1b[1mtest-name\x1b[0m\x1b[31m test-description\x1b[0m\n\x1b[31;1m test-error\x1b[0m\n")) 172 }) 173 174 it("returns terminal error writer", func() { 175 Expect(l.TerminalErrorWriter()).NotTo(BeNil()) 176 }) 177 178 it("indicates that terminal error is enabled", func() { 179 Expect(l.IsTerminalErrorEnabled()).To(BeTrue()) 180 }) 181 182 it("writes title log", func() { 183 l.Title("test-name", "test-version", "test-homepage") 184 Expect(b.String()).To(Equal("\x1b[34m\x1b[0m\n\x1b[34m\x1b[1mtest-name\x1b[0m\x1b[34m test-version\x1b[0m\n \x1b[34;2;3mtest-homepage\x1b[0m\n")) 185 }) 186 187 it("returns title writer", func() { 188 Expect(l.TitleWriter()).NotTo(BeNil()) 189 }) 190 191 it("indicates that title is enabled", func() { 192 Expect(l.IsTitleEnabled()).To(BeTrue()) 193 }) 194 }) 195 }