github.com/xgoffin/jenkins-library@v1.154.0/cmd/mavenExecuteIntegration.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/SAP/jenkins-library/pkg/log"
     6  	"github.com/SAP/jenkins-library/pkg/maven"
     7  	"github.com/SAP/jenkins-library/pkg/telemetry"
     8  	"path/filepath"
     9  	"strconv"
    10  	"strings"
    11  	"unicode"
    12  )
    13  
    14  func mavenExecuteIntegration(config mavenExecuteIntegrationOptions, _ *telemetry.CustomData) {
    15  	err := runMavenExecuteIntegration(&config, maven.NewUtilsBundle())
    16  	if err != nil {
    17  		log.Entry().WithError(err).Fatal("step execution failed")
    18  	}
    19  }
    20  
    21  func runMavenExecuteIntegration(config *mavenExecuteIntegrationOptions, utils maven.Utils) error {
    22  	pomPath := filepath.Join("integration-tests", "pom.xml")
    23  	hasIntegrationTestsModule, _ := utils.FileExists(pomPath)
    24  	if !hasIntegrationTestsModule {
    25  		return fmt.Errorf("maven module 'integration-tests' does not exist in project structure")
    26  	}
    27  
    28  	if config.InstallArtifacts {
    29  		err := maven.InstallMavenArtifacts(&maven.EvaluateOptions{
    30  			M2Path:              config.M2Path,
    31  			ProjectSettingsFile: config.ProjectSettingsFile,
    32  			GlobalSettingsFile:  config.GlobalSettingsFile,
    33  		}, utils)
    34  		if err != nil {
    35  			return err
    36  		}
    37  	}
    38  
    39  	if err := validateForkCount(config.ForkCount); err != nil {
    40  		return err
    41  	}
    42  
    43  	retryDefine := fmt.Sprintf("-Dsurefire.rerunFailingTestsCount=%v", config.Retry)
    44  	forkCountDefine := fmt.Sprintf("-Dsurefire.forkCount=%v", config.ForkCount)
    45  
    46  	mavenOptions := maven.ExecuteOptions{
    47  		PomPath:             pomPath,
    48  		M2Path:              config.M2Path,
    49  		ProjectSettingsFile: config.ProjectSettingsFile,
    50  		GlobalSettingsFile:  config.GlobalSettingsFile,
    51  		Goals:               []string{"org.jacoco:jacoco-maven-plugin:prepare-agent", config.Goal},
    52  		Defines:             []string{retryDefine, forkCountDefine},
    53  	}
    54  
    55  	_, err := maven.Execute(&mavenOptions, utils)
    56  
    57  	return err
    58  }
    59  
    60  func validateForkCount(value string) error {
    61  	var err error
    62  
    63  	if strings.HasSuffix(value, "C") {
    64  		value := strings.TrimSuffix(value, "C")
    65  		for _, c := range value {
    66  			if !unicode.IsDigit(c) && c != '.' {
    67  				err = fmt.Errorf("only integers or floats allowed with 'C' suffix")
    68  				break
    69  			}
    70  		}
    71  		if err == nil {
    72  			_, err = strconv.ParseFloat(value, 64)
    73  		}
    74  	} else {
    75  		for _, c := range value {
    76  			if !unicode.IsDigit(c) {
    77  				err = fmt.Errorf("only integers allowed without 'C' suffix")
    78  				break
    79  			}
    80  		}
    81  		if err == nil {
    82  			_, err = strconv.ParseInt(value, 10, 64)
    83  		}
    84  	}
    85  
    86  	if err != nil {
    87  		return fmt.Errorf("invalid forkCount parameter '%v': %w, please see https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#forkCount for details", value, err)
    88  	}
    89  	return nil
    90  }