1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/mavenBuild_test.go
Anil Keshav a92dd234b1
feat (buildSettingsInfo) new common cpe which holds build settings json value (#3272)
* create build settings for maven

* cases for when mavenBuild may be present

* fixing unit test for mavenBuild to include cpe

* changing position of buildSettngsJson to be called atfter build runs

* package

* extending the struct for other build types

* adding values for mta build settings

* changing config data type

* adding npm build settings

* unit tests

* fix trailing space

* typo correction in yaml

* Vitalii/build settings info (#3277)

* Add buildsettings package

* Improve buildSetting package for mta, npm

* Add unit-test

* Fix

* Fix

Co-authored-by: Vitalii Sidorov <vitalii.sidorov@sap.com>

* review changes

* removing buildTool param

* changing npm script name

* fix npmExecute tests

* including build settings info in npm struct

Co-authored-by: Your Name <you@example.com>
Co-authored-by: kingvvgo <56587879+kingvvgo@users.noreply.github.com>
Co-authored-by: Vitalii Sidorov <vitalii.sidorov@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-16 15:02:57 +01:00

135 lines
4.5 KiB
Go

package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMavenBuild(t *testing.T) {
cpe := mavenBuildCommonPipelineEnvironment{}
t.Run("mavenBuild should install the artifact", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
})
t.Run("mavenBuild should skip integration tests", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
mockedUtils.AddFile("integration-tests/pom.xml", []byte{})
config := mavenBuildOptions{}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
assert.Contains(t, mockedUtils.Calls[0].Params, "-pl", "!integration-tests")
})
t.Run("mavenBuild should flatten", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Flatten: true}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[0].Params, "flatten:flatten")
assert.Contains(t, mockedUtils.Calls[0].Params, "-Dflatten.mode=resolveCiFriendliesOnly")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DupdatePomFile=true")
})
t.Run("mavenBuild should run only verify", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Verify: true}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[0].Params, "verify")
assert.NotContains(t, mockedUtils.Calls[0].Params, "install")
})
t.Run("mavenBuild should createBOM", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{CreateBOM: true}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[0].Params, "org.cyclonedx:cyclonedx-maven-plugin:makeAggregateBom")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DschemaVersion=1.2")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeBomSerialNumber=true")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeCompileScope=true")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeProvidedScope=true")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeRuntimeScope=true")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeSystemScope=true")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeTestScope=false")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DincludeLicenseText=false")
assert.Contains(t, mockedUtils.Calls[0].Params, "-DoutputFormat=xml")
})
t.Run("mavenBuild include install and deploy when publish is true", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Publish: true, Verify: false}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[0].Params, "install")
assert.NotContains(t, mockedUtils.Calls[0].Params, "verify")
assert.Contains(t, mockedUtils.Calls[1].Params, "deploy")
})
t.Run("mavenBuild with deploy must skip build, install and test", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Publish: true, Verify: false}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.main.skip=true")
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.test.skip=true")
assert.Contains(t, mockedUtils.Calls[1].Params, "-Dmaven.install.skip=true")
})
t.Run("mavenBuild with deploy must include alt repo id and url when passed as parameter", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Publish: true, Verify: false, AltDeploymentRepositoryID: "ID", AltDeploymentRepositoryURL: "http://sampleRepo.com"}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[1].Params, "-DaltDeploymentRepository=ID::default::http://sampleRepo.com")
})
t.Run("mavenBuild accepts profiles", func(t *testing.T) {
mockedUtils := newMavenMockUtils()
config := mavenBuildOptions{Profiles: []string{"profile1", "profile2"}}
err := runMavenBuild(&config, nil, &mockedUtils, &cpe)
assert.Nil(t, err)
assert.Contains(t, mockedUtils.Calls[0].Params, "--activate-profiles")
assert.Contains(t, mockedUtils.Calls[0].Params, "profile1,profile2")
})
}