diff --git a/configs_test.go b/configs_test.go index def9e61..230fbfa 100644 --- a/configs_test.go +++ b/configs_test.go @@ -1,6 +1,7 @@ package logrusiowriter import ( + "bytes" "testing" "github.com/sirupsen/logrus" @@ -51,4 +52,73 @@ func TestWithConfig(t *testing.T) { t.Errorf("Error provided to OnLevelParseError should not be nil") } }) + + t.Run("trimming", func(t *testing.T) { + const newLineAppendedByLogrus = "\n" + for _, tc := range []struct { + desc string + trimming bool + input string + expected string + }{ + { + desc: "zerolength with trimming", + trimming: true, + input: "", + expected: "level=info" + newLineAppendedByLogrus, + }, + { + desc: "zerolength without trimming", + trimming: false, + input: "", + expected: "level=info" + newLineAppendedByLogrus, + }, + { + desc: "only newline with trimming", + trimming: true, + input: "\n", + expected: "level=info" + newLineAppendedByLogrus, + }, + { + desc: "only newline without trimming", + trimming: false, + input: "\n", + expected: `level=info msg="\n"` + newLineAppendedByLogrus, + }, + { + desc: "with trailing newline and trimming", + trimming: true, + input: "message\n", + expected: "level=info msg=message" + newLineAppendedByLogrus, + }, + { + desc: "with trailing newline and no trimming", + trimming: false, + input: "message\n", + expected: `level=info msg="message\n"` + newLineAppendedByLogrus, + }, + { + desc: "no newline with trimming", + trimming: true, + input: "message", + expected: "level=info msg=message" + newLineAppendedByLogrus, + }, + } { + t.Run(tc.desc, func(t *testing.T) { + buf := &bytes.Buffer{} + + bufLogger := logrus.New() + bufLogger.SetOutput(buf) + bufLogger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true + + writer := New(WithLogger(bufLogger), WithTrailingNewLineTrimming(tc.trimming)) + + _, _ = writer.Write([]byte(tc.input)) + + if buf.String() != tc.expected { + t.Errorf("Unexpected output\nExpected: '%s'\nGot: '%s'", tc.expected, buf.String()) + } + }) + } + }) }