-
Notifications
You must be signed in to change notification settings - Fork 784
/
XUnitViewer.go
95 lines (78 loc) · 2.21 KB
/
XUnitViewer.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package reportingtools
import (
"fmt"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
)
// XUnitViewer is an implementation of the XUnitClient interface
type XUnitViewer struct{}
// EnsureXUnitViewer makes sure `xunit-viewer` is installed, otherwise it attempts to install it. It also checks NPM is installed
func (c XUnitViewer) EnsureXUnitViewer(o *opts.CommonOptions) error {
cmd := util.Command{
Name: "xunit-viewer",
}
_, err := cmd.RunWithoutRetry()
if err == nil {
return nil
}
installBinary := true
if !o.BatchMode {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
confirm := &survey.Confirm{
Message: fmt.Sprint("xunit-viewer doesn't seem to be installed, would you like to install it?"),
Default: true,
}
err = survey.AskOne(confirm, &installBinary, nil, surveyOpts)
if err != nil {
return err
}
}
if installBinary {
err := c.EnsureNPMIsInstalled()
if err != nil {
log.Logger().Warn("npm is not installed, we can't install xunit-viewer")
return err
}
cmd = util.Command{
Name: "npm",
Args: []string{"i", "-g", "xunit-viewer"},
Timeout: time.Minute * 5,
}
_, err = cmd.RunWithoutRetry()
if err != nil {
log.Logger().Warn("it was impossible to install xunit-viewer, skipping")
return err
}
}
return nil
}
// EnsureNPMIsInstalled makes sure NPM is installed and fails otherwise
func (c XUnitViewer) EnsureNPMIsInstalled() error {
cmd := util.Command{
Name: "npm",
Args: []string{"--version"},
}
_, err := cmd.RunWithoutRetry()
return err
}
// CreateHTMLReport uses xunit-viewer to create an HTML report into the outputReportName, with a given suitename, from a given xml report
func (c XUnitViewer) CreateHTMLReport(outputReportName, suiteName, targetFileName string) error {
cmd := util.Command{
Name: "xunit-viewer",
Args: []string{
"--results=" + targetFileName,
"--output=" + outputReportName,
"--title=" + suiteName,
},
}
out, err := cmd.RunWithoutRetry()
fmt.Println(out)
if err != nil {
return errors.Wrapf(err, "There was a problem generating the html report")
}
return nil
}