A golang package for logging to a file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

102 lines
1.7 KiB

package flog
import (
"bufio"
"bytes"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetOutput(t *testing.T) {
assert.Equal(t, os.Stdout, getOutput())
b := bytes.Buffer{}
Output = &b
assert.Equal(t, &b, getOutput())
Output = nil
}
func TestLogFormat(t *testing.T) {
b := bytes.Buffer{}
MinLevel, Output = LevelDebug, &b
Debug(`Testtt`)
assert.True(t, strings.HasPrefix(b.String(), `debug [`))
b.Reset()
Info(`Testtt`)
assert.True(t, strings.HasPrefix(b.String(), `info [`))
MinLevel, Output = LevelInfo, nil
}
func TestLogLevel(t *testing.T) {
b := bytes.Buffer{}
MinLevel, Output = LevelWarning, &b
Info(`Testtt`)
assert.True(t, b.Len() == 0)
b.Reset()
Warning(`Testtt`)
assert.False(t, b.Len() == 0)
b.Reset()
MinLevel = LevelError
Warning(`Testtt`)
assert.True(t, b.Len() == 0)
b.Reset()
MinLevel, Output = LevelInfo, nil
}
func TestLogf(t *testing.T) {
b := bytes.Buffer{}
Output = &b
Infof(`Test%s-%d`, `tt`, 1)
assert.True(t, strings.Contains(b.String(), `Testtt-1`))
Output = nil
}
func TestStackTrace(t *testing.T) {
b := bytes.Buffer{}
Output = &b
Error(`Testtt`)
checkStackTrace(t, &b, `git.fuyu.moe/Fuyu/flog.TestStackTrace`)
b.Reset()
func() {
defer func() {
v := recover()
if v != nil {
Critical(v)
}
}()
panic(`Testtt`)
}()
checkStackTrace(t, &b, `git.fuyu.moe/Fuyu/flog.TestStackTrace.func1`)
Output = nil
}
func checkStackTrace(t *testing.T, b *bytes.Buffer, f string) {
rdr := bufio.NewReader(b)
_, _ = rdr.ReadBytes('\n')
data, err := rdr.ReadBytes('\n')
if err != nil {
t.Fatal(`Got error while reading:`, err)
}
parts := strings.Split(strings.TrimSpace(string(data)), ` `)
assert.Equal(t, f, parts[0])
}