github.com/mponton/terratest@v0.44.0/modules/logger/logger_test.go (about)

     1  package logger
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	tftesting "github.com/mponton/terratest/modules/testing"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDoLog(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	text := "test-do-log"
    17  	var buffer bytes.Buffer
    18  
    19  	DoLog(t, 1, &buffer, text)
    20  
    21  	assert.Regexp(t, fmt.Sprintf("^%s .+? [[:word:]]+.go:[0-9]+: %s$", t.Name(), text), strings.TrimSpace(buffer.String()))
    22  }
    23  
    24  type customLogger struct {
    25  	logs []string
    26  }
    27  
    28  func (c *customLogger) Logf(t tftesting.TestingT, format string, args ...interface{}) {
    29  	c.logs = append(c.logs, fmt.Sprintf(format, args...))
    30  }
    31  
    32  func TestCustomLogger(t *testing.T) {
    33  	Logf(t, "this should be logged with the default logger")
    34  
    35  	var l *Logger
    36  	l.Logf(t, "this should be logged with the default logger too")
    37  
    38  	l = New(nil)
    39  	l.Logf(t, "this should be logged with the default logger too!")
    40  
    41  	c := &customLogger{}
    42  	l = New(c)
    43  	l.Logf(t, "log output 1")
    44  	l.Logf(t, "log output 2")
    45  
    46  	t.Run("logger-subtest", func(t *testing.T) {
    47  		l.Logf(t, "subtest log")
    48  	})
    49  
    50  	assert.Len(t, c.logs, 3)
    51  	assert.Equal(t, "log output 1", c.logs[0])
    52  	assert.Equal(t, "log output 2", c.logs[1])
    53  	assert.Equal(t, "subtest log", c.logs[2])
    54  }