40 lines
988 B
Go
40 lines
988 B
Go
package utils
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestErrorWrapper(t *testing.T) {
|
|
scanner, _, writer := mockLogger(t)
|
|
log := zerolog.New(writer)
|
|
|
|
TestError1 := errors.New("first error")
|
|
TestError2 := errors.New("secound error")
|
|
|
|
resulterror := WrapError(TestError1, TestError2, log)
|
|
|
|
assert.Equal(t, "first error: secound error", resulterror.Error())
|
|
|
|
scanner.Scan()
|
|
got := scanner.Text()
|
|
assert.Equal(t, "{\"level\":\"error\",\"error\":\"secound error\",\"message\":\"first error\"}", got)
|
|
|
|
assert.ErrorIs(t, resulterror, TestError1)
|
|
assert.NotErrorIs(t, resulterror, TestError2)
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/44119951/how-to-check-a-log-output-in-go-test
|
|
func mockLogger(t *testing.T) (*bufio.Scanner, *os.File, *os.File) {
|
|
reader, writer, err := os.Pipe()
|
|
if err != nil {
|
|
assert.Fail(t, "couldn't get os Pipe: %v", err)
|
|
}
|
|
|
|
return bufio.NewScanner(reader), reader, writer
|
|
}
|