11package framework
22
33import (
4+ "k8s.io/utils/strings/slices"
45 "os"
56 "path/filepath"
67 "strings"
@@ -9,20 +10,19 @@ import (
910)
1011
1112const (
12- defaultTestFileGlobPattern = "*.test.yaml"
13+ testFileGlobPattern = "*.test.yaml"
1314)
1415
1516// Loader loads a test suite.
1617type Loader struct {
17- globPattern string
18- rootDir string
18+ rootDir string
19+ additionalTestDirs [] string
1920}
2021
2122// NewLoader returns a a loader and applies options to it.
2223func NewLoader (rootDir string , opts ... LoaderOpt ) * Loader {
2324 loader := Loader {
2425 rootDir : rootDir ,
25- globPattern : defaultTestFileGlobPattern ,
2626 }
2727
2828 for _ , opt := range opts {
@@ -34,10 +34,10 @@ func NewLoader(rootDir string, opts ...LoaderOpt) *Loader {
3434// LoaderOpts allows to set custom options.
3535type LoaderOpt func (loader * Loader )
3636
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 {
3939 return func (loader * Loader ) {
40- loader .globPattern = pattern
40+ loader .additionalTestDirs = append ( loader . additionalTestDirs , path ... )
4141 }
4242}
4343
@@ -52,10 +52,9 @@ func (loader *Loader) LoadSuite() (*Test, error) {
5252 suite .Name = strings .TrimRight (loader .rootDir , "/" )
5353 }
5454
55- // Locate test files, if any.
56- testYAMLFiles , err := filepath .Glob (filepath .Join (loader .rootDir , loader .globPattern ))
55+ testYAMLFiles , err := loader .readTestYAMLFiles ()
5756 if err != nil {
58- return nil , errors . Wrap ( err , "globbing for .test.yaml files" )
57+ return nil , err
5958 }
6059
6160 for _ , file := range testYAMLFiles {
@@ -77,3 +76,27 @@ func (loader *Loader) LoadSuite() (*Test, error) {
7776
7877 return & suite , nil
7978}
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