github.com/mondo192/jfrog-client-go@v1.0.0/utils/log/logger_test.go (about)

     1  package log
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/mondo192/jfrog-client-go/utils/io"
     6  	"github.com/stretchr/testify/assert"
     7  	"testing"
     8  )
     9  
    10  func TestLoggerRemoveEmojis(t *testing.T) {
    11  	testLoggerWithEmojis(t, false, expectedLogOutputWithoutEmojis)
    12  }
    13  
    14  func TestLoggerLeaveEmojis(t *testing.T) {
    15  	expected := expectedLogOutputWithEmojis
    16  	if io.IsWindows() {
    17  		// Should not print emojis on Windows
    18  		expected = expectedLogOutputWithoutEmojis
    19  	}
    20  	testLoggerWithEmojis(t, true, expected)
    21  }
    22  
    23  func testLoggerWithEmojis(t *testing.T, mockIsTerminalFlags bool, expected string) {
    24  	previousLog := Logger
    25  	// Restore previous logger when the function returns.
    26  	defer SetLogger(previousLog)
    27  
    28  	// Set new logger with output redirection to buffer.
    29  	buffer := &bytes.Buffer{}
    30  	SetLogger(NewLogger(DEBUG, buffer))
    31  	if mockIsTerminalFlags {
    32  		// Mock logger with isTerminal flags set to true
    33  		revertFlags := SetIsTerminalFlagsWithCallback(true)
    34  		// Revert to previous status
    35  		defer revertFlags()
    36  	}
    37  	Debug("111", 111, "", "111😀111👻🪶")
    38  	Info("222", 222, "", "222😀222👻🪶")
    39  	Warn("333", 333, "", "333😀333👻🪶")
    40  	Error("444", 444, "", "444😀444👻🪶")
    41  	Output("555", 555, "", "555😀555👻🪶")
    42  
    43  	// Compare output.
    44  	logOutput := buffer.Bytes()
    45  	compareResult := bytes.Compare(logOutput, []byte(expected))
    46  	assert.Equal(t, 0, compareResult)
    47  }
    48  
    49  const expectedLogOutputWithoutEmojis = `[Debug] 111 111  111111
    50  [Info] 222 222  222222
    51  [Warn] 333 333  333333
    52  [Error] 444 444  444444
    53  555 555  555555
    54  `
    55  
    56  const expectedLogOutputWithEmojis = `[Debug] 111 111  111😀111👻🪶
    57  [Info] 222 222  222😀222👻🪶
    58  [Warn] 333 333  333😀333👻🪶
    59  [Error] 444 444  444😀444👻🪶
    60  555 555  555😀555👻🪶
    61  `