github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/writer_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package log
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/stretchr/testify/assert"
     9  	"testing"
    10  )
    11  
    12  type mockLogger struct {
    13  	infoLines  []string
    14  	warnLines  []string
    15  	errorLines []string
    16  }
    17  
    18  func newMockLogger() *mockLogger {
    19  	var logger = mockLogger{}
    20  	return &logger
    21  }
    22  
    23  func (l *mockLogger) Info(args ...interface{}) {
    24  	l.infoLines = append(l.infoLines, fmt.Sprint(args...))
    25  }
    26  
    27  func (l *mockLogger) Warn(args ...interface{}) {
    28  	l.warnLines = append(l.warnLines, fmt.Sprint(args...))
    29  }
    30  
    31  func (l *mockLogger) Error(args ...interface{}) {
    32  	l.errorLines = append(l.errorLines, fmt.Sprint(args...))
    33  }
    34  
    35  func TestWriter(t *testing.T) {
    36  	t.Run("should buffer and append correctly", func(t *testing.T) {
    37  		mockLogger := newMockLogger()
    38  		writer := logrusWriter{logger: mockLogger}
    39  
    40  		written, err := writer.Write([]byte("line "))
    41  		assert.Equal(t, len("line "), written)
    42  		assert.NoError(t, err)
    43  
    44  		written, err = writer.Write([]byte("without "))
    45  		assert.Equal(t, len("without "), written)
    46  		assert.NoError(t, err)
    47  
    48  		written, err = writer.Write([]byte("linebreaks"))
    49  		assert.Equal(t, len("linebreaks"), written)
    50  		assert.NoError(t, err)
    51  
    52  		assert.Equal(t, 0, len(mockLogger.infoLines))
    53  		assert.Equal(t, 0, len(mockLogger.warnLines))
    54  		assert.Equal(t, 0, len(mockLogger.errorLines))
    55  
    56  		assert.Equal(t, "line without linebreaks", writer.buffer.String())
    57  	})
    58  
    59  	t.Run("should forward to info log", func(t *testing.T) {
    60  		mockLogger := newMockLogger()
    61  		writer := logrusWriter{logger: mockLogger}
    62  
    63  		line := "line with linebreak\n"
    64  
    65  		written, err := writer.Write([]byte(line))
    66  		assert.Equal(t, len(line), written)
    67  		assert.NoError(t, err)
    68  
    69  		if assert.Equal(t, 1, len(mockLogger.infoLines)) {
    70  			assert.Equal(t, "line with linebreak", mockLogger.infoLines[0])
    71  		}
    72  		assert.Equal(t, 0, len(mockLogger.warnLines))
    73  		assert.Equal(t, 0, len(mockLogger.errorLines))
    74  
    75  		assert.Equal(t, 0, writer.buffer.Len())
    76  	})
    77  	t.Run("should split at line breaks", func(t *testing.T) {
    78  		mockLogger := newMockLogger()
    79  		writer := logrusWriter{logger: mockLogger}
    80  
    81  		input := "line\nwith\nlinebreaks\n"
    82  
    83  		written, err := writer.Write([]byte(input))
    84  		assert.Equal(t, len(input), written)
    85  		assert.NoError(t, err)
    86  
    87  		if assert.Equal(t, 3, len(mockLogger.infoLines)) {
    88  			assert.Equal(t, "line", mockLogger.infoLines[0])
    89  			assert.Equal(t, "with", mockLogger.infoLines[1])
    90  			assert.Equal(t, "linebreaks", mockLogger.infoLines[2])
    91  		}
    92  		assert.Equal(t, 0, len(mockLogger.warnLines))
    93  		assert.Equal(t, 0, len(mockLogger.errorLines))
    94  
    95  		assert.Equal(t, 0, writer.buffer.Len())
    96  	})
    97  	t.Run("should output empty lines", func(t *testing.T) {
    98  		mockLogger := newMockLogger()
    99  		writer := logrusWriter{logger: mockLogger}
   100  
   101  		input := "\n\n"
   102  
   103  		written, err := writer.Write([]byte(input))
   104  		assert.Equal(t, len(input), written)
   105  		assert.NoError(t, err)
   106  
   107  		if assert.Equal(t, 2, len(mockLogger.infoLines)) {
   108  			assert.Equal(t, "", mockLogger.infoLines[0])
   109  			assert.Equal(t, "", mockLogger.infoLines[1])
   110  		}
   111  		assert.Equal(t, 0, len(mockLogger.warnLines))
   112  		assert.Equal(t, 0, len(mockLogger.errorLines))
   113  
   114  		assert.Equal(t, 0, writer.buffer.Len())
   115  	})
   116  	t.Run("should ignore empty input", func(t *testing.T) {
   117  		mockLogger := newMockLogger()
   118  		writer := logrusWriter{logger: mockLogger}
   119  
   120  		written, err := writer.Write([]byte(""))
   121  		assert.Equal(t, 0, written)
   122  		assert.NoError(t, err)
   123  
   124  		assert.Equal(t, 0, len(mockLogger.infoLines))
   125  		assert.Equal(t, 0, len(mockLogger.warnLines))
   126  		assert.Equal(t, 0, len(mockLogger.errorLines))
   127  
   128  		assert.Equal(t, 0, writer.buffer.Len())
   129  	})
   130  	t.Run("should align log level", func(t *testing.T) {
   131  		mockLogger := newMockLogger()
   132  		writer := logrusWriter{logger: mockLogger}
   133  
   134  		input := "I will count to three.\n1\nWARNING: 2\nERROR: 3\n"
   135  
   136  		written, err := writer.Write([]byte(input))
   137  		assert.Equal(t, len(input), written)
   138  		assert.NoError(t, err)
   139  
   140  		if assert.Equal(t, 2, len(mockLogger.infoLines)) {
   141  			assert.Equal(t, "I will count to three.", mockLogger.infoLines[0])
   142  			assert.Equal(t, "1", mockLogger.infoLines[1])
   143  		}
   144  		if assert.Equal(t, 1, len(mockLogger.warnLines)) {
   145  			assert.Equal(t, "WARNING: 2", mockLogger.warnLines[0])
   146  		}
   147  		if assert.Equal(t, 1, len(mockLogger.errorLines)) {
   148  			assert.Equal(t, "ERROR: 3", mockLogger.errorLines[0])
   149  		}
   150  
   151  		assert.Equal(t, 0, writer.buffer.Len())
   152  	})
   153  }