forked from fergusstrange/embedded-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
58 lines (37 loc) · 1.06 KB
/
logging_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package embeddedpostgres
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type customLogger struct {
logLines []byte
}
func (cl *customLogger) Write(p []byte) (n int, err error) {
cl.logLines = append(cl.logLines, p...)
return len(p), nil
}
func Test_SyncedLogger_CreateError(t *testing.T) {
logger := customLogger{}
_, err := newSyncedLogger("/not-exists-anywhere", &logger)
assert.Error(t, err)
}
func Test_SyncedLogger_ErrorDuringFlush(t *testing.T) {
logger := customLogger{}
sl, slErr := newSyncedLogger("", &logger)
assert.NoError(t, slErr)
rmFileErr := os.Remove(sl.file.Name())
assert.NoError(t, rmFileErr)
err := sl.flush()
assert.Error(t, err)
}
func Test_SyncedLogger_NoErrorDuringFlush(t *testing.T) {
logger := customLogger{}
sl, slErr := newSyncedLogger("", &logger)
assert.NoError(t, slErr)
err := os.WriteFile(sl.file.Name(), []byte("some logs\non a new line"), os.ModeAppend)
assert.NoError(t, err)
err = sl.flush()
assert.NoError(t, err)
assert.Equal(t, "some logs\non a new line", string(logger.logLines))
}