...
1 package reporters
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path"
8
9 "github.com/onsi/ginkgo/v2/types"
10 )
11
12
13 func GenerateJSONReport(report types.Report, destination string) error {
14 if err := os.MkdirAll(path.Dir(destination), 0770); err != nil {
15 return err
16 }
17 f, err := os.Create(destination)
18 if err != nil {
19 return err
20 }
21 defer f.Close()
22 enc := json.NewEncoder(f)
23 enc.SetIndent("", " ")
24 err = enc.Encode([]types.Report{
25 report,
26 })
27 if err != nil {
28 return err
29 }
30 return nil
31 }
32
33
34
35 func MergeAndCleanupJSONReports(sources []string, destination string) ([]string, error) {
36 messages := []string{}
37 allReports := []types.Report{}
38 for _, source := range sources {
39 reports := []types.Report{}
40 data, err := os.ReadFile(source)
41 if err != nil {
42 messages = append(messages, fmt.Sprintf("Could not open %s:\n%s", source, err.Error()))
43 continue
44 }
45 err = json.Unmarshal(data, &reports)
46 if err != nil {
47 messages = append(messages, fmt.Sprintf("Could not decode %s:\n%s", source, err.Error()))
48 continue
49 }
50 os.Remove(source)
51 allReports = append(allReports, reports...)
52 }
53
54 if err := os.MkdirAll(path.Dir(destination), 0770); err != nil {
55 return messages, err
56 }
57 f, err := os.Create(destination)
58 if err != nil {
59 return messages, err
60 }
61 defer f.Close()
62 enc := json.NewEncoder(f)
63 enc.SetIndent("", " ")
64 err = enc.Encode(allReports)
65 if err != nil {
66 return messages, err
67 }
68 return messages, nil
69 }
70
View as plain text