-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_root_test.go
82 lines (73 loc) · 1.85 KB
/
detect_root_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
package main
import (
"io/fs"
"testing"
"github.com/stretchr/testify/assert"
)
func assertDirHasEntries(t *testing.T, fsys fs.FS, names []string) {
assert.NotNil(t, fsys, "Filesystem is nil")
found := map[string]bool{}
for _, name := range names {
found[name] = false
}
count := 0
entries, err := fs.ReadDir(fsys, ".")
assert.Nil(t, err)
for _, entry := range entries {
entryName := entry.Name()
if seen, ok := found[entryName]; ok {
if !seen {
found[entryName] = true
count++
}
} else {
t.Errorf("Unknown entry %s", entryName)
}
}
if count != len(names) {
t.Errorf("Not all names were found in the directory: %v", found)
}
}
func assertDetectedRootWillHaveEntries(t *testing.T, zipEntries []entry, expected []string) {
fses := createBothTestZipReaderFS(zipEntries)
for _, fs := range fses {
t.Run(fs.Name(), func(t *testing.T) {
root, err := detectRoot(fs)
assert.Nil(t, err)
assertDirHasEntries(t, root, expected)
})
}
}
func TestDetectRootInNormalCase(t *testing.T) {
entries := []entry{
{"www.website.com/abc/1.html", ""},
{"www.website.com/abc/2.html", ""},
{"www.website.com/def/1.html", ""},
{"www.website.com/def/2.html", ""},
{"www.website.com/index.html", ""},
}
assertDetectedRootWillHaveEntries(t, entries,
[]string{"abc", "def", "index.html"})
}
func TestDetectRootInBaseCase(t *testing.T) {
entries := []entry{
{"abc/1.html", ""},
{"def/1.html", ""},
{"def/2.html", ""},
{"index.html", ""},
{"abc/2.html", ""},
}
assertDetectedRootWillHaveEntries(t, entries,
[]string{"abc", "def", "index.html"})
}
func TestDetectRootWithExcludedFiles(t *testing.T) {
entries := []entry{
{"def/1.html", ""},
{"def/index.html", ""},
{"wget.log", ""},
{"nohup.out", ""},
}
assertDetectedRootWillHaveEntries(t, entries,
[]string{"1.html", "index.html"})
}
// TODO: Test more edge cases