Adding test for line override

Signed-off-by: Kris Nóva <kris@nivenly.com>
master
Kris Nóva 2021-02-25 13:10:59 -08:00
parent dc4e3aa5d3
commit 352a85877c
2 changed files with 36 additions and 11 deletions

View File

@ -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]
//

View File

@ -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()
}
}