github.com/paketo-buildpacks/libpak@v1.70.0/bard/logger_test.go (about) 1 /* 2 * Copyright 2018-2020 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/buildpacks/libcnb" 26 . "github.com/onsi/gomega" 27 "github.com/sclevine/spec" 28 29 "github.com/paketo-buildpacks/libpak/bard" 30 ) 31 32 func testLogger(t *testing.T, context spec.G, it spec.S) { 33 var ( 34 Expect = NewWithT(t).Expect 35 36 b *bytes.Buffer 37 l bard.Logger 38 ) 39 40 it.Before(func() { 41 b = bytes.NewBuffer(nil) 42 }) 43 44 context("without BP_DEBUG", func() { 45 it.Before(func() { 46 l = bard.NewLogger(b) 47 }) 48 49 it("does not configure debug", func() { 50 Expect(l.IsDebugEnabled()).To(BeFalse()) 51 }) 52 }) 53 54 context("with BP_DEBUG", func() { 55 it.Before(func() { 56 Expect(os.Setenv("BP_DEBUG", "")).To(Succeed()) 57 l = bard.NewLogger(b) 58 }) 59 60 it.After(func() { 61 Expect(os.Unsetenv("BP_DEBUG")).To(Succeed()) 62 }) 63 64 it("configures debug", func() { 65 Expect(l.IsDebugEnabled()).To(BeTrue()) 66 }) 67 }) 68 69 context("with BP_LOG_LEVEL set to DEBUG", func() { 70 it.Before(func() { 71 Expect(os.Setenv("BP_LOG_LEVEL", "DEBUG")).To(Succeed()) 72 l = bard.NewLogger(b) 73 }) 74 75 it.After(func() { 76 Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed()) 77 }) 78 79 it("configures debug", func() { 80 Expect(l.IsDebugEnabled()).To(BeTrue()) 81 }) 82 }) 83 84 context("with debug disabled", func() { 85 it.Before(func() { 86 l = bard.NewLoggerWithOptions(b) 87 }) 88 89 it("does not write debug log", func() { 90 l.Debug("test-message") 91 Expect(b.String()).To(Equal("")) 92 }) 93 94 it("does not write debug formatted log", func() { 95 l.Debugf("test-%s", "message") 96 Expect(b.String()).To(Equal("")) 97 }) 98 99 it("does not return debug writer", func() { 100 Expect(l.DebugWriter()).To(BeNil()) 101 }) 102 103 it("indicates that debug is not enabled", func() { 104 Expect(l.IsDebugEnabled()).To(BeFalse()) 105 }) 106 107 it("writes info log", func() { 108 l.Info("test-message") 109 Expect(b.String()).To(Equal("test-message\n")) 110 }) 111 112 it("writes info formatted log", func() { 113 l.Infof("test-%s", "message") 114 Expect(b.String()).To(Equal("test-message\n")) 115 }) 116 117 it("returns info writer", func() { 118 Expect(l.InfoWriter()).NotTo(BeNil()) 119 }) 120 121 it("indicates that info is enabled", func() { 122 Expect(l.IsInfoEnabled()).To(BeTrue()) 123 }) 124 }) 125 126 context("with debug enabled", func() { 127 it.Before(func() { 128 l = bard.NewLoggerWithOptions(b, bard.WithDebug(b)) 129 }) 130 131 it("writes body log", func() { 132 l.Body("test-message-1\ntest-message-2") 133 Expect(b.String()).To(Equal("\x1b[2m test-message-1\x1b[0m\n\x1b[2m test-message-2\x1b[0m\n")) 134 }) 135 136 it("writes body formatted log", func() { 137 l.Bodyf("test-%s\ntest-%s", "message-1", "message-2") 138 Expect(b.String()).To(Equal("\x1b[2m test-message-1\x1b[0m\n\x1b[2m test-message-2\x1b[0m\n")) 139 }) 140 141 it("returns body writer", func() { 142 Expect(l.BodyWriter()).NotTo(BeNil()) 143 }) 144 145 it("indicates that body is enabled", func() { 146 Expect(l.IsBodyEnabled()).To(BeTrue()) 147 }) 148 149 it("writes debug log", func() { 150 l.Debug("test-message") 151 Expect(b.String()).To(Equal("test-message\n")) 152 }) 153 154 it("writes debug formatted log", func() { 155 l.Debugf("test-%s", "message") 156 Expect(b.String()).To(Equal("test-message\n")) 157 }) 158 159 it("returns debug writer", func() { 160 Expect(l.DebugWriter()).NotTo(BeNil()) 161 }) 162 163 it("indicates that debug is enabled", func() { 164 Expect(l.IsDebugEnabled()).To(BeTrue()) 165 }) 166 167 it("writes header log", func() { 168 l.Header("test-message-1\ntest-message-2") 169 Expect(b.String()).To(Equal(" test-message-1\n test-message-2\n")) 170 }) 171 172 it("writes header formatted log", func() { 173 l.Headerf("test-%s\ntest-%s", "message-1", "message-2") 174 Expect(b.String()).To(Equal(" test-message-1\n test-message-2\n")) 175 }) 176 177 it("returns header writer", func() { 178 Expect(l.HeaderWriter()).NotTo(BeNil()) 179 }) 180 181 it("indicates header body is enabled", func() { 182 Expect(l.IsHeaderEnabled()).To(BeTrue()) 183 }) 184 185 it("writes info log", func() { 186 l.Info("test-message") 187 Expect(b.String()).To(Equal("test-message\n")) 188 }) 189 190 it("writes info formatted log", func() { 191 l.Infof("test-%s", "message") 192 Expect(b.String()).To(Equal("test-message\n")) 193 }) 194 195 it("returns info writer", func() { 196 Expect(l.InfoWriter()).NotTo(BeNil()) 197 }) 198 199 it("indicates that info is enabled", func() { 200 Expect(l.IsInfoEnabled()).To(BeTrue()) 201 }) 202 203 it("writes terminal error", func() { 204 l.TerminalError(bard.IdentifiableError{Name: "test-name", Description: "test-description", Err: fmt.Errorf("test-error")}) 205 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")) 206 }) 207 208 it("returns terminal error writer", func() { 209 Expect(l.TerminalErrorWriter()).NotTo(BeNil()) 210 }) 211 212 it("indicates that terminal error is enabled", func() { 213 Expect(l.IsTerminalErrorEnabled()).To(BeTrue()) 214 }) 215 216 it("writes title log", func() { 217 l.Title(libcnb.Buildpack{ 218 Info: libcnb.BuildpackInfo{ 219 Name: "test-name", 220 Version: "test-version", 221 Homepage: "test-homepage", 222 }, 223 }) 224 225 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")) 226 }) 227 228 it("returns title writer", func() { 229 Expect(l.TitleWriter()).NotTo(BeNil()) 230 }) 231 232 it("indicates that title is enabled", func() { 233 Expect(l.IsTitleEnabled()).To(BeTrue()) 234 }) 235 }) 236 }