refactor: move ReadFile to hrp/loader

This commit is contained in:
lilong.129
2024-08-19 22:54:27 +08:00
parent 98ce2cc818
commit 9efbd04055
3 changed files with 20 additions and 23 deletions

View File

@@ -11,7 +11,6 @@ import (
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/json"
)
@@ -73,7 +72,7 @@ func LoadTestCases(tests ...ITestCase) ([]*TestCase, error) {
// LoadFileObject loads file content with file extension and assigns to structObj
func LoadFileObject(path string, structObj interface{}) (err error) {
log.Info().Str("path", path).Msg("load file")
file, err := builtin.ReadFile(path)
file, err := readFile(path)
if err != nil {
return errors.Wrap(err, "read file failed")
}
@@ -133,3 +132,19 @@ func parseEnvContent(file []byte, obj interface{}) error {
}
return nil
}
func readFile(path string) ([]byte, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("convert absolute path failed")
return nil, errors.Wrap(code.LoadFileError, err.Error())
}
file, err := os.ReadFile(path)
if err != nil {
log.Error().Err(err).Msg("read file failed")
return nil, errors.Wrap(code.LoadFileError, err.Error())
}
return file, nil
}