| 1 |
package testutils |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
|
| 7 |
"github.com/juju/errors" |
| 8 |
) |
| 9 |
|
| 10 |
// GetTestCaseDirs scans the dir recursively and returns relative paths |
| 11 |
// to all the dirs which contain the file "test_case.yaml". For example: |
| 12 |
// |
| 13 |
// []string{"mytest1_foo", "some_group/mytest1", "some_group/mytest2"} |
| 14 |
func GetTestCaseDirs(testCasesDir string, testDescrFname string) ([]string, error) { |
| 15 |
var result []string |
| 16 |
|
| 17 |
// Walk the directory recursively |
| 18 |
err := filepath.Walk(testCasesDir, func(path string, info os.FileInfo, err error) error { |
| 19 |
// If there's an error walking, wrap and return it |
| 20 |
if err != nil { |
| 21 |
return errors.Annotate(err, "failed to walk path: "+path) |
| 22 |
} |
| 23 |
|
| 24 |
// If it's a directory and contains testDescrFname |
| 25 |
if info.IsDir() { |
| 26 |
// Check if testDescrFname exists in the current directory |
| 27 |
testCaseFile := filepath.Join(path, testDescrFname) |
| 28 |
if _, err := os.Stat(testCaseFile); err == nil { |
| 29 |
// If the file exists, add the relative path to the result |
| 30 |
relPath, err := filepath.Rel(testCasesDir, path) |
| 31 |
if err != nil { |
| 32 |
return errors.Annotate(err, "failed to get relative path for: "+path) |
| 33 |
} |
| 34 |
// Add the relative path to the result |
| 35 |
result = append(result, relPath) |
| 36 |
} |
| 37 |
} |
| 38 |
return nil |
| 39 |
}) |
| 40 |
|
| 41 |
if err != nil { |
| 42 |
return nil, errors.Annotate(err, "failed to scan directories") |
| 43 |
} |
| 44 |
|
| 45 |
return result, nil |
| 46 |
} |