github.com/jenkins-x/jx/v2@v2.1.155/pkg/reportingtools/XUnitViewer.go (about) 1 package reportingtools 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/jenkins-x/jx-logging/pkg/log" 8 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 9 "github.com/jenkins-x/jx/v2/pkg/util" 10 "github.com/pkg/errors" 11 "gopkg.in/AlecAivazis/survey.v1" 12 ) 13 14 // XUnitViewer is an implementation of the XUnitClient interface 15 type XUnitViewer struct{} 16 17 // EnsureXUnitViewer makes sure `xunit-viewer` is installed, otherwise it attempts to install it. It also checks NPM is installed 18 func (c XUnitViewer) EnsureXUnitViewer(o *opts.CommonOptions) error { 19 20 cmd := util.Command{ 21 Name: "xunit-viewer", 22 } 23 24 _, err := cmd.RunWithoutRetry() 25 26 if err == nil { 27 return nil 28 } 29 installBinary := true 30 if !o.BatchMode { 31 surveyOpts := survey.WithStdio(o.In, o.Out, o.Err) 32 confirm := &survey.Confirm{ 33 Message: fmt.Sprint("xunit-viewer doesn't seem to be installed, would you like to install it?"), 34 Default: true, 35 } 36 37 err = survey.AskOne(confirm, &installBinary, nil, surveyOpts) 38 if err != nil { 39 return err 40 } 41 } 42 if installBinary { 43 err := c.EnsureNPMIsInstalled() 44 if err != nil { 45 log.Logger().Warn("npm is not installed, we can't install xunit-viewer") 46 return err 47 } 48 49 cmd = util.Command{ 50 Name: "npm", 51 Args: []string{"i", "-g", "xunit-viewer"}, 52 Timeout: time.Minute * 5, 53 } 54 55 _, err = cmd.RunWithoutRetry() 56 if err != nil { 57 log.Logger().Warn("it was impossible to install xunit-viewer, skipping") 58 return err 59 } 60 } 61 62 return nil 63 } 64 65 // EnsureNPMIsInstalled makes sure NPM is installed and fails otherwise 66 func (c XUnitViewer) EnsureNPMIsInstalled() error { 67 cmd := util.Command{ 68 Name: "npm", 69 Args: []string{"--version"}, 70 } 71 72 _, err := cmd.RunWithoutRetry() 73 74 return err 75 } 76 77 // CreateHTMLReport uses xunit-viewer to create an HTML report into the outputReportName, with a given suitename, from a given xml report 78 func (c XUnitViewer) CreateHTMLReport(outputReportName, suiteName, targetFileName string) error { 79 cmd := util.Command{ 80 Name: "xunit-viewer", 81 Args: []string{ 82 "--results=" + targetFileName, 83 "--output=" + outputReportName, 84 "--title=" + suiteName, 85 }, 86 } 87 88 out, err := cmd.RunWithoutRetry() 89 fmt.Println(out) 90 if err != nil { 91 return errors.Wrapf(err, "There was a problem generating the html report") 92 } 93 94 return nil 95 }