-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlog_test.go
More file actions
64 lines (57 loc) · 1.37 KB
/
log_test.go
File metadata and controls
64 lines (57 loc) · 1.37 KB
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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestCloseLogFileWithNilHandle(t *testing.T) {
oldLogFile := logFile
defer func() {
logFile = oldLogFile
}()
logFile = nil
if !CloseLogFile() {
t.Fatal("CloseLogFile should succeed for nil handle")
}
}
func TestLoadLogFirstOpenAndReopen(t *testing.T) {
oldConfig := *config
oldLogFile := logFile
oldTodayStr := todayStr
oldLastLogPath := lastLogPath
defer func() {
CloseLogFile()
tmpConf := oldConfig
config = &tmpConf
logFile = oldLogFile
todayStr = oldTodayStr
lastLogPath = oldLastLogPath
}()
tmpConf := oldConfig
config = &tmpConf
config.LogToFile = true
config.LogPath = t.TempDir()
logFile = nil
todayStr = ""
lastLogPath = ""
if !LoadLog() {
t.Fatal("LoadLog should succeed on first open")
}
if logFile == nil {
t.Fatal("LoadLog should populate logFile")
}
nextLogPath := filepath.Join(config.LogPath, "next")
if err := os.MkdirAll(nextLogPath, os.ModePerm); err != nil {
t.Fatalf("failed to prepare second log dir: %v", err)
}
config.LogPath = nextLogPath
if !LoadLog() {
t.Fatal("LoadLog should succeed on reopen")
}
if logFile == nil {
t.Fatal("LoadLog should keep logFile populated after reopen")
}
if got := filepath.Dir(logFile.Name()); got != nextLogPath {
t.Fatalf("log file should be reopened under the new directory: got %q want %q", got, nextLogPath)
}
}