-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers_test.go
85 lines (68 loc) · 1.7 KB
/
helpers_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
package gorotator
import (
"os"
"regexp"
"testing"
)
func Test_checkOrCreateDir(t *testing.T) {
const pathToDir = "test"
if err := checkOrCreateDir(pathToDir); err != nil {
t.Fatal(err)
}
f, err := os.Open(pathToDir)
if err != nil {
t.Fatal("Open err: ", err)
}
defer f.Close()
defer os.Remove(pathToDir)
stat, err := f.Stat()
if err != nil {
t.Fatal("Stat err: ", err)
}
if !stat.IsDir() {
t.Error("not a directory")
}
if stat.Name() != pathToDir {
t.Error("not a directory")
}
}
func Test_openNewFile(t *testing.T) {
cfg := Config{
FileName: "test.log",
PathToDir: "",
MaxFileSize: 0,
MaxNumberOfFiles: 0,
IsWindows: false,
}
f, err := openNewFile(cfg)
if err != nil {
t.Fatal("openNewFile err: ", err)
}
defer f.Close()
defer os.Remove(cfg.FileName)
stat, err := f.Stat()
if err != nil {
t.Fatal("Stat err: ", err)
}
if stat.IsDir() {
t.Error("should not be a directory")
}
if stat.Name() != cfg.FileName {
t.Error("not a directory")
}
}
func Test_newName(t *testing.T) {
const (
oldName = "testFile"
unixPattern = `^` + oldName + `\.[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\.[0-9]{8,9}\+[0-9]{1,2}:[0-9]{1,2}$`
windowsPattern = `^` + oldName + `_[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}_[0-9]{8,9}$`
)
newFileName := newName(oldName, false)
if !regexp.MustCompile(unixPattern).MatchString(newFileName) {
t.Error(newFileName, "doesn't match the pattern:\n", unixPattern)
}
newFileNameWin := newName(oldName, true)
if !regexp.MustCompile(windowsPattern).MatchString(newFileNameWin) {
t.Error(newFileNameWin, "doesn't match the pattern:\n", windowsPattern)
}
}