github.com/xgoffin/jenkins-library@v1.154.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  	for _, module := range modulePaths {
    26  		command.SetDir(module)
    27  		err := command.RunExecutable(installCommandTokens[0], installCommandTokens[1:]...)
    28  		if err != nil {
    29  			log.SetErrorCategory(log.ErrorCustom)
    30  			log.Entry().
    31  				WithError(err).
    32  				WithField("command", config.InstallCommand).
    33  				Fatal("failed to execute install command")
    34  		}
    35  
    36  		command.SetDir(module)
    37  		err = command.RunExecutable(runCommandTokens[0], runCommandTokens[1:]...)
    38  		if err != nil {
    39  			log.SetErrorCategory(log.ErrorTest)
    40  			log.Entry().
    41  				WithError(err).
    42  				WithField("command", config.RunCommand).
    43  				Fatal("failed to execute run command")
    44  		}
    45  	}
    46  }
    47  
    48  func tokenize(command string) []string {
    49  	return strings.Split(command, " ")
    50  }