github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/pkg/logger/logger_test.go (about)

     1  package logger
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestCtxWithForkedOutput(t *testing.T) {
    12  	out1 := &bytes.Buffer{}
    13  	out2 := &bytes.Buffer{}
    14  
    15  	ctx := WithLogger(context.Background(), NewLogger(DebugLvl, out1))
    16  	l := Get(CtxWithForkedOutput(ctx, out2))
    17  
    18  	l.Infof("test %s", "abcd")
    19  	l.Debugf("test2 %d", 5)
    20  
    21  	assert.Equal(t, "test abcd\ntest2 5\n", out1.String())
    22  	assert.Equal(t, "test abcd\ntest2 5\n", out2.String())
    23  }
    24  
    25  func TestWriteAcrossNestedLoggers(t *testing.T) {
    26  	out1 := bytes.NewBuffer(nil)
    27  	out2 := bytes.NewBuffer(nil)
    28  	prefixedOut1 := NewPrefixedLogger("|", NewLogger(DebugLvl, out1))
    29  	ctx := WithLogger(context.Background(), prefixedOut1)
    30  	l := Get(CtxWithForkedOutput(ctx, out2))
    31  	w := NewPrefixedLogger(">", l).Writer(InfoLvl)
    32  
    33  	_, _ = w.Write([]byte("a"))
    34  	_, _ = w.Write([]byte("b\nc"))
    35  	_, _ = w.Write([]byte("d\ne\n"))
    36  
    37  	assert.Equal(t, "|>ab\n|>cd\n|>e\n", out1.String())
    38  	assert.Equal(t, ">ab\n>cd\n>e\n", out2.String())
    39  }
    40  
    41  func TestWriteWithFormatPlaceholder(t *testing.T) {
    42  	out := bytes.NewBuffer(nil)
    43  	l := NewLogger(InfoLvl, out)
    44  	l.Write(InfoLvl, []byte("%s"))
    45  	assert.Equal(t, "%s", out.String())
    46  }