add specs for logging and proc scanner

This commit is contained in:
Dominic Breuker
2018-03-06 09:42:10 +01:00
parent 572ce2ef3e
commit d3c7681096
5 changed files with 134 additions and 25 deletions

View File

@@ -1,10 +1,17 @@
package logging
import (
"fmt"
"log"
"os"
)
const (
ColorNone = iota
ColorRed
ColorGreen
)
type Logger struct {
infoLogger *log.Logger
errorLogger *log.Logger
@@ -30,6 +37,16 @@ func (l *Logger) Errorf(format string, v ...interface{}) {
}
// Eventf writes an event with timestamp to stdout
func (l *Logger) Eventf(format string, v ...interface{}) {
l.eventLogger.Printf(format, v...)
func (l *Logger) Eventf(color int, format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
switch color {
case ColorRed:
msg = fmt.Sprintf("\x1b[31;1m%s\x1b[0m", msg)
case ColorGreen:
msg = fmt.Sprintf("\x1b[32;1m%s\x1b[0m", msg)
default:
}
l.eventLogger.Printf("%s", msg)
}

View File

@@ -3,11 +3,15 @@ package logging
import (
"bytes"
"log"
"reflect"
"regexp"
"testing"
)
const dateFormatPattern = `[\d]{4}/[\d]{2}/[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}`
const ansiPattern = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var ansiMatcher = regexp.MustCompile(ansiPattern)
var l = NewLogger()
@@ -15,27 +19,39 @@ var logTests = []struct {
logger *log.Logger
test func()
expectation string
colors [][]byte
}{
{l.infoLogger, func() { l.Infof("Info message no. %d", 1) }, "Info message no. 1\n"},
{l.infoLogger, func() { l.Infof("Info message no. %d with a string %s\n", 2, "appended to it") }, "Info message no. 2 with a string appended to it\n"},
{l.errorLogger, func() { l.Errorf("Error message") }, "Error message\n"},
{l.errorLogger, func() { l.Errorf("Error message\n") }, "Error message\n"},
{l.eventLogger, func() { l.Eventf("Event message") }, dateFormatPattern + " Event message\n"},
{l.infoLogger, func() { l.Infof("Info message no. %d", 1) }, "Info message no. 1\n", nil},
{l.infoLogger, func() { l.Infof("Info message no. %d with a string %s\n", 2, "appended to it") }, "Info message no. 2 with a string appended to it\n", nil},
{l.errorLogger, func() { l.Errorf("Error message") }, "Error message\n", nil},
{l.errorLogger, func() { l.Errorf("Error message\n") }, "Error message\n", nil},
{l.eventLogger, func() { l.Eventf(ColorNone, "Event message") }, dateFormatPattern + " Event message\n", nil},
{l.eventLogger, func() { l.Eventf(ColorRed, "Event message") }, dateFormatPattern + " Event message\n", [][]byte{[]byte("\x1b[31;1m"), []byte("\x1b[0m")}},
{l.eventLogger, func() { l.Eventf(ColorGreen, "Event message") }, dateFormatPattern + " Event message\n", [][]byte{[]byte("\x1b[32;1m"), []byte("\x1b[0m")}},
}
func TestLogging(t *testing.T) {
for i, tt := range logTests {
actual := captureOutput(tt.logger, tt.test)
// check colors and remove afterwards
colors := ansiMatcher.FindAll(actual, 2)
if !reflect.DeepEqual(colors, tt.colors) {
t.Errorf("[%d] Wrong colors: got %+v but want %+v", i, colors, tt.colors)
}
actual = ansiMatcher.ReplaceAll(actual, []byte(""))
// check contents
matcher := regexp.MustCompile(tt.expectation)
if !matcher.Match([]byte(actual)) {
t.Fatalf("[%d] Wrong message logged!: %s", i, actual)
if !matcher.Match(actual) {
t.Errorf("[%d] Wrong message logged!: got %s but wanted %v", i, actual, matcher)
}
}
}
func captureOutput(logger *log.Logger, f func()) string {
func captureOutput(logger *log.Logger, f func()) []byte {
var buf bytes.Buffer
logger.SetOutput(&buf)
f()
return buf.String()
return buf.Bytes()
}