Skip to content

Commit

Permalink
feat(worker): worker junit command (#4833)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and richardlt committed Dec 18, 2019
1 parent ac5272e commit 071520c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
74 changes: 74 additions & 0 deletions engine/worker/cmd_junit_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"encoding/xml"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/ovh/venom"
"github.com/spf13/cobra"

"github.com/ovh/cds/sdk"
)

func cmdJunitParser(w *currentWorker) *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(w),
}
return c
}

func junitParserCmd(w *currentWorker) 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 venom.Tests
for _, f := range filepaths {
var ftests venom.Tests
data, err := ioutil.ReadFile(f)
if err != nil {
return fmt.Errorf("junit parser: cannot read file %s (%s)", f, err)
}
var vf venom.Tests
if err := xml.Unmarshal(data, &vf); err != nil {
// Check if file contains testsuite only (and no testsuites)
if s, ok := parseTestsuiteAlone(data); ok {
ftests.TestSuites = append(ftests.TestSuites, s)
}
tests.TestSuites = append(tests.TestSuites, ftests.TestSuites...)
} else {
tests.TestSuites = append(tests.TestSuites, vf.TestSuites...)
}
}

var res sdk.Result
_ = computeStats(&res, &tests)

fmt.Println(tests.Total, tests.TotalOK, tests.TotalKO, tests.TotalSkipped)

return nil
}
}
1 change: 1 addition & 0 deletions engine/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
cmd.AddCommand(cmdRegister(w))
cmd.AddCommand(cmdCache(w))
cmd.AddCommand(cmdKey(w))
cmd.AddCommand(cmdJunitParser(w))

// last command: doc, this command is hidden
cmd.AddCommand(cmdDoc(cmd))
Expand Down

0 comments on commit 071520c

Please sign in to comment.