-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlogger_test.go
144 lines (121 loc) · 3.82 KB
/
logger_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/term"
"github.com/creativeprojects/resticprofile/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func readTail(t *testing.T, filename string, count int) (lines []string) {
t.Helper()
file, e := os.Open(filename)
require.NoError(t, e)
defer file.Close()
for s := bufio.NewScanner(file); s.Scan(); require.NoError(t, s.Err()) {
lines = append(lines, s.Text())
}
if len(lines) < count {
count = len(lines)
}
return lines[len(lines)-count:]
}
func TestFileHandlerWithTemporaryDirMarker(t *testing.T) {
util.ClearTempDir()
defer util.ClearTempDir()
logFile := filepath.Join(util.MustGetTempDir(), "sub", "file.log")
assert.NoFileExists(t, logFile)
handler, _, err := getFileHandler(filepath.Join(constants.TemporaryDirMarker, "sub", "file.log"))
require.NoError(t, err)
assert.FileExists(t, logFile)
assert.NoError(t, handler.Close())
util.ClearTempDir()
assert.NoFileExists(t, logFile)
}
func TestFileHandler(t *testing.T) {
logFile := filepath.Join(t.TempDir(), "file.log")
handler, writer, err := getFileHandler(logFile)
require.NoError(t, err)
defer handler.Close()
require.Implements(t, (*term.Flusher)(nil), writer)
flusher := writer.(term.Flusher)
log := func(line string) {
assert.NoError(t, handler.LogEntry(clog.LogEntry{Level: clog.LevelInfo, Format: line}))
}
// output is accessible while handler is not closed
{
log("log-line-1")
assert.NoError(t, flusher.Flush())
lines := readTail(t, logFile, 1)
require.NotEmpty(t, lines)
assert.Regexp(t, `^.+\slog-line-1$`, lines[0])
}
// output is buffered
{
log("log-line-2")
assert.Regexp(t, `^.+\slog-line-1$`, readTail(t, logFile, 1)[0])
assert.NoError(t, flusher.Flush())
assert.Regexp(t, `^.+\slog-line-2$`, readTail(t, logFile, 1)[0])
}
// output is auto-flushed
{
log("log-line-3")
assert.Regexp(t, `^.+\slog-line-2$`, readTail(t, logFile, 1)[0])
time.Sleep(300 * time.Millisecond)
assert.Regexp(t, `^.+\slog-line-3$`, readTail(t, logFile, 1)[0])
}
// output is formatted as expected
{
lines := readTail(t, logFile, 10)
require.Len(t, lines, 3)
for i := 0; i < 3; i++ {
assert.Regexp(t, fmt.Sprintf(`^.+\slog-line-%d$`, i+1), lines[i])
}
}
}
func TestParseCommandOutput(t *testing.T) {
tests := []struct {
co string
all, log bool
}{
{co: "", all: false, log: false},
{co: "auto", all: term.OsStdoutIsTerminal(), log: true},
{co: "log", all: false, log: true},
{co: "console", all: false, log: false},
{co: "all", all: true, log: false},
{co: "all,log", all: true, log: true},
{co: "console,log", all: true, log: true},
{co: "log,console", all: true, log: true},
{co: "log,a", all: false, log: true},
{co: "console,a", all: false, log: false},
{co: " auto ", all: term.OsStdoutIsTerminal(), log: true},
{co: " all ", all: true, log: false},
{co: " log ", all: false, log: true},
{co: " console ", all: false, log: false},
{co: " console , log ", all: true, log: true},
{co: " log , console ", all: true, log: true},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
a, l := parseCommandOutput(test.co)
assert.Equal(t, test.all, a, "all")
assert.Equal(t, test.log, l, "log")
})
}
}
func TestCloseFileHandler(t *testing.T) {
logFile := filepath.Join(t.TempDir(), "file.log")
handler, writer, err := getFileHandler(logFile)
require.NoError(t, err)
assert.NotNil(t, handler)
assert.NotNil(t, writer)
assert.NoError(t, handler.LogEntry(clog.LogEntry{Level: clog.LevelInfo, Format: "log-line-1"}))
handler.Close()
assert.Error(t, handler.LogEntry(clog.LogEntry{Level: clog.LevelInfo, Format: "log-line-2"}))
}