forked from TrueCloudLab/s3-tests-parser
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package parser
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/csv"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func ParseSuite(filePath string, format string) (map[string]bool, error) {
|
|
switch format {
|
|
case "csv":
|
|
return parseSuiteCSV(filePath)
|
|
case "json":
|
|
return parseSuiteJSON(filePath)
|
|
default:
|
|
return nil, fmt.Errorf("unknown format: %s", format)
|
|
}
|
|
}
|
|
|
|
func parseSuiteCSV(filePath string) (map[string]bool, error) {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read suite file: %w", err)
|
|
}
|
|
|
|
rr := csv.NewReader(bytes.NewReader(data))
|
|
records, err := rr.ReadAll()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse suite file: %w", err)
|
|
}
|
|
|
|
indexName := -1
|
|
indexStatus := -1
|
|
tests := make(map[string]bool)
|
|
|
|
for i, recs := range records {
|
|
if i == 0 {
|
|
for j, rec := range recs {
|
|
if rec == "Name" {
|
|
indexName = j
|
|
} else if rec == "Status" {
|
|
indexStatus = j
|
|
}
|
|
}
|
|
|
|
if indexName == -1 || indexStatus == -1 {
|
|
return nil, fmt.Errorf("invalid csv format, couldn't find 'Name' and 'Status' fields")
|
|
}
|
|
}
|
|
|
|
tests[recs[indexName]] = recs[indexStatus] == "passed"
|
|
}
|
|
|
|
return tests, nil
|
|
}
|
|
|
|
type suiteNode struct {
|
|
Name string `json:"name"`
|
|
Status string `json:"status"`
|
|
Children []suiteNode `json:"children"`
|
|
}
|
|
|
|
func parseSuiteJSON(filePath string) (map[string]bool, error) {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read suite file: %w", err)
|
|
}
|
|
|
|
var suiteNode suiteNode
|
|
if err = json.Unmarshal(data, &suiteNode); err != nil {
|
|
return nil, fmt.Errorf("failed to parse suite file: %w", err)
|
|
}
|
|
|
|
tests := make(map[string]bool)
|
|
parseSuiteNode(suiteNode, tests)
|
|
|
|
return tests, nil
|
|
}
|
|
|
|
func parseSuiteNode(node suiteNode, res map[string]bool) {
|
|
if node.Status != "" {
|
|
res[node.Name] = node.Status == "passed"
|
|
return
|
|
}
|
|
|
|
for _, child := range node.Children {
|
|
parseSuiteNode(child, res)
|
|
}
|
|
}
|