| 11 | ) |
| 12 | |
| 13 | func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { |
| 14 | testCases := make([]*TestCase, 0) |
| 15 | |
| 16 | for _, iTestCase := range iTestCases { |
| 17 | if _, ok := iTestCase.(*TestCase); ok { |
| 18 | testcase, err := iTestCase.ToTestCase() |
| 19 | if err != nil { |
| 20 | log.Error().Err(err).Msg("failed to convert ITestCase interface to TestCase struct") |
| 21 | return nil, err |
| 22 | } |
| 23 | testCases = append(testCases, testcase) |
| 24 | continue |
| 25 | } |
| 26 | |
| 27 | // iTestCase should be a TestCasePath, file path or folder path |
| 28 | tcPath, ok := iTestCase.(*TestCasePath) |
| 29 | if !ok { |
| 30 | return nil, errors.New("invalid iTestCase type") |
| 31 | } |
| 32 | |
| 33 | casePath := tcPath.GetPath() |
| 34 | err := fs.WalkDir(os.DirFS(casePath), ".", func(path string, dir fs.DirEntry, e error) error { |
| 35 | if dir == nil { |
| 36 | // casePath is a file other than a dir |
| 37 | path = casePath |
| 38 | } else if dir.IsDir() && path != "." && strings.HasPrefix(path, ".") { |
| 39 | // skip hidden folders |
| 40 | return fs.SkipDir |
| 41 | } else { |
| 42 | // casePath is a dir |
| 43 | path = filepath.Join(casePath, path) |
| 44 | } |
| 45 | |
| 46 | // ignore non-testcase files |
| 47 | ext := filepath.Ext(path) |
| 48 | if ext != ".yml" && ext != ".yaml" && ext != ".json" { |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // filtered testcases |
| 53 | testCasePath := TestCasePath(path) |
| 54 | tc, err := testCasePath.ToTestCase() |
| 55 | if err != nil { |
| 56 | return nil |
| 57 | } |
| 58 | testCases = append(testCases, tc) |
| 59 | return nil |
| 60 | }) |
| 61 | if err != nil { |
| 62 | return nil, errors.Wrap(err, "read dir failed") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | log.Info().Int("count", len(testCases)).Msg("load testcases successfully") |
| 67 | return testCases, nil |
| 68 | } |