forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_junit_parser.go
More file actions
71 lines (60 loc) · 1.65 KB
/
cmd_junit_parser.go
File metadata and controls
71 lines (60 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/ovh/cds/engine/worker/internal/action"
"github.com/ovh/cds/sdk"
)
func cmdJunitParser() *cobra.Command {
c := &cobra.Command{
Use: "junit-parser",
Short: "worker junit-parser",
Long: `
worker junit-parser command helps you to parse junit files and print a summary.
It displays the number of tests, the number of passed tests, the number of failed tests and the number of skipped tests.
Examples:
$ ls
result1.xml result2.xml
$ worker junit-parser result1.xml
10 10 0 0
$ worker junit-parser *.xml
20 20 0 0
`,
RunE: junitParserCmd(),
}
return c
}
func junitParserCmd() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var filepaths []string
for _, arg := range args {
matches, err := filepath.Glob(arg)
if err != nil {
return err
}
filepaths = append(filepaths, matches...)
}
var tests sdk.JUnitTestsSuites
for _, f := range filepaths {
data, err := os.ReadFile(f)
if err != nil {
return fmt.Errorf("junit parser: cannot read file %s (%s)", f, err)
}
var ftests sdk.JUnitTestsSuites
if err := xml.Unmarshal(data, &ftests); err != nil {
// Check if file contains testsuite only (and no testsuites)
if s, ok := action.ParseTestsuiteAlone(data); ok {
ftests.TestSuites = append(ftests.TestSuites, s)
}
}
tests.TestSuites = append(tests.TestSuites, ftests.TestSuites...)
}
tests = tests.EnsureData()
stats := tests.ComputeStats()
fmt.Println(stats.Total, stats.TotalOK, stats.TotalKO, stats.TotalSkipped)
return nil
}
}