diff --git a/api_testsuite.go b/api_testsuite.go index 28e6b93..5be3eb7 100644 --- a/api_testsuite.go +++ b/api_testsuite.go @@ -250,16 +250,18 @@ func (ats *Suite) parseAndRunTest( v any, testFilePath string, r *report.ReportElement, rootLoader template.Loader, allowParallelExec bool, ) bool { - // Parse PathSpec (if any) and determine number of parallel runs parallelRuns := 1 - if vStr, ok := v.(string); ok { - pathSpec, err := util.ParsePathSpec(vStr) - if err != nil { - logrus.Error(fmt.Errorf("test string is not a valid path spec: %w", err)) - return false - } - parallelRuns = pathSpec.ParallelRuns + // Get the Manifest with @ logic + referencedPathSpec, testRaw, err := template.LoadManifestDataAsRawJson(v, filepath.Dir(testFilePath)) + if err != nil { + r.SaveToReportLog(err.Error()) + logrus.Error(fmt.Errorf("can not LoadManifestDataAsRawJson (%s): %s", testFilePath, err)) + return false + } + if referencedPathSpec != nil { + testFilePath = filepath.Join(filepath.Dir(testFilePath), referencedPathSpec.Path) + parallelRuns = referencedPathSpec.ParallelRuns } // If parallel runs are requested, check that they're actually allowed @@ -268,17 +270,6 @@ func (ats *Suite) parseAndRunTest( return false } - // Get the Manifest with @ logic - referencedFilePath, testRaw, err := template.LoadManifestDataAsRawJson(v, filepath.Dir(testFilePath)) - if referencedFilePath != "" { - testFilePath = filepath.Join(filepath.Dir(testFilePath), referencedFilePath) - } - if err != nil { - r.SaveToReportLog(err.Error()) - logrus.Error(fmt.Errorf("can not LoadManifestDataAsRawJson (%s): %s", testFilePath, err)) - return false - } - // Execute test cases var successCount atomic.Uint32 var waitGroup sync.WaitGroup diff --git a/pkg/lib/template/util.go b/pkg/lib/template/util.go index 8eb7956..f899d47 100644 --- a/pkg/lib/template/util.go +++ b/pkg/lib/template/util.go @@ -7,22 +7,22 @@ import ( "github.com/programmfabrik/apitest/pkg/lib/util" ) -func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (filepath string, res any, err error) { +func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (pathSpec *util.PathSpec, res any, err error) { switch typedData := data.(type) { case string: - pathSpec, err := util.ParsePathSpec(typedData) + pathSpec, err = util.ParsePathSpec(typedData) if err != nil { - return "", res, fmt.Errorf("error parsing pathSpec: %w", err) + return nil, res, fmt.Errorf("error parsing pathSpec: %w", err) } requestTmpl, err := pathSpec.LoadContents(manifestDir) if err != nil { - return "", res, fmt.Errorf("error loading fileFromPathSpec: %s", err) + return nil, res, fmt.Errorf("error loading fileFromPathSpec: %s", err) } // We have json, and load it thereby into our apitest structure requestBytes, err := loader.Render(requestTmpl, manifestDir, nil) if err != nil { - return "", res, fmt.Errorf("error rendering request: %s", err) + return nil, res, fmt.Errorf("error rendering request: %s", err) } var jsonObject util.JsonObject @@ -31,45 +31,45 @@ func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (file if err = util.Unmarshal(requestBytes, &jsonObject); err != nil { if err = util.Unmarshal(requestBytes, &jsonArray); err == nil { - return filepath, jsonArray, nil + return pathSpec, jsonArray, nil } - return "", res, fmt.Errorf("error unmarshalling: %s", err) + return nil, res, fmt.Errorf("error unmarshalling: %s", err) } - return pathSpec.Path, jsonObject, nil + return pathSpec, jsonObject, nil case util.JsonObject: - return "", typedData, nil + return nil, typedData, nil case util.JsonArray: - return "", typedData, nil + return nil, typedData, nil default: - return "", res, fmt.Errorf("specification needs to be string[@...] or jsonObject but is: %s", data) + return nil, res, fmt.Errorf("specification needs to be string[@...] or jsonObject but is: %s", data) } } -func LoadManifestDataAsRawJson(data any, manifestDir string) (filepath string, res json.RawMessage, err error) { +func LoadManifestDataAsRawJson(data any, manifestDir string) (pathSpec *util.PathSpec, res json.RawMessage, err error) { switch typedData := data.(type) { case []byte: err = res.UnmarshalJSON(typedData) return case string: - pathSpec, err := util.ParsePathSpec(typedData) + pathSpec, err = util.ParsePathSpec(typedData) if err != nil { - return "", res, fmt.Errorf("error parsing pathSpec: %w", err) + return nil, res, fmt.Errorf("error parsing pathSpec: %w", err) } res, err := pathSpec.LoadContents(manifestDir) if err != nil { - return "", res, fmt.Errorf("error loading fileFromPathSpec: %s", err) + return nil, res, fmt.Errorf("error loading fileFromPathSpec: %s", err) } - return pathSpec.Path, res, nil + return pathSpec, res, nil case util.JsonObject, util.JsonArray: jsonMar, err := json.Marshal(typedData) if err != nil { - return "", res, fmt.Errorf("error marshaling: %s", err) + return nil, res, fmt.Errorf("error marshaling: %s", err) } if err = util.Unmarshal(jsonMar, &res); err != nil { - return "", res, fmt.Errorf("error unmarshalling: %s", err) + return nil, res, fmt.Errorf("error unmarshalling: %s", err) } - return "", res, nil + return nil, res, nil default: - return "", res, fmt.Errorf("specification needs to be string[@...] or jsonObject but is: %s", data) + return nil, res, fmt.Errorf("specification needs to be string[@...] or jsonObject but is: %s", data) } }