mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-08 22:13:13 +08:00
refactor: update summary in controller
This commit is contained in:
69
hrp/loader.go
Normal file
69
hrp/loader.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
|
||||
testCases := make([]*TestCase, 0)
|
||||
|
||||
for _, iTestCase := range iTestCases {
|
||||
if _, ok := iTestCase.(*TestCase); ok {
|
||||
testcase, err := iTestCase.ToTestCase()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to convert ITestCase interface to TestCase struct")
|
||||
return nil, err
|
||||
}
|
||||
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 := tcPath.GetPath()
|
||||
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.ToTestCase()
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user