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:
parent
04cdf4b2fe
commit
a40ac015a8
@ -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")
|
||||
}
|
||||
|
@ -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
46
pkg/mock/fileUtils.go
Normal 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")
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user