-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk_test.go
105 lines (95 loc) · 3.4 KB
/
walk_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
// Copyright © 2018 Chad Netzer <chad.netzer@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package hardlinkable
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
P "github.com/chadnetzer/hardlinkable/internal/pathpool"
)
type testIncludesExcludes struct {
in []string
ex []string
match string
n int
}
func TestWalkFileIncludes(t *testing.T) {
topdir, err := ioutil.TempDir("", "hardlinkable")
if err != nil {
t.Fatalf("Couldn't create temp dir for dir excludes tests: %v", err)
}
defer os.RemoveAll(topdir)
if os.Chdir(topdir) != nil {
t.Fatalf("Couldn't chdir to temp dir for dir excludes tests")
}
filenameMap := map[string]struct{}{
"f1": struct{}{},
"*f2": struct{}{},
"f3.*.txt": struct{}{},
"f4.*.raw": struct{}{},
"f5": struct{}{},
}
for pattern := range filenameMap {
f, err := ioutil.TempFile(topdir, pattern)
if err != nil {
t.Fatalf("Couldn't create tempfile with pattern: '%v' in dir: '%v'", pattern, topdir)
}
defer os.Remove(f.Name())
}
dirs := []string{topdir}
incExSlice := []testIncludesExcludes{
testIncludesExcludes{[]string{}, []string{}, "", len(filenameMap)},
testIncludesExcludes{[]string{"f1"}, []string{}, "f1", 1},
testIncludesExcludes{[]string{"f1"}, []string{"f1"}, "", len(filenameMap)},
testIncludesExcludes{[]string{"f1"}, []string{"f1", "f2"}, "", len(filenameMap) - 1},
testIncludesExcludes{[]string{"f1", "f2"}, []string{}, "f1", 2},
testIncludesExcludes{[]string{"f1", "f2"}, []string{}, "f2", 2},
testIncludesExcludes{[]string{"f1", "f2", "f3", "f4", "f5"}, []string{}, "f5", 5},
testIncludesExcludes{[]string{"f1", "f2", "f3", "f4", "f5", "f6"}, []string{}, "f5", 5},
}
s := status{}
s.Options = &Options{}
s.Results = newResults(s.Options)
s.pool = P.NewPool()
for _, v := range incExSlice {
s.Options.FileIncludes = v.in
s.Options.FileExcludes = v.ex
c := matchedPathnames(*s.Options, s.Results, s.pool, dirs, []string{})
n := 0
var filenames []string
foundMatch := false
for pe := range c {
n++
_, filename := path.Split(pe.pathname)
filenames = append(filenames, filename)
if strings.Contains(filename, v.match) {
foundMatch = true
}
}
if !foundMatch {
t.Errorf("Included pattern '%v' not found in filenames: %v", v.match, filenames)
}
if n != v.n {
t.Errorf("Expected %v files for '%v' included match, got: %v", v.n, v.match, n)
}
}
}