forked from ovh/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_files_test.go
130 lines (115 loc) · 2.71 KB
/
process_files_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
121
122
123
124
125
126
127
128
129
130
package venom
import (
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
func tempDir(t *testing.T) (string, error) {
dir := os.TempDir()
name := path.Join(dir, randomString(5))
if err := os.MkdirAll(name, os.FileMode(0744)); err != nil {
return "", err
}
t.Logf("Creating directory %s", name)
return name, nil
}
func randomString(n int) string {
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, n)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}
func Test_getFilesPath(t *testing.T) {
rand.Seed(time.Now().UnixNano())
log.SetLevel(log.DebugLevel)
type args struct {
exclude []string
}
tests := []struct {
init func(t *testing.T) ([]string, error)
name string
args args
want []string
wantErr bool
}{
{
name: "Check an empty directory",
init: func(t *testing.T) ([]string, error) {
dir, err := tempDir(t)
return []string{dir}, err
},
wantErr: true,
},
{
name: "Check an directory with one yaml file",
init: func(t *testing.T) ([]string, error) {
dir, err := tempDir(t)
if err != nil {
return nil, err
}
d1 := []byte("hello")
err = ioutil.WriteFile(path.Join(dir, "d1.yml"), d1, 0644)
return []string{dir}, err
},
want: []string{"d1.yml"},
wantErr: false,
},
{
name: "Check an directory with one yaml file and a subdirectory with another file",
init: func(t *testing.T) ([]string, error) {
dir1, err := tempDir(t)
if err != nil {
return nil, err
}
d1 := []byte("hello")
if err = ioutil.WriteFile(path.Join(dir1, "d1.yml"), d1, 0644); err != nil {
return nil, err
}
dir2 := path.Join(dir1, randomString(10))
t.Logf("Creating directory %s", dir2)
if err := os.Mkdir(dir2, 0744); err != nil {
return nil, err
}
d2 := []byte("hello")
if err = ioutil.WriteFile(path.Join(dir2, "d2.yml"), d2, 0644); err != nil {
return nil, err
}
return []string{dir1, dir2}, err
},
want: []string{"d1.yml", "d2.yml"},
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
path, err := tt.init(t)
if err != nil {
t.Fatal(err)
}
got, err := getFilesPath(path, tt.args.exclude)
if (err != nil) != tt.wantErr {
t.Errorf("getFilesPath() name:%s error = %v, wantErr %v", tt.name, err, tt.wantErr)
return
}
for _, f := range tt.want {
var found bool
for _, g := range got {
if strings.HasSuffix(g, f) {
found = true
}
}
if !found {
t.Errorf("getFilesPath() error want %v got %v", f, got)
}
}
})
}
}