Skip to content

Commit

Permalink
chore: add more pkg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 8, 2023
1 parent 5da9367 commit 7ba0790
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 9 deletions.
12 changes: 6 additions & 6 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,32 +296,32 @@ func (l *Logger) Print(msg interface{}, keyvals ...interface{}) {
}

// Debugf prints a debug message with formatting.
func (l *logger) Debugf(format string, args ...interface{}) {
func (l *Logger) Debugf(format string, args ...interface{}) {
l.log(DebugLevel, fmt.Sprintf(format, args...))
}

// Infof prints an info message with formatting.
func (l *logger) Infof(format string, args ...interface{}) {
func (l *Logger) Infof(format string, args ...interface{}) {
l.log(InfoLevel, fmt.Sprintf(format, args...))
}

// Warnf prints a warning message with formatting.
func (l *logger) Warnf(format string, args ...interface{}) {
func (l *Logger) Warnf(format string, args ...interface{}) {
l.log(WarnLevel, fmt.Sprintf(format, args...))
}

// Errorf prints an error message with formatting.
func (l *logger) Errorf(format string, args ...interface{}) {
func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(ErrorLevel, fmt.Sprintf(format, args...))
}

// Fatalf prints a fatal message with formatting and exits.
func (l *logger) Fatalf(format string, args ...interface{}) {
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log(FatalLevel, fmt.Sprintf(format, args...))
os.Exit(1)
}

// Printf prints a message with no level and formatting.
func (l *logger) Printf(format string, args ...interface{}) {
func (l *Logger) Printf(format string, args ...interface{}) {
l.log(noLevel, fmt.Sprintf(format, args...))
}
7 changes: 4 additions & 3 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func TestWrongLevel(t *testing.T) {

func TestLogFormatter(t *testing.T) {
var buf bytes.Buffer
l := New(WithOutput(&buf), WithLevel(DebugLevel))
l := New(&buf)
l.SetLevel(DebugLevel)
cases := []struct {
name string
format string
Expand All @@ -99,7 +100,7 @@ func TestLogFormatter(t *testing.T) {
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Debugf,
expected: "DEBUG foo bar\n",
expected: "DEBU foo bar\n",
},
{
name: "warn format",
Expand All @@ -113,7 +114,7 @@ func TestLogFormatter(t *testing.T) {
format: "%s %s",
args: []interface{}{"foo", "bar"},
fun: l.Errorf,
expected: "ERROR foo bar\n",
expected: "ERRO foo bar\n",
},
}
for _, c := range cases {
Expand Down
129 changes: 129 additions & 0 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package log

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -56,12 +60,31 @@ func TestPrint(t *testing.T) {
SetOutput(&buf)
SetLevel(FatalLevel)
SetTimeFunction(_zeroTime)
SetReportTimestamp(true)
SetReportCaller(false)
SetTimeFormat(DefaultTimeFormat)
Error("error")
Print("print")
assert.Equal(t, "0001/01/01 00:00:00 print\n", buf.String())
}

func TestPrintf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(FatalLevel)
SetTimeFunction(_zeroTime)
SetReportTimestamp(true)
SetReportCaller(false)
SetTimeFormat(DefaultTimeFormat)
Errorf("error")
Printf("print")
assert.Equal(t, "0001/01/01 00:00:00 print\n", buf.String())
}

func TestFatal(t *testing.T) {
SetReportTimestamp(true)
SetReportCaller(false)
SetTimeFormat(DefaultTimeFormat)
if os.Getenv("FATAL") == "1" {
Fatal("i'm dead")
return
Expand All @@ -74,3 +97,109 @@ func TestFatal(t *testing.T) {
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

func TestFatalf(t *testing.T) {
SetReportTimestamp(true)
SetReportCaller(false)
SetTimeFormat(DefaultTimeFormat)
if os.Getenv("FATAL") == "1" {
Fatalf("i'm %s", "dead")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatalf")
cmd.Env = append(os.Environ(), "FATAL=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

func TestDebugf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(DebugLevel)
SetTimeFunction(_zeroTime)
SetReportTimestamp(true)
SetReportCaller(true)
SetTimeFormat(DefaultTimeFormat)
_, file, line, _ := runtime.Caller(0)
Debugf("debug %s", "foo")
assert.Equal(t, fmt.Sprintf("0001/01/01 00:00:00 DEBU <log/%s:%d> debug foo\n", filepath.Base(file), line+1), buf.String())
}

func TestInfof(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(InfoLevel)
SetReportTimestamp(false)
SetReportCaller(false)
SetTimeFormat(DefaultTimeFormat)
Infof("info %s", "foo")
assert.Equal(t, "INFO info foo\n", buf.String())
}

func TestWarnf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(WarnLevel)
SetReportCaller(false)
SetReportTimestamp(true)
SetTimeFunction(_zeroTime)
SetTimeFormat(DefaultTimeFormat)
Warnf("warn %s", "foo")
assert.Equal(t, "0001/01/01 00:00:00 WARN warn foo\n", buf.String())
}

func TestErrorf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(ErrorLevel)
SetReportCaller(false)
SetReportTimestamp(true)
SetTimeFunction(_zeroTime)
SetTimeFormat(time.Kitchen)
Errorf("error %s", "foo")
assert.Equal(t, "12:00AM ERRO error foo\n", buf.String())
}

func TestWith(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(InfoLevel)
SetReportCaller(false)
SetReportTimestamp(true)
SetTimeFunction(_zeroTime)
SetTimeFormat(DefaultTimeFormat)
With("foo", "bar").Info("info")
assert.Equal(t, "0001/01/01 00:00:00 INFO info foo=bar\n", buf.String())
}

func TestGetLevel(t *testing.T) {
SetLevel(InfoLevel)
assert.Equal(t, InfoLevel, GetLevel())
}

func TestPrefix(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(InfoLevel)
SetReportCaller(false)
SetReportTimestamp(false)
SetPrefix("prefix")
Info("info")
assert.Equal(t, "INFO prefix: info\n", buf.String())
assert.Equal(t, "prefix", GetPrefix())
SetPrefix("")
}

func TestFormatter(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetLevel(InfoLevel)
SetReportCaller(false)
SetReportTimestamp(false)
SetFormatter(JSONFormatter)
Info("info")
assert.Equal(t, "{\"lvl\":\"info\",\"msg\":\"info\"}\n", buf.String())
}

0 comments on commit 7ba0790

Please sign in to comment.