2023-03-21 04:18:09 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package logger_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "git.rob.mx/nidito/chinampa/pkg/logger"
|
|
|
|
rt "git.rob.mx/nidito/chinampa/pkg/runtime"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func withEnv(t *testing.T, env map[string]string) {
|
|
|
|
prevEnv := os.Environ()
|
|
|
|
for _, entry := range prevEnv {
|
|
|
|
parts := strings.SplitN(entry, "=", 2)
|
|
|
|
os.Unsetenv(parts[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range env {
|
|
|
|
os.Setenv(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2023-03-21 06:28:25 +00:00
|
|
|
rt.ResetParsedFlagsCache()
|
2023-03-21 04:18:09 +00:00
|
|
|
|
|
|
|
for k := range env {
|
|
|
|
os.Unsetenv(k)
|
|
|
|
}
|
|
|
|
for _, entry := range prevEnv {
|
|
|
|
parts := strings.SplitN(entry, "=", 2)
|
|
|
|
os.Setenv(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-20 05:47:15 +00:00
|
|
|
func escaped(a string) string {
|
|
|
|
return strings.ReplaceAll(a, "\033", "\\033")
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:18:09 +00:00
|
|
|
func TestFormatter(t *testing.T) {
|
|
|
|
now := strings.Replace(time.Now().Local().Format(time.DateTime), " ", "T", 1)
|
|
|
|
cases := []struct {
|
|
|
|
Color bool
|
|
|
|
Verbose bool
|
|
|
|
Call func(args ...any)
|
|
|
|
Expects string
|
|
|
|
Level logrus.Level
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Color: true,
|
|
|
|
Call: Info,
|
|
|
|
Expects: "message",
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: true,
|
|
|
|
Verbose: true,
|
|
|
|
Call: Info,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: fmt.Sprintf("\033[2m%s\033[22m \033[2minfo\033[22m\033[2m\033[22m message", now),
|
2023-03-21 04:18:09 +00:00
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: true,
|
|
|
|
Call: Debug,
|
|
|
|
Expects: "",
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Debug,
|
|
|
|
Expects: "DEBUG: message",
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: true,
|
|
|
|
Call: Debug,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: "\033[2mDEBUG: \033[22m\033[2mmessage\033[22m",
|
2023-03-21 04:18:09 +00:00
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: true,
|
|
|
|
Verbose: true,
|
|
|
|
Call: Debug,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: fmt.Sprintf("\033[2m%s\033[22m \033[2mdebug\033[22m\033[2m\033[22m\t\033[2mmessage\033[22m",
|
2023-03-21 04:18:09 +00:00
|
|
|
now),
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Trace,
|
|
|
|
Expects: "",
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Trace,
|
|
|
|
Expects: "TRACE: message",
|
|
|
|
Level: logrus.TraceLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Warn,
|
|
|
|
Expects: "",
|
|
|
|
Level: logrus.ErrorLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Warn,
|
|
|
|
Expects: "WARNING: message",
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Warn,
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
Color: true,
|
|
|
|
Verbose: true,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: fmt.Sprintf("\033[2m%s\033[22m \033[1;93mwarning\033[22;0m\033[2m\033[22m\tmessage", now),
|
2023-03-21 04:18:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Warn,
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
Color: true,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: "\033[1;43;30m WARNING \033[22;0;0m message",
|
2023-03-21 04:18:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Error,
|
|
|
|
Expects: "ERROR: message",
|
|
|
|
Level: logrus.ErrorLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Error,
|
|
|
|
Expects: "ERROR: message",
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Error,
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
Color: true,
|
|
|
|
Verbose: true,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: fmt.Sprintf("\033[2m%s\033[22m \033[1;91merror\033[22;0m\033[2m\033[22m\tmessage", now),
|
2023-03-21 04:18:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Call: Error,
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
Color: true,
|
2024-04-20 05:47:15 +00:00
|
|
|
Expects: "\033[1;41;225m ERROR \033[22;0;0m message",
|
2023-03-21 04:18:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
fname := runtime.FuncForPC(reflect.ValueOf(c.Call).Pointer()).Name()
|
|
|
|
comps := []string{fname, c.Level.String()}
|
|
|
|
if c.Color {
|
|
|
|
comps = append(comps, "color")
|
|
|
|
}
|
|
|
|
if c.Verbose {
|
|
|
|
comps = append(comps, "verbose")
|
|
|
|
}
|
|
|
|
name := strings.Join(comps, "/")
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
env := map[string]string{
|
|
|
|
"COLOR": "",
|
|
|
|
"VERBOSE": "",
|
|
|
|
}
|
|
|
|
if c.Color {
|
|
|
|
env["COLOR"] = "always"
|
|
|
|
} else {
|
|
|
|
env["NO_COLOR"] = "1"
|
|
|
|
}
|
|
|
|
if c.Verbose {
|
|
|
|
env["VERBOSE"] = "1"
|
|
|
|
}
|
|
|
|
withEnv(t, env)
|
|
|
|
data := bytes.Buffer{}
|
|
|
|
logrus.SetLevel(c.Level)
|
|
|
|
logrus.SetOutput(&data)
|
|
|
|
c.Call("message")
|
|
|
|
expected := c.Expects
|
|
|
|
if c.Expects != "" {
|
|
|
|
expected = c.Expects + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := data.String(); res != expected {
|
2024-04-20 05:47:15 +00:00
|
|
|
t.Fatalf("%s:\ngot : %s\nwanted: %v", name, escaped(res), escaped(expected))
|
2023-03-21 04:18:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|