-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk_test.go
120 lines (101 loc) · 3.24 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package hashlink
/*
Copyright 2019 Nicholas Krichevsky
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// closableStringReader serves as a wrapper for *strings.Reader to allow it to implement the io.ReadCloser interface
type closableStringReader struct {
closeCount int
*strings.Reader
}
// staticWalker is a mock walker for use with testing
type staticWalker struct {
// A map of file names to file contents
files map[string]string
// A map of file names to closableStringReaders
readers map[string]*closableStringReader
}
// Walk will simply return io.ReadClosers (within pathedData) of all of the files within the given root. Note that
// process must close the file once it is doneA.
func (walker staticWalker) Walk(root string, process func(reader pathedData) error) error {
// Ignore the root - it doesn't matter for our case here.
for filename, contents := range walker.files {
reader := &closableStringReader{Reader: strings.NewReader(contents)}
walker.readers[filename] = reader
err := process(pathedData{path: filename, data: reader})
if err != nil {
return err
}
}
return nil
}
// Close will simply nop. Implemented so strings.Reader can fulfill the ReadCloser interface.
func (r *closableStringReader) Close() error {
r.closeCount++
return nil
}
type walkTest struct {
name string
setup func() pathWalker
test func(t *testing.T, walker pathWalker)
}
func runWalkTestTable(t *testing.T, table []walkTest) {
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
walker := tt.setup()
tt.test(t, walker)
})
}
}
func TestGetAllItemsFromWalker(t *testing.T) {
tests := []walkTest{
{
name: "no files",
setup: func() pathWalker {
files := map[string]string{}
return staticWalker{files: files, readers: make(map[string]*closableStringReader, len(files))}
},
test: func(t *testing.T, walker pathWalker) {
result, err := getAllItemsFromWalker(walker, "/")
assert.Nil(t, err)
assert.NotNil(t, result)
assert.Equal(t, 0, len(result))
},
},
{
name: "bunch of files",
setup: func() pathWalker {
files := map[string]string{
"a/b": "hello world",
"a/bb/c": "my awesome file!",
"a/bb/d": "unit testing...",
"a/bb/e": "this is the last file I'm testing",
}
return staticWalker{files: files, readers: make(map[string]*closableStringReader, len(files))}
},
test: func(t *testing.T, walker pathWalker) {
result, err := getAllItemsFromWalker(walker, "/")
assert.Nil(t, err)
paths := []string{}
for _, data := range result {
paths = append(paths, data.path)
}
assert.ElementsMatch(t, []string{"a/b", "a/bb/c", "a/bb/d", "a/bb/e"}, paths)
},
},
}
runWalkTestTable(t, tests)
}