-
Notifications
You must be signed in to change notification settings - Fork 452
/
list.go
190 lines (171 loc) · 5.46 KB
/
list.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package bptest
import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/discovery"
testing "github.com/mitchellh/go-testing-interface"
)
const (
discoverTestFilename = "discover_test.go"
)
type bpTest struct {
name string
config string
location string
bptestCfg discovery.BlueprintTestConfig
}
// getTests returns slice of all blueprint tests
func getTests(intTestDir string) ([]bpTest, error) {
intTestDir, err := getIntTestDir(intTestDir)
if err != nil {
return nil, err
}
Log.Info(fmt.Sprintf("using test-dir: %s", intTestDir))
tests := []bpTest{}
discoveredTests, err := getDiscoveredTests(intTestDir)
if err != nil {
return nil, err
}
tests = append(tests, discoveredTests...)
explicitTests, err := getExplicitTests(intTestDir)
if err != nil {
return nil, err
}
tests = append(tests, explicitTests...)
return tests, nil
}
// getDiscoveredTests returns slice of discovered blueprint tests
func getDiscoveredTests(intTestDir string) ([]bpTest, error) {
discoverTestFile := path.Join(intTestDir, discoverTestFilename)
// skip discovering tests if no discoverTestFile
_, err := os.Stat(discoverTestFile)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
Log.Warn(fmt.Sprintf("Skipping discovered test. %s not found.", discoverTestFilename))
return nil, nil
}
// if discoverTestFile is present, find auto discovered tests
tests := []bpTest{}
if discoverTestFile != "" {
discoverTestName, err := getDiscoverTestName(discoverTestFile)
if err != nil {
return nil, err
}
discoveredSubTests := discovery.FindTestConfigs(&testing.RuntimeT{}, intTestDir)
for testName, testCfg := range discoveredSubTests {
bptestCfg, err := discovery.GetTestConfig(path.Join(testCfg, discovery.DefaultTestConfigFilename))
if err != nil {
Log.Warn(fmt.Sprintf("error discovering BlueprintTest config: %v", err))
}
tests = append(tests, bpTest{name: fmt.Sprintf("%s/%s", discoverTestName, testName), config: testCfg, location: discoverTestFile, bptestCfg: bptestCfg})
}
}
sort.SliceStable(tests, func(i, j int) bool { return tests[i].name < tests[j].name })
return tests, nil
}
func getExplicitTests(intTestDir string) ([]bpTest, error) {
// find all explicit test files ending with *_test.go excluding discover_test.go within intTestDir
testFiles := findFiles(intTestDir,
func(d fs.DirEntry) bool {
return strings.HasSuffix(d.Name(), "_test.go") && d.Name() != discoverTestFilename
},
)
eTests := []bpTest{}
for _, testFile := range testFiles {
// testDir name maps to a matching example/fixture
testDir := path.Dir(testFile)
testCfg, err := discovery.GetConfigDirFromTestDir(testDir)
if err != nil {
Log.Warn(fmt.Sprintf("unable to discover configs for %s: %v", testDir, err))
}
// discover BlueprintTest config if any
bptestCfg, err := discovery.GetTestConfig(path.Join(testCfg, discovery.DefaultTestConfigFilename))
if err != nil {
Log.Warn(fmt.Sprintf("error discovering BlueprintTest config: %v", err))
}
testFns, err := getTestFuncsFromFile(testFile)
if err != nil {
return nil, err
}
for _, fnName := range testFns {
eTests = append(eTests, bpTest{name: fnName, location: testFile, config: testCfg, bptestCfg: bptestCfg})
}
}
sort.SliceStable(eTests, func(i, j int) bool { return eTests[i].name < eTests[j].name })
return eTests, nil
}
// getDiscoverTestName returns test name used for auto discovered tests
func getDiscoverTestName(dFileName string) (string, error) {
fn, err := getTestFuncsFromFile(dFileName)
if err != nil {
return "", err
}
// enforce only one main test func decl for discovered tests
if len(fn) != 1 {
return "", fmt.Errorf("only one function should be defined in %s. Found %+q", dFileName, fn)
}
return fn[0], nil
}
// discoverIntTestDir attempts to discover the integration test directory
// by searching for discover_test.go in the current working directory.
// If not found, it returns current working directory.
func discoverIntTestDir(cwd string) (string, error) {
// search for discover_test.go
discoverTestFiles := findFiles(cwd,
func(d fs.DirEntry) bool {
return d.Name() == discoverTestFilename
},
)
if len(discoverTestFiles) > 1 {
return "", fmt.Errorf("found multiple %s files: %+q. Exactly one file was expected", discoverTestFilename, discoverTestFiles)
}
if len(discoverTestFiles) == 1 {
relIntTestDir, err := filepath.Rel(cwd, path.Dir(discoverTestFiles[0]))
if err != nil {
return "", err
}
return relIntTestDir, nil
}
// no discover_test.go file discovered
return ".", nil
}
// getIntTestDir discovers the integration test directory
// from current working directory if an empty intTestDir is provided
func getIntTestDir(intTestDir string) (string, error) {
if intTestDir != "" {
return intTestDir, nil
}
// discover from current working directory
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return discoverIntTestDir(cwd)
}
// findFiles returns a slice of file paths matching matchFn
func findFiles(dir string, matchFn func(d fs.DirEntry) bool) []string {
files := []string{}
filepath.WalkDir(dir, func(fpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// ignore hidden dirs
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
if !d.IsDir() && matchFn(d) {
files = append(files, fpath)
return nil
}
return nil
})
return files
}