2014-12-15 22:20:33 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-07-13 14:46:35 +03:00
|
|
|
"fmt"
|
2018-08-09 16:01:49 +03:00
|
|
|
"os"
|
2018-11-16 17:19:37 +03:00
|
|
|
"runtime"
|
2018-09-25 14:45:23 +03:00
|
|
|
"sort"
|
2017-02-14 13:53:03 +03:00
|
|
|
"strings"
|
2014-12-15 22:20:33 +03:00
|
|
|
"testing"
|
2015-03-27 02:04:45 +03:00
|
|
|
"time"
|
2017-11-22 06:43:47 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-25 14:45:23 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-12-15 22:20:33 +03:00
|
|
|
)
|
|
|
|
|
2017-07-21 17:06:23 +03:00
|
|
|
func TestFormatting(t *testing.T) {
|
|
|
|
tf := &TextFormatter{DisableColors: true}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected string
|
|
|
|
}{
|
2017-07-21 17:14:28 +03:00
|
|
|
{`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"},
|
2017-07-21 17:06:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
|
|
|
|
if string(b) != tc.expected {
|
|
|
|
t.Errorf("formatting expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 22:20:33 +03:00
|
|
|
func TestQuoting(t *testing.T) {
|
2014-12-18 09:59:41 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
2014-12-15 22:20:33 +03:00
|
|
|
|
|
|
|
checkQuoting := func(q bool, value interface{}) {
|
|
|
|
b, _ := tf.Format(WithField("test", value))
|
2014-12-18 09:59:41 +03:00
|
|
|
idx := bytes.Index(b, ([]byte)("test="))
|
2017-07-13 14:46:35 +03:00
|
|
|
cont := bytes.Contains(b[idx+5:], []byte("\""))
|
2014-12-15 22:20:33 +03:00
|
|
|
if cont != q {
|
|
|
|
if q {
|
|
|
|
t.Errorf("quoting expected for: %#v", value)
|
|
|
|
} else {
|
|
|
|
t.Errorf("quoting not expected for: %#v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:53:03 +03:00
|
|
|
checkQuoting(false, "")
|
2014-12-15 22:20:33 +03:00
|
|
|
checkQuoting(false, "abcd")
|
|
|
|
checkQuoting(false, "v1.0")
|
2015-03-04 17:04:50 +03:00
|
|
|
checkQuoting(false, "1234567890")
|
2017-06-18 16:48:52 +03:00
|
|
|
checkQuoting(false, "/foobar")
|
|
|
|
checkQuoting(false, "foo_bar")
|
|
|
|
checkQuoting(false, "foo@bar")
|
|
|
|
checkQuoting(false, "foobar^")
|
|
|
|
checkQuoting(false, "+/-_^@f.oobar")
|
|
|
|
checkQuoting(true, "foobar$")
|
|
|
|
checkQuoting(true, "&foobar")
|
2014-12-15 22:20:33 +03:00
|
|
|
checkQuoting(true, "x y")
|
|
|
|
checkQuoting(true, "x,y")
|
|
|
|
checkQuoting(false, errors.New("invalid"))
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
2017-02-14 13:53:03 +03:00
|
|
|
|
2017-02-14 14:51:23 +03:00
|
|
|
// Test for quoting empty fields.
|
2017-02-14 13:53:03 +03:00
|
|
|
tf.QuoteEmptyFields = true
|
|
|
|
checkQuoting(true, "")
|
2017-02-15 16:08:26 +03:00
|
|
|
checkQuoting(false, "abcd")
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
2019-10-15 08:53:51 +03:00
|
|
|
|
|
|
|
// Test forcing quotes.
|
|
|
|
tf.ForceQuote = true
|
|
|
|
checkQuoting(true, "")
|
|
|
|
checkQuoting(true, "abcd")
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
2014-12-15 22:20:33 +03:00
|
|
|
}
|
2015-03-09 18:19:51 +03:00
|
|
|
|
2017-07-13 14:46:35 +03:00
|
|
|
func TestEscaping(t *testing.T) {
|
2017-07-10 15:09:37 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{`ba"r`, `ba\"r`},
|
|
|
|
{`ba'r`, `ba'r`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
if !bytes.Contains(b, []byte(tc.expected)) {
|
|
|
|
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:29:56 +03:00
|
|
|
func TestEscaping_Interface(t *testing.T) {
|
2017-07-12 18:15:13 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
2017-07-12 18:29:56 +03:00
|
|
|
|
2017-07-12 18:15:13 +03:00
|
|
|
ts := time.Now()
|
|
|
|
|
2017-07-12 18:29:56 +03:00
|
|
|
testCases := []struct {
|
|
|
|
value interface{}
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{ts, fmt.Sprintf("\"%s\"", ts.String())},
|
|
|
|
{errors.New("error: something went wrong"), "\"error: something went wrong\""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
if !bytes.Contains(b, []byte(tc.expected)) {
|
|
|
|
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
2017-07-12 18:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 02:04:45 +03:00
|
|
|
func TestTimestampFormat(t *testing.T) {
|
|
|
|
checkTimeStr := func(format string) {
|
|
|
|
customFormatter := &TextFormatter{DisableColors: true, TimestampFormat: format}
|
|
|
|
customStr, _ := customFormatter.Format(WithField("test", "test"))
|
|
|
|
timeStart := bytes.Index(customStr, ([]byte)("time="))
|
|
|
|
timeEnd := bytes.Index(customStr, ([]byte)("level="))
|
2017-07-13 14:46:35 +03:00
|
|
|
timeStr := customStr[timeStart+5+len("\"") : timeEnd-1-len("\"")]
|
2015-03-27 02:04:45 +03:00
|
|
|
if format == "" {
|
|
|
|
format = time.RFC3339
|
|
|
|
}
|
|
|
|
_, e := time.Parse(format, (string)(timeStr))
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
|
|
|
|
checkTimeStr("Mon Jan _2 15:04:05 2006")
|
|
|
|
checkTimeStr("")
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:28:23 +03:00
|
|
|
func TestDisableLevelTruncation(t *testing.T) {
|
|
|
|
entry := &Entry{
|
|
|
|
Time: time.Now(),
|
|
|
|
Message: "testing",
|
|
|
|
}
|
|
|
|
keys := []string{}
|
|
|
|
timestampFormat := "Mon Jan 2 15:04:05 -0700 MST 2006"
|
|
|
|
checkDisableTruncation := func(disabled bool, level Level) {
|
|
|
|
tf := &TextFormatter{DisableLevelTruncation: disabled}
|
|
|
|
var b bytes.Buffer
|
|
|
|
entry.Level = level
|
2018-11-05 15:29:28 +03:00
|
|
|
tf.printColored(&b, entry, keys, nil, timestampFormat)
|
2016-08-31 17:28:23 +03:00
|
|
|
logLine := (&b).String()
|
|
|
|
if disabled {
|
|
|
|
expected := strings.ToUpper(level.String())
|
|
|
|
if !strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string expected to be %s when truncation disabled", expected)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expected := strings.ToUpper(level.String())
|
|
|
|
if len(level.String()) > 4 {
|
|
|
|
if strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string %s expected to be truncated to %s when truncation is enabled", expected, expected[0:4])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string expected to be %s when truncation is enabled and level string is below truncation threshold", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDisableTruncation(true, DebugLevel)
|
|
|
|
checkDisableTruncation(true, InfoLevel)
|
|
|
|
checkDisableTruncation(false, ErrorLevel)
|
|
|
|
checkDisableTruncation(false, InfoLevel)
|
|
|
|
}
|
|
|
|
|
2019-06-05 10:10:46 +03:00
|
|
|
func TestPadLevelText(t *testing.T) {
|
|
|
|
// A note for future maintainers / committers:
|
|
|
|
//
|
|
|
|
// This test denormalizes the level text as a part of its assertions.
|
|
|
|
// Because of that, its not really a "unit test" of the PadLevelText functionality.
|
|
|
|
// So! Many apologies to the potential future person who has to rewrite this test
|
|
|
|
// when they are changing some completely unrelated functionality.
|
|
|
|
params := []struct {
|
|
|
|
name string
|
|
|
|
level Level
|
|
|
|
paddedLevelText string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "PanicLevel",
|
|
|
|
level: PanicLevel,
|
|
|
|
paddedLevelText: "PANIC ", // 2 extra spaces
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FatalLevel",
|
|
|
|
level: FatalLevel,
|
|
|
|
paddedLevelText: "FATAL ", // 2 extra spaces
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ErrorLevel",
|
|
|
|
level: ErrorLevel,
|
|
|
|
paddedLevelText: "ERROR ", // 2 extra spaces
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WarnLevel",
|
|
|
|
level: WarnLevel,
|
|
|
|
// WARNING is already the max length, so we don't need to assert a paddedLevelText
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DebugLevel",
|
|
|
|
level: DebugLevel,
|
|
|
|
paddedLevelText: "DEBUG ", // 2 extra spaces
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TraceLevel",
|
|
|
|
level: TraceLevel,
|
|
|
|
paddedLevelText: "TRACE ", // 2 extra spaces
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InfoLevel",
|
|
|
|
level: InfoLevel,
|
|
|
|
paddedLevelText: "INFO ", // 3 extra spaces
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-05 10:27:10 +03:00
|
|
|
// We create a "default" TextFormatter to do a control test.
|
|
|
|
// We also create a TextFormatter with PadLevelText, which is the parameter we want to do our most relevant assertions against.
|
2019-06-05 10:10:46 +03:00
|
|
|
tfDefault := TextFormatter{}
|
|
|
|
tfWithPadding := TextFormatter{PadLevelText: true}
|
|
|
|
|
|
|
|
for _, val := range params {
|
|
|
|
t.Run(val.name, func(t *testing.T) {
|
|
|
|
// TextFormatter writes into these bytes.Buffers, and we make assertions about their contents later
|
|
|
|
var bytesDefault bytes.Buffer
|
|
|
|
var bytesWithPadding bytes.Buffer
|
|
|
|
|
|
|
|
// The TextFormatter instance and the bytes.Buffer instance are different here
|
2019-06-25 06:50:37 +03:00
|
|
|
// all the other arguments are the same. We also initialize them so that they
|
|
|
|
// fill in the value of levelTextMaxLength.
|
|
|
|
tfDefault.init(&Entry{})
|
2019-06-05 10:10:46 +03:00
|
|
|
tfDefault.printColored(&bytesDefault, &Entry{Level: val.level}, []string{}, nil, "")
|
2019-06-25 06:50:37 +03:00
|
|
|
tfWithPadding.init(&Entry{})
|
2019-06-05 10:10:46 +03:00
|
|
|
tfWithPadding.printColored(&bytesWithPadding, &Entry{Level: val.level}, []string{}, nil, "")
|
|
|
|
|
|
|
|
// turn the bytes back into a string so that we can actually work with the data
|
|
|
|
logLineDefault := (&bytesDefault).String()
|
|
|
|
logLineWithPadding := (&bytesWithPadding).String()
|
|
|
|
|
|
|
|
// Control: the level text should not be padded by default
|
|
|
|
if val.paddedLevelText != "" && strings.Contains(logLineDefault, val.paddedLevelText) {
|
2019-06-27 06:37:17 +03:00
|
|
|
t.Errorf("log line %q should not contain the padded level text %q by default", logLineDefault, val.paddedLevelText)
|
2019-06-05 10:10:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assertion: the level text should still contain the string representation of the level
|
|
|
|
if !strings.Contains(strings.ToLower(logLineWithPadding), val.level.String()) {
|
2019-06-27 06:37:17 +03:00
|
|
|
t.Errorf("log line %q should contain the level text %q when padding is enabled", logLineWithPadding, val.level.String())
|
2019-06-05 10:10:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assertion: the level text should be in its padded form now
|
|
|
|
if val.paddedLevelText != "" && !strings.Contains(logLineWithPadding, val.paddedLevelText) {
|
2019-06-27 06:37:17 +03:00
|
|
|
t.Errorf("log line %q should contain the padded level text %q when padding is enabled", logLineWithPadding, val.paddedLevelText)
|
2019-06-05 10:10:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:59:41 +03:00
|
|
|
func TestDisableTimestampWithColoredOutput(t *testing.T) {
|
|
|
|
tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
|
|
|
|
|
|
|
|
b, _ := tf.Format(WithField("test", "test"))
|
|
|
|
if strings.Contains(string(b), "[0000]") {
|
|
|
|
t.Error("timestamp not expected when DisableTimestamp is true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:26:35 +03:00
|
|
|
func TestNewlineBehavior(t *testing.T) {
|
|
|
|
tf := &TextFormatter{ForceColors: true}
|
|
|
|
|
|
|
|
// Ensure a single new line is removed as per stdlib log
|
|
|
|
e := NewEntry(StandardLogger())
|
|
|
|
e.Message = "test message\n"
|
|
|
|
b, _ := tf.Format(e)
|
|
|
|
if bytes.Contains(b, []byte("test message\n")) {
|
|
|
|
t.Error("first newline at end of Entry.Message resulted in unexpected 2 newlines in output. Expected newline to be removed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a double new line is reduced to a single new line
|
|
|
|
e = NewEntry(StandardLogger())
|
|
|
|
e.Message = "test message\n\n"
|
|
|
|
b, _ = tf.Format(e)
|
|
|
|
if bytes.Contains(b, []byte("test message\n\n")) {
|
|
|
|
t.Error("Double newline at end of Entry.Message resulted in unexpected 2 newlines in output. Expected single newline")
|
|
|
|
}
|
|
|
|
if !bytes.Contains(b, []byte("test message\n")) {
|
|
|
|
t.Error("Double newline at end of Entry.Message did not result in a single newline after formatting")
|
|
|
|
}
|
2018-08-05 23:40:58 +03:00
|
|
|
}
|
2018-08-05 23:34:28 +03:00
|
|
|
|
2017-11-22 06:43:47 +03:00
|
|
|
func TestTextFormatterFieldMap(t *testing.T) {
|
|
|
|
formatter := &TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
FieldMap: FieldMap{
|
|
|
|
FieldKeyMsg: "message",
|
|
|
|
FieldKeyLevel: "somelevel",
|
|
|
|
FieldKeyTime: "timeywimey",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &Entry{
|
|
|
|
Message: "oh hi",
|
|
|
|
Level: WarnLevel,
|
|
|
|
Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC),
|
2018-06-19 04:32:35 +03:00
|
|
|
Data: Fields{
|
|
|
|
"field1": "f1",
|
|
|
|
"message": "messagefield",
|
|
|
|
"somelevel": "levelfield",
|
|
|
|
"timeywimey": "timeywimeyfield",
|
|
|
|
},
|
2017-11-22 06:43:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := formatter.Format(entry)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t,
|
2018-06-19 04:32:35 +03:00
|
|
|
`timeywimey="1981-02-24T04:28:03Z" `+
|
|
|
|
`somelevel=warning `+
|
|
|
|
`message="oh hi" `+
|
|
|
|
`field1=f1 `+
|
|
|
|
`fields.message=messagefield `+
|
|
|
|
`fields.somelevel=levelfield `+
|
|
|
|
`fields.timeywimey=timeywimeyfield`+"\n",
|
2017-11-22 06:43:47 +03:00
|
|
|
string(b),
|
2018-06-19 04:39:53 +03:00
|
|
|
"Formatted output doesn't respect FieldMap")
|
2017-11-22 06:43:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:01:49 +03:00
|
|
|
func TestTextFormatterIsColored(t *testing.T) {
|
|
|
|
params := []struct {
|
|
|
|
name string
|
|
|
|
expectedResult bool
|
|
|
|
isTerminal bool
|
|
|
|
disableColor bool
|
|
|
|
forceColor bool
|
|
|
|
envColor bool
|
|
|
|
clicolorIsSet bool
|
|
|
|
clicolorForceIsSet bool
|
|
|
|
clicolorVal string
|
|
|
|
clicolorForceVal string
|
|
|
|
}{
|
|
|
|
// Default values
|
|
|
|
{
|
|
|
|
name: "testcase1",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: false,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
},
|
|
|
|
// Output on terminal
|
|
|
|
{
|
|
|
|
name: "testcase2",
|
|
|
|
expectedResult: true,
|
|
|
|
isTerminal: true,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: false,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
},
|
|
|
|
// Output on terminal with color disabled
|
|
|
|
{
|
|
|
|
name: "testcase3",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: true,
|
|
|
|
disableColor: true,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: false,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
},
|
|
|
|
// Output not on terminal with color disabled
|
|
|
|
{
|
|
|
|
name: "testcase4",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: true,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: false,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
},
|
|
|
|
// Output not on terminal with color forced
|
|
|
|
{
|
|
|
|
name: "testcase5",
|
|
|
|
expectedResult: true,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: true,
|
|
|
|
envColor: false,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
},
|
|
|
|
// Output on terminal with clicolor set to "0"
|
|
|
|
{
|
|
|
|
name: "testcase6",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: true,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "0",
|
|
|
|
},
|
|
|
|
// Output on terminal with clicolor set to "1"
|
|
|
|
{
|
|
|
|
name: "testcase7",
|
|
|
|
expectedResult: true,
|
|
|
|
isTerminal: true,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "1",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor set to "0"
|
|
|
|
{
|
|
|
|
name: "testcase8",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "0",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor set to "1"
|
|
|
|
{
|
|
|
|
name: "testcase9",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "1",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor set to "1" and force color
|
|
|
|
{
|
|
|
|
name: "testcase10",
|
|
|
|
expectedResult: true,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: true,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "1",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor set to "0" and force color
|
|
|
|
{
|
|
|
|
name: "testcase11",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: true,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: true,
|
|
|
|
clicolorForceIsSet: false,
|
|
|
|
clicolorVal: "0",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor_force set to "1"
|
|
|
|
{
|
2018-08-13 18:27:32 +03:00
|
|
|
name: "testcase12",
|
2018-08-09 16:01:49 +03:00
|
|
|
expectedResult: true,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: true,
|
|
|
|
clicolorForceVal: "1",
|
|
|
|
},
|
|
|
|
// Output not on terminal with clicolor_force set to "0"
|
|
|
|
{
|
2018-08-13 18:27:32 +03:00
|
|
|
name: "testcase13",
|
2018-08-09 16:01:49 +03:00
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: false,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: true,
|
|
|
|
clicolorForceVal: "0",
|
|
|
|
},
|
|
|
|
// Output on terminal with clicolor_force set to "0"
|
|
|
|
{
|
|
|
|
name: "testcase14",
|
|
|
|
expectedResult: false,
|
|
|
|
isTerminal: true,
|
|
|
|
disableColor: false,
|
|
|
|
forceColor: false,
|
|
|
|
envColor: true,
|
|
|
|
clicolorIsSet: false,
|
|
|
|
clicolorForceIsSet: true,
|
|
|
|
clicolorForceVal: "0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-13 18:27:32 +03:00
|
|
|
cleanenv := func() {
|
|
|
|
os.Unsetenv("CLICOLOR")
|
|
|
|
os.Unsetenv("CLICOLOR_FORCE")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanenv()
|
2018-08-09 16:01:49 +03:00
|
|
|
|
|
|
|
for _, val := range params {
|
|
|
|
t.Run("textformatter_"+val.name, func(subT *testing.T) {
|
|
|
|
tf := TextFormatter{
|
|
|
|
isTerminal: val.isTerminal,
|
|
|
|
DisableColors: val.disableColor,
|
|
|
|
ForceColors: val.forceColor,
|
|
|
|
EnvironmentOverrideColors: val.envColor,
|
|
|
|
}
|
2018-08-13 18:27:32 +03:00
|
|
|
cleanenv()
|
2018-08-09 16:01:49 +03:00
|
|
|
if val.clicolorIsSet {
|
|
|
|
os.Setenv("CLICOLOR", val.clicolorVal)
|
|
|
|
}
|
|
|
|
if val.clicolorForceIsSet {
|
|
|
|
os.Setenv("CLICOLOR_FORCE", val.clicolorForceVal)
|
|
|
|
}
|
|
|
|
res := tf.isColored()
|
2018-12-09 23:47:44 +03:00
|
|
|
if runtime.GOOS == "windows" && !tf.ForceColors && !val.clicolorForceIsSet {
|
2018-11-16 17:19:37 +03:00
|
|
|
assert.Equal(subT, false, res)
|
|
|
|
} else {
|
|
|
|
assert.Equal(subT, val.expectedResult, res)
|
|
|
|
}
|
2018-08-09 16:01:49 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:45:23 +03:00
|
|
|
func TestCustomSorting(t *testing.T) {
|
|
|
|
formatter := &TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
SortingFunc: func(keys []string) {
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
if keys[j] == "prefix" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if keys[i] == "prefix" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.Compare(keys[i], keys[j]) == -1
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &Entry{
|
|
|
|
Message: "Testing custom sort function",
|
|
|
|
Time: time.Now(),
|
|
|
|
Level: InfoLevel,
|
|
|
|
Data: Fields{
|
|
|
|
"test": "testvalue",
|
|
|
|
"prefix": "the application prefix",
|
|
|
|
"blablabla": "blablabla",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := formatter.Format(entry)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(string(b), "prefix="), "format output is %q", string(b))
|
|
|
|
}
|