github.com/jaylevin/jenkins-library@v1.230.4/cmd/batsExecuteTests.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 9 "github.com/SAP/jenkins-library/pkg/command" 10 pipergit "github.com/SAP/jenkins-library/pkg/git" 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/go-git/go-git/v5" 15 "github.com/pkg/errors" 16 ) 17 18 type batsExecuteTestsUtils interface { 19 CloneRepo(URL string) error 20 Stdin(in io.Reader) 21 Stdout(out io.Writer) 22 Stderr(err io.Writer) 23 SetEnv([]string) 24 FileWrite(path string, content []byte, perm os.FileMode) error 25 RunExecutable(e string, p ...string) error 26 } 27 28 type batsExecuteTestsUtilsBundle struct { 29 *command.Command 30 *piperutils.Files 31 } 32 33 func newBatsExecuteTestsUtils() batsExecuteTestsUtils { 34 utils := batsExecuteTestsUtilsBundle{ 35 Command: &command.Command{}, 36 Files: &piperutils.Files{}, 37 } 38 utils.Stdout(log.Writer()) 39 utils.Stderr(log.Writer()) 40 return &utils 41 } 42 43 func batsExecuteTests(config batsExecuteTestsOptions, telemetryData *telemetry.CustomData, influx *batsExecuteTestsInflux) { 44 utils := newBatsExecuteTestsUtils() 45 46 influx.step_data.fields.bats = false 47 err := runBatsExecuteTests(&config, telemetryData, utils) 48 if err != nil { 49 log.Entry().WithError(err).Fatal("step execution failed") 50 } 51 influx.step_data.fields.bats = true 52 } 53 54 func runBatsExecuteTests(config *batsExecuteTestsOptions, telemetryData *telemetry.CustomData, utils batsExecuteTestsUtils) error { 55 if config.OutputFormat != "tap" && config.OutputFormat != "junit" { 56 log.SetErrorCategory(log.ErrorConfiguration) 57 return fmt.Errorf("output format '%v' is incorrect. Possible drivers: tap, junit", config.OutputFormat) 58 } 59 60 err := utils.CloneRepo(config.Repository) 61 62 if err != nil && !errors.Is(err, git.ErrRepositoryAlreadyExists) { 63 return fmt.Errorf("couldn't pull %s repository: %w", config.Repository, err) 64 } 65 66 tapOutput := bytes.Buffer{} 67 utils.Stdout(io.MultiWriter(&tapOutput, log.Writer())) 68 69 utils.SetEnv(config.EnvVars) 70 err = utils.RunExecutable("bats-core/bin/bats", "--recursive", "--tap", config.TestPath) 71 if err != nil { 72 return fmt.Errorf("failed to run bats test: %w", err) 73 } 74 75 err = utils.FileWrite("TEST-"+config.TestPackage+".tap", tapOutput.Bytes(), 0644) 76 if err != nil { 77 return fmt.Errorf("failed to write tap file: %w", err) 78 } 79 80 if config.OutputFormat == "junit" { 81 output := bytes.Buffer{} 82 utils.Stdout(io.MultiWriter(&output, log.Writer())) 83 84 utils.SetEnv(append(config.EnvVars, "NPM_CONFIG_PREFIX=~/.npm-global")) 85 err = utils.RunExecutable("npm", "install", "tap-xunit", "-g") 86 if err != nil { 87 return fmt.Errorf("failed to install tap-xunit: %w", err) 88 } 89 90 homedir, _ := os.UserHomeDir() 91 path := "PATH=" + os.Getenv("PATH") + ":" + homedir + "/.npm-global/bin" 92 93 output = bytes.Buffer{} 94 utils.Stdout(&output) 95 utils.Stdin(&tapOutput) 96 utils.SetEnv(append(config.EnvVars, path)) 97 err = utils.RunExecutable("tap-xunit", "--package="+config.TestPackage) 98 if err != nil { 99 return fmt.Errorf("failed to run tap-xunit: %w", err) 100 } 101 err = utils.FileWrite("TEST-"+config.TestPackage+".xml", output.Bytes(), 0644) 102 if err != nil { 103 return fmt.Errorf("failed to write tap file: %w", err) 104 } 105 } 106 107 return nil 108 } 109 110 func (b *batsExecuteTestsUtilsBundle) CloneRepo(URL string) error { 111 _, err := pipergit.PlainClone("", "", URL, "bats-core") 112 return err 113 114 }