Skip to content

Commit

Permalink
feat: split files for test cases #289
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Oct 19, 2024
1 parent e992b33 commit 9b57074
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions internal/cmd/integrations/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"internal/cmd/utils"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -141,14 +142,7 @@ var ScaffoldCmd = &cobra.Command{
if err = generateFolder(path.Join(folder, "testcases")); err != nil {
return err
}
testCasesBody, err = apiclient.PrettifyJson(testCasesBody)
if err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
path.Join(folder, "testcases", "testcases.json"),
false,
testCasesBody); err != nil {
if err = generateTestcases(testCasesBody, folder); err != nil {
return err
}
}
Expand Down Expand Up @@ -402,3 +396,39 @@ func getName(authConfigResp []byte) string {
_ = json.Unmarshal(authConfigResp, &m)
return m["displayName"]
}

func generateTestcases(testcases []byte, folder string) error {
var data []map[string]interface{}
err := json.Unmarshal(testcases, &data)
if err != nil {
return fmt.Errorf("Error decoding JSON: %s", err)
}
for _, obj := range data {
jsonData, err := json.Marshal(obj)
if err != nil {
return fmt.Errorf("Error encoding JSON: %s", err)
}
name, err := getTestCaseName(obj)
if err != nil {
return fmt.Errorf("unable to get name: %v", err)
}
jsonData, err = apiclient.PrettifyJson(jsonData)
if err != nil {
return err
}
if err = apiclient.WriteByteArrayToFile(
path.Join(folder, "testcases", name+".json"),
false,
jsonData); err != nil {
return err
}
}
return nil
}

func getTestCaseName(jsonData map[string]interface{}) (string, error) {
if name, ok := jsonData["name"].(string); ok && name != "" {
return filepath.Base(name), nil
}
return "", fmt.Errorf("name not found")
}

0 comments on commit 9b57074

Please sign in to comment.