From 352a85877c30081a6236de4e2eee14c281c04ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kris=20N=C3=B3va?= Date: Thu, 25 Feb 2021 13:10:59 -0800 Subject: [PATCH] Adding test for line override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kris NĂ³va --- logger.go | 11 ----------- logger_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/logger.go b/logger.go index 51acbf0..e23bf1e 100644 --- a/logger.go +++ b/logger.go @@ -24,17 +24,6 @@ import ( type LoggerFunc func(format string, a ...interface{}) -//type Logger interface { -// LineBytes(prefix, format string, a ...interface{}) []byte -// Line(prefix, format string, a ...interface{}) string -// Always(format string, a ...interface{}) -// Success(format string, a ...interface{}) -// Debug(format string, a ...interface{}) -// Info(format string, a ...interface{}) -// Warning(format string, a ...interface{}) -// Critical(format string, a ...interface{}) -//} - const ( // [Log Constants] // diff --git a/logger_test.go b/logger_test.go index 6846c54..db8e919 100644 --- a/logger_test.go +++ b/logger_test.go @@ -17,7 +17,12 @@ package logger import ( "bytes" "fmt" + "strings" "testing" + "time" + + "github.com/fatih/color" + "github.com/kris-nova/novaarchive/logger" ) const ( @@ -206,3 +211,34 @@ func TestAlwaysCriticalDebugOnly(t *testing.T) { t.Errorf("Expected (%s) Actual (%s)", expected, actual) } } + +// Note: Taken from the use case in eksctl +// https://github.com/michaelbeaumont/eksctl/blob/a1564e4cf825be9fd8936794e230d3b5c2d267ea/cmd/eksctl/main.go#L78-L79 +func TestLineOverride(t *testing.T) { + Level = -1 + BitwiseLevel = LogAlways + Line = func(prefix, format string, a ...interface{}) string { + if !strings.Contains(format, "\n") { + format = fmt.Sprintf("%s%s", format, "\n") + } + if logger.Timestamps { + now := time.Now() + fNow := now.Format(logger.Layout) + prefix = fmt.Sprintf("%s [%s]", fNow, prefix) + } else { + prefix = fmt.Sprintf("[%s]", prefix) + } + out := fmt.Sprintf("%s %s", prefix, fmt.Sprintf(format, a...)) + out = color.GreenString(out) + return out + } + buffer := new(bytes.Buffer) + Writer = buffer + Always(TestFormat, testA...) + actual := buffer.String() + expected := Line(PreAlways, TestFormat, testA...) + if actual != expected { + t.Errorf("actual(%s) != expected(%s)", actual, expected) + t.FailNow() + } +}