github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/logging/logger_simple_test.go (about)

     1  package logging_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/sclevine/spec"
     8  
     9  	"github.com/buildpacks/pack/pkg/logging"
    10  	h "github.com/buildpacks/pack/testhelpers"
    11  )
    12  
    13  const (
    14  	debugMatcher = `^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\.\d{6} DEBUG:  \w*\n$`
    15  	infoMatcher  = `^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\.\d{6} INFO:   \w*\n$`
    16  	warnMatcher  = `^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\.\d{6} WARN:   \w*\n$`
    17  	errorMatcher = `^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\.\d{6} ERROR:  \w*\n$`
    18  )
    19  
    20  func TestSimpleLogger(t *testing.T) {
    21  	spec.Run(t, "SimpleLogger", func(t *testing.T, when spec.G, it spec.S) {
    22  		var w bytes.Buffer
    23  		var logger logging.Logger
    24  
    25  		it.Before(func() {
    26  			logger = logging.NewSimpleLogger(&w)
    27  		})
    28  
    29  		it.After(func() {
    30  			w.Reset()
    31  		})
    32  
    33  		it("should print debug messages properly", func() {
    34  			logger.Debug("test")
    35  			h.AssertMatch(t, w.String(), debugMatcher)
    36  		})
    37  
    38  		it("should format debug messages properly", func() {
    39  			logger.Debugf("test%s", "foo")
    40  			h.AssertMatch(t, w.String(), debugMatcher)
    41  		})
    42  
    43  		it("should print info messages properly", func() {
    44  			logger.Info("test")
    45  			h.AssertMatch(t, w.String(), infoMatcher)
    46  		})
    47  
    48  		it("should format info messages properly", func() {
    49  			logger.Infof("test%s", "foo")
    50  			h.AssertMatch(t, w.String(), infoMatcher)
    51  		})
    52  
    53  		it("should print error messages properly", func() {
    54  			logger.Error("test")
    55  			h.AssertMatch(t, w.String(), errorMatcher)
    56  		})
    57  
    58  		it("should format error messages properly", func() {
    59  			logger.Errorf("test%s", "foo")
    60  			h.AssertMatch(t, w.String(), errorMatcher)
    61  		})
    62  
    63  		it("should print warn messages properly", func() {
    64  			logger.Warn("test")
    65  			h.AssertMatch(t, w.String(), warnMatcher)
    66  		})
    67  
    68  		it("should format warn messages properly", func() {
    69  			logger.Warnf("test%s", "foo")
    70  			h.AssertMatch(t, w.String(), warnMatcher)
    71  		})
    72  
    73  		it("shouldn't be verbose by default", func() {
    74  			h.AssertFalse(t, logger.IsVerbose())
    75  		})
    76  
    77  		it("should not format writer messages", func() {
    78  			_, _ = logger.Writer().Write([]byte("test"))
    79  			h.AssertEq(t, w.String(), "test")
    80  		})
    81  	})
    82  }