1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Add tests for maven build (#1276)

This commit is contained in:
Daniel Kurzynski 2020-03-26 08:23:21 +01:00 committed by GitHub
parent 04cdf4b2fe
commit a40ac015a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 6 deletions

View File

@ -14,16 +14,18 @@ func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData) {
c.Stdout(log.Entry().Writer())
c.Stderr(log.Entry().Writer())
err := runMavenBuild(&config, telemetryData, &c)
utils := piperutils.Files{}
err := runMavenBuild(&config, telemetryData, &c, &utils)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomData, command execRunner) error {
func runMavenBuild(config *mavenBuildOptions, telemetryData *telemetry.CustomData, command execRunner, utils piperutils.FileUtils) error {
var flags = []string{"-update-snapshots", "--batch-mode"}
exists, _ := piperutils.FileExists("integration-tests/pom.xml")
exists, _ := utils.FileExists("integration-tests/pom.xml")
if exists {
flags = append(flags, "-pl", "!integration-tests")
}

View File

@ -10,21 +10,40 @@ func TestMavenBuild(t *testing.T) {
t.Run("mavenBuild should install the artifact", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
mockedUtils := mock.FilesMock{}
config := mavenBuildOptions{}
err := runMavenBuild(&config, nil, &execMockRunner)
err := runMavenBuild(&config, nil, &execMockRunner, &mockedUtils)
assert.Nil(t, err)
assert.Equal(t, execMockRunner.Calls[0].Exec, "mvn")
assert.Contains(t, execMockRunner.Calls[0].Params, "install")
})
t.Run("mavenBuild should skip integration tests", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
mockedUtils := mock.FilesMock{}
mockedUtils.Files = []string{"integration-tests/pom.xml"}
config := mavenBuildOptions{}
err := runMavenBuild(&config, nil, &execMockRunner, &mockedUtils)
assert.Nil(t, err)
assert.Equal(t, execMockRunner.Calls[0].Exec, "mvn")
assert.Contains(t, execMockRunner.Calls[0].Params, "-pl", "!integration-tests")
})
t.Run("mavenBuild should flatten", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
mockedUtils := mock.FilesMock{}
config := mavenBuildOptions{Flatten: true}
err := runMavenBuild(&config, nil, &execMockRunner)
err := runMavenBuild(&config, nil, &execMockRunner, &mockedUtils)
assert.Nil(t, err)
assert.Contains(t, execMockRunner.Calls[0].Params, "flatten:flatten")
@ -35,9 +54,11 @@ func TestMavenBuild(t *testing.T) {
t.Run("mavenBuild should run only verify", func(t *testing.T) {
execMockRunner := mock.ExecMockRunner{}
mockedUtils := mock.FilesMock{}
config := mavenBuildOptions{Verify: true}
err := runMavenBuild(&config, nil, &execMockRunner)
err := runMavenBuild(&config, nil, &execMockRunner, &mockedUtils)
assert.Nil(t, err)
assert.Contains(t, execMockRunner.Calls[0].Params, "verify")

46
pkg/mock/fileUtils.go Normal file
View File

@ -0,0 +1,46 @@
// +build !release
package mock
import (
"errors"
"os"
)
// FilesMock ...
type FilesMock struct {
Files []string
}
// FileExists ...
func (f FilesMock) FileExists(filename string) (bool, error) {
for _, file := range f.Files {
if file == filename {
return true, nil
}
}
return false, nil
}
// Copy ...
func (f FilesMock) Copy(src, dst string) (int64, error) {
return 0, errors.New("Not implemented")
}
// FileRead ...
func (f FilesMock) FileRead(path string) ([]byte, error) {
return nil, errors.New("Not implemented")
}
// FileWrite ...
func (f FilesMock) FileWrite(path string, content []byte, perm os.FileMode) error {
return errors.New("Not implemented")
}
// MkdirAll ...
func (f FilesMock) MkdirAll(path string, perm os.FileMode) error {
return errors.New("Not implemented")
}