package hrp import ( "io/fs" "os" "path/filepath" "strings" "github.com/pkg/errors" "github.com/rs/zerolog/log" ) // LoadTestCases load testcases from TestCasePath or TestCase func LoadTestCases(tests ...ITestCase) ([]*TestCase, error) { testCases := make([]*TestCase, 0) for _, iTestCase := range tests { if testcase, ok := iTestCase.(*TestCase); ok { testCases = append(testCases, testcase) continue } // iTestCase should be a TestCasePath, file path or folder path tcPath, ok := iTestCase.(*TestCasePath) if !ok { return nil, errors.New("invalid iTestCase type") } casePath := string(*tcPath) err := fs.WalkDir(os.DirFS(casePath), ".", func(path string, dir fs.DirEntry, e error) error { if dir == nil { // casePath is a file other than a dir path = casePath } else if dir.IsDir() && path != "." && strings.HasPrefix(path, ".") { // skip hidden folders return fs.SkipDir } else { // casePath is a dir path = filepath.Join(casePath, path) } // ignore non-testcase files ext := filepath.Ext(path) if ext != ".yml" && ext != ".yaml" && ext != ".json" { return nil } // filtered testcases testCasePath := TestCasePath(path) tc, err := testCasePath.GetTestCase() if err != nil { log.Warn().Err(err).Str("path", path).Msg("load testcase failed") return nil } testCases = append(testCases, tc) return nil }) if err != nil { return nil, errors.Wrap(err, "read dir failed") } } log.Info().Int("count", len(testCases)).Msg("load testcases successfully") return testCases, nil }