github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/uiVeri5ExecuteTests.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/command"
     8  	"github.com/SAP/jenkins-library/pkg/log"
     9  	"github.com/SAP/jenkins-library/pkg/telemetry"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func uiVeri5ExecuteTests(config uiVeri5ExecuteTestsOptions, telemetryData *telemetry.CustomData) {
    14  	// for command execution use Command
    15  	c := command.Command{}
    16  	// reroute command output to logging framework
    17  	c.Stdout(log.Writer())
    18  	c.Stderr(log.Writer())
    19  
    20  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    21  	err := runUIVeri5(&config, &c)
    22  	if err != nil {
    23  		log.Entry().WithError(err).Fatal("step execution failed")
    24  	}
    25  }
    26  
    27  func runUIVeri5(config *uiVeri5ExecuteTestsOptions, command command.ExecRunner) error {
    28  	envs := []string{"NPM_CONFIG_PREFIX=~/.npm-global"}
    29  	path := "PATH=" + os.Getenv("PATH") + ":~/.npm-global/bin"
    30  	envs = append(envs, path)
    31  	if config.TestServerURL != "" {
    32  		envs = append(envs, "TARGET_SERVER_URL="+config.TestServerURL)
    33  	}
    34  	command.SetEnv(envs)
    35  
    36  	installCommandTokens := strings.Split(config.InstallCommand, " ")
    37  	if err := command.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...); err != nil {
    38  		log.SetErrorCategory(log.ErrorCustom)
    39  		return errors.Wrapf(err, "failed to execute install command: %v", config.InstallCommand)
    40  	}
    41  
    42  	if config.TestOptions != "" {
    43  		log.SetErrorCategory(log.ErrorConfiguration)
    44  		return errors.Errorf("parameter testOptions no longer supported, please use runOptions parameter instead.")
    45  	}
    46  	if err := command.RunExecutable(config.RunCommand, config.RunOptions...); err != nil {
    47  		log.SetErrorCategory(log.ErrorTest)
    48  		return errors.Wrapf(err, "failed to execute run command: %v %v", config.RunCommand, strings.Join(config.RunOptions, " "))
    49  	}
    50  	return nil
    51  }