github.com/SAP/jenkins-library@v1.362.0/cmd/karmaExecuteTests.go (about)

     1  package cmd
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/SAP/jenkins-library/pkg/command"
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/telemetry"
     9  )
    10  
    11  func karmaExecuteTests(config karmaExecuteTestsOptions, telemetryData *telemetry.CustomData) {
    12  	c := command.Command{}
    13  	// reroute command output to loging framework
    14  	// also log stdout as Karma reports into it
    15  	c.Stdout(log.Writer())
    16  	c.Stderr(log.Writer())
    17  	runKarma(config, &c)
    18  }
    19  
    20  func runKarma(config karmaExecuteTestsOptions, command command.ExecRunner) {
    21  	installCommandTokens := tokenize(config.InstallCommand)
    22  	runCommandTokens := tokenize(config.RunCommand)
    23  	modulePaths := config.Modules
    24  
    25  	if GeneralConfig.Verbose {
    26  		runCommandTokens = append(runCommandTokens, "--", "--log-level", "DEBUG")
    27  	}
    28  
    29  	for _, module := range modulePaths {
    30  		command.SetDir(module)
    31  		err := command.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...)
    32  		if err != nil {
    33  			log.SetErrorCategory(log.ErrorCustom)
    34  			log.Entry().
    35  				WithError(err).
    36  				WithField("command", config.InstallCommand).
    37  				Fatal("failed to execute install command")
    38  		}
    39  
    40  		command.SetDir(module)
    41  		err = command.RunExecutable(runCommandTokens[0], runCommandTokens[1:]...)
    42  		if err != nil {
    43  			log.SetErrorCategory(log.ErrorTest)
    44  			log.Entry().
    45  				WithError(err).
    46  				WithField("command", config.RunCommand).
    47  				Fatal("failed to execute run command")
    48  		}
    49  	}
    50  }
    51  
    52  func tokenize(command string) []string {
    53  	return strings.Split(command, " ")
    54  }