Skip to content

Commit

Permalink
Refactoring LoadManifest* functions to return PathSpec instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 17, 2024
1 parent a01c29a commit 6e37bb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
29 changes: 10 additions & 19 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 20 additions & 20 deletions pkg/lib/template/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

0 comments on commit 6e37bb3

Please sign in to comment.