1
1
package framework
2
2
3
3
import (
4
+ "k8s.io/utils/strings/slices"
4
5
"os"
5
6
"path/filepath"
6
7
"strings"
@@ -9,20 +10,19 @@ import (
9
10
)
10
11
11
12
const (
12
- defaultTestFileGlobPattern = "*.test.yaml"
13
+ testFileGlobPattern = "*.test.yaml"
13
14
)
14
15
15
16
// Loader loads a test suite.
16
17
type Loader struct {
17
- globPattern string
18
- rootDir string
18
+ rootDir string
19
+ additionalTestDirs [] string
19
20
}
20
21
21
22
// NewLoader returns a a loader and applies options to it.
22
23
func NewLoader (rootDir string , opts ... LoaderOpt ) * Loader {
23
24
loader := Loader {
24
25
rootDir : rootDir ,
25
- globPattern : defaultTestFileGlobPattern ,
26
26
}
27
27
28
28
for _ , opt := range opts {
@@ -34,10 +34,10 @@ func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
34
34
// LoaderOpts allows to set custom options.
35
35
type LoaderOpt func (loader * Loader )
36
36
37
- // WithCustomFilePattern sets a custom file pattern to load test files .
38
- func WithCustomFilePattern ( pattern string ) LoaderOpt {
37
+ // WithAdditionalTestDirs adds additional test source directories which are scanned for tests .
38
+ func WithAdditionalTestDirs ( path ... string ) LoaderOpt {
39
39
return func (loader * Loader ) {
40
- loader .globPattern = pattern
40
+ loader .additionalTestDirs = append ( loader . additionalTestDirs , path ... )
41
41
}
42
42
}
43
43
@@ -52,10 +52,9 @@ func (loader *Loader) LoadSuite() (*Test, error) {
52
52
suite .Name = strings .TrimRight (loader .rootDir , "/" )
53
53
}
54
54
55
- // Locate test files, if any.
56
- testYAMLFiles , err := filepath .Glob (filepath .Join (loader .rootDir , loader .globPattern ))
55
+ testYAMLFiles , err := loader .readTestYAMLFiles ()
57
56
if err != nil {
58
- return nil , errors . Wrap ( err , "globbing for .test.yaml files" )
57
+ return nil , err
59
58
}
60
59
61
60
for _ , file := range testYAMLFiles {
@@ -77,3 +76,27 @@ func (loader *Loader) LoadSuite() (*Test, error) {
77
76
78
77
return & suite , nil
79
78
}
79
+
80
+ // readTestYAMLFiles locates test files, if any.
81
+ func (loader * Loader ) readTestYAMLFiles () ([]string , error ) {
82
+ var testYAMLFiles []string
83
+ var scannedDirs []string
84
+
85
+ dirs := append (loader .additionalTestDirs , loader .rootDir )
86
+ for _ , dir := range dirs {
87
+ if slices .Contains (scannedDirs , dir ) {
88
+ continue
89
+ }
90
+
91
+ dirGlob := filepath .Join (dir , testFileGlobPattern )
92
+
93
+ yamlFiles , err := filepath .Glob (dirGlob )
94
+ if err != nil {
95
+ return nil , errors .Wrapf (err , "resolving files using wildcard pattern %s" , dirGlob )
96
+ }
97
+ testYAMLFiles = append (testYAMLFiles , yamlFiles ... )
98
+ scannedDirs = append (scannedDirs , dir )
99
+ }
100
+
101
+ return testYAMLFiles , nil
102
+ }
0 commit comments