-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtest_helpers.go
87 lines (69 loc) · 1.69 KB
/
test_helpers.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
package bfs
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/itchio/wharf/tlc"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type folderSpec struct {
entries []*entrySpec
}
type entrySpec struct {
name string
data []byte
}
func cleanAndMakeFolder(fs *folderSpec, dest string) error {
err := os.RemoveAll(dest)
if err != nil {
return errors.Wrap(err, "cleaning up test folder")
}
return makeFolder(fs, dest)
}
func makeFolder(fs *folderSpec, dest string) error {
err := os.MkdirAll(dest, 0755)
if err != nil {
return errors.Wrap(err, "creating test folder")
}
for _, e := range fs.entries {
entryPath := filepath.Join(dest, e.name)
entryDir := filepath.Dir(entryPath)
err = os.MkdirAll(entryDir, 0755)
if err != nil {
return errors.Wrap(err, "creating test folder directory entry")
}
err = ioutil.WriteFile(entryPath, e.data, os.FileMode(0644))
if err != nil {
return errors.Wrap(err, "writing test folder file entry")
}
}
return nil
}
func checkFolder(t *testing.T, fs *folderSpec, dest string) {
entryNames := make(map[string]bool)
for _, e := range fs.entries {
entryNames[filepath.ToSlash(e.name)] = true
entryPath := filepath.Join(dest, e.name)
data, err := ioutil.ReadFile(entryPath)
must(t, err)
assert.EqualValues(t, e.data, data)
}
// make sure all entries are accounted for
container, err := tlc.WalkDir(dest, &tlc.WalkOpts{
Filter: tlc.DefaultFilter,
})
must(t, err)
for _, f := range container.Files {
if _, ok := entryNames[f.Path]; !ok {
t.Errorf("extra file entry found: %s", f.Path)
}
}
}
func must(t *testing.T, err error) {
if err != nil {
assert.NoError(t, err)
t.FailNow()
}
}