From 071520c49d3b23fd19ffd0760f71342d609a745a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Wed, 18 Dec 2019 10:33:16 +0100 Subject: [PATCH] feat(worker): worker junit command (#4833) --- engine/worker/cmd_junit_parser.go | 74 +++++++++++++++++++++++++++++++ engine/worker/main.go | 1 + 2 files changed, 75 insertions(+) create mode 100644 engine/worker/cmd_junit_parser.go diff --git a/engine/worker/cmd_junit_parser.go b/engine/worker/cmd_junit_parser.go new file mode 100644 index 0000000000..cdf3df19b7 --- /dev/null +++ b/engine/worker/cmd_junit_parser.go @@ -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 + } +} diff --git a/engine/worker/main.go b/engine/worker/main.go index 148849e9d2..50648e31ee 100644 --- a/engine/worker/main.go +++ b/engine/worker/main.go @@ -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))