-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal_test.go
More file actions
117 lines (102 loc) · 2.73 KB
/
local_test.go
File metadata and controls
117 lines (102 loc) · 2.73 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
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
package main
import (
"bytes"
"log"
"testing"
)
func TestLoadAllLocalFiles(t *testing.T) {
logger, buf := getTestLogger()
var exclude StringSlice
fileChan := loadLocalFiles("./_testdata", exclude, logger)
files := sink(fileChan)
expected := 19
actual := len(files)
if actual != expected {
t.Errorf("wanted %d files, got %d files", expected, actual)
t.Errorf("%s\n", buf)
}
filename := "file_33.html"
file, ok := files[filename]
if !ok {
t.Errorf("Couldn't find file '%s' in file list", filename)
t.Errorf("%+v", files)
return
}
if file.Err != nil {
t.Errorf("Expected file.Err to be nil, got %v\n", file.Err)
}
if file.Name != filename {
t.Errorf("Expected file.Path to be %s, got %s\n", filename, file.Name)
}
if file.Size != 34 {
t.Errorf("Expected file.Path to be %d, got %d\n", 34, file.Size)
}
if file.Name != filename {
t.Errorf("expected file.path ('%s') to be the same as the key ('%s') of the map", file.Name, filename)
}
}
func BenchmarkLoadAllLocalFiles(b *testing.B) {
logger, _ := getTestLogger()
var exclude StringSlice
for i := 0; i < b.N; i++ {
loadLocalFiles("./_testdata", exclude, logger)
}
}
func TestLoadSingleFile(t *testing.T) {
logger, buf := getTestLogger()
fileChan := loadLocalFiles("./_testdata/file_33.html", StringSlice{}, logger)
files := sink(fileChan)
if len(files) != 1 {
t.Errorf("wanted %d files, got %d files", 1, len(files))
t.Errorf("%s\n", buf)
}
filename := "file_33.html"
_, ok := files[filename]
if !ok {
t.Errorf("Couldn't find file '%s' in file list", filename)
t.Errorf("%+v", files)
return
}
}
func TestLoadFiles(t *testing.T) {
tests := []struct {
in string
out int
exclude StringSlice
}{
{in: "./_testdata/dir_45", out: 13},
{in: "./_testdata/dir_45/", out: 13},
{in: "./_testdata/XXX_SDASD", out: 0},
{in: "./_testdata/file_33.html", out: 1},
{in: "./_testdata", out: 0, exclude: StringSlice{"*"}},
{in: "./_testdata", out: 11, exclude: StringSlice{"*.html"}},
{in: "./_testdata", out: 6, exclude: StringSlice{"*dir_45*"}},
}
for _, test := range tests {
logger, buf := getTestLogger()
fileChan := loadLocalFiles(test.in, test.exclude, logger)
files := sink(fileChan)
if len(files) != test.out {
t.Errorf("wanted %d files, got %d files", test.out, len(files))
t.Errorf("%s\n", buf)
}
}
}
func sink(in chan *FileStat) map[string]*FileStat {
out := make(map[string]*FileStat)
for f := range in {
if f.Err != nil {
return out
}
out[f.Name] = f
}
return out
}
func getTestLogger() (*Logger, *bytes.Buffer) {
buf := new(bytes.Buffer)
return &Logger{
Out: log.New(buf, "[Out] ", log.Lshortfile),
Err: log.New(buf, "[Err] ", log.Lshortfile),
Debug: log.New(buf, "[DEBUG] ", log.Lshortfile),
}, buf
}