github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/gaugeExecuteTests.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/SAP/jenkins-library/pkg/command" 11 "github.com/SAP/jenkins-library/pkg/log" 12 "github.com/SAP/jenkins-library/pkg/piperutils" 13 "github.com/SAP/jenkins-library/pkg/telemetry" 14 "github.com/pkg/errors" 15 ) 16 17 var ErrorGaugeInstall error = errors.New("error installing gauge") 18 var ErrorGaugeRunnerInstall error = errors.New("error installing runner") 19 var ErrorGaugeRun error = errors.New("error running gauge") 20 21 type gaugeExecuteTestsUtils interface { 22 FileExists(filename string) (bool, error) 23 MkdirAll(path string, perm os.FileMode) error 24 SetEnv([]string) 25 RunExecutable(executable string, params ...string) error 26 Stdout(io.Writer) 27 Getenv(key string) string 28 } 29 30 type gaugeExecuteTestsUtilsBundle struct { 31 *command.Command 32 *piperutils.Files 33 } 34 35 func newGaugeExecuteTestsUtils() gaugeExecuteTestsUtils { 36 utils := gaugeExecuteTestsUtilsBundle{ 37 Command: &command.Command{}, 38 Files: &piperutils.Files{}, 39 } 40 utils.Stdout(log.Writer()) 41 utils.Stderr(log.Writer()) 42 return &utils 43 } 44 45 func gaugeExecuteTests(config gaugeExecuteTestsOptions, telemetryData *telemetry.CustomData, influx *gaugeExecuteTestsInflux) { 46 utils := newGaugeExecuteTestsUtils() 47 48 influx.step_data.fields.gauge = false 49 err := runGaugeExecuteTests(&config, telemetryData, utils) 50 if err != nil { 51 log.Entry().WithError(err).Fatal("step execution failed") 52 } 53 influx.step_data.fields.gauge = true 54 } 55 56 func runGaugeExecuteTests(config *gaugeExecuteTestsOptions, telemetryData *telemetry.CustomData, utils gaugeExecuteTestsUtils) error { 57 if config.InstallCommand != "" { 58 err := installGauge(config.InstallCommand, utils) 59 if err != nil { 60 return err 61 } 62 } 63 64 if config.LanguageRunner != "" { 65 err := installLanguageRunner(config.LanguageRunner, utils) 66 if err != nil { 67 return err 68 } 69 } 70 71 err := runGauge(config, utils) 72 if err != nil { 73 return fmt.Errorf("failed to run gauge: %w", err) 74 } 75 76 return nil 77 } 78 79 func installGauge(gaugeInstallCommand string, utils gaugeExecuteTestsUtils) error { 80 installGaugeTokens := strings.Split(gaugeInstallCommand, " ") 81 installGaugeTokens = append(installGaugeTokens, "--prefix=~/.npm-global") 82 err := utils.RunExecutable(installGaugeTokens[0], installGaugeTokens[1:]...) 83 if err != nil { 84 log.SetErrorCategory(log.ErrorConfiguration) 85 return errors.Wrap(ErrorGaugeInstall, err.Error()) 86 } 87 88 return nil 89 } 90 91 func installLanguageRunner(languageRunner string, utils gaugeExecuteTestsUtils) error { 92 installParams := []string{"install", languageRunner} 93 gaugePath := filepath.FromSlash(filepath.Join(utils.Getenv("HOME"), "/.npm-global/bin/gauge")) 94 err := utils.RunExecutable(gaugePath, installParams...) 95 if err != nil { 96 log.SetErrorCategory(log.ErrorConfiguration) 97 return errors.Wrap(ErrorGaugeRunnerInstall, err.Error()) 98 } 99 return nil 100 101 } 102 103 func runGauge(config *gaugeExecuteTestsOptions, utils gaugeExecuteTestsUtils) error { 104 runCommandTokens := strings.Split(config.RunCommand, " ") 105 if config.TestOptions != "" { 106 runCommandTokens = append(runCommandTokens, strings.Split(config.TestOptions, " ")...) 107 } 108 gaugePath := filepath.FromSlash(filepath.Join(utils.Getenv("HOME"), "/.npm-global/bin/gauge")) 109 err := utils.RunExecutable(gaugePath, runCommandTokens...) 110 if err != nil { 111 return errors.Wrap(ErrorGaugeRun, err.Error()) 112 } 113 return nil 114 } 115 116 func (utils gaugeExecuteTestsUtilsBundle) Getenv(key string) string { 117 return os.Getenv(key) 118 }