mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
eaf5479e9c
Avoid maven error `Unknown lifecycle phase \"-\"` when the value of a define contains `-`. Don't split and trim maven arguments. Expect they come in as a list, keep them as list. This is a breaking change compared to the old Groovy implementation which relied on using a shell for calling maven. As an example, consider this diff: ```diff - goals: 'org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate', - defines: "-Dexpression=$pomPathExpression -DforceStdout -q", + goals: ['org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate'], + defines: ["-Dexpression=$pomPathExpression", "-DforceStdout", "-q"], ```
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
"github.com/stretchr/testify/assert"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestMavenExecute(t *testing.T) {
|
|
t.Run("mavenExecute should write output file", func(t *testing.T) {
|
|
// init
|
|
config := mavenExecuteOptions{
|
|
Goals: []string{"goal"},
|
|
LogSuccessfulMavenTransfers: true,
|
|
ReturnStdout: true,
|
|
}
|
|
|
|
mockRunner := mock.ExecMockRunner{}
|
|
mockRunner.StdoutReturn = map[string]string{}
|
|
mockRunner.StdoutReturn[""] = "test output"
|
|
|
|
var outputFile string
|
|
var output []byte
|
|
|
|
oldWriteFile := writeFile
|
|
writeFile = func(filename string, data []byte, perm os.FileMode) error {
|
|
outputFile = filename
|
|
output = data
|
|
return nil
|
|
}
|
|
defer func() { writeFile = oldWriteFile }()
|
|
|
|
// test
|
|
err := runMavenExecute(config, &mockRunner)
|
|
|
|
// assert
|
|
expectedParams := []string{
|
|
"--batch-mode", "goal",
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
if assert.Equal(t, 1, len(mockRunner.Calls)) {
|
|
assert.Equal(t, "mvn", mockRunner.Calls[0].Exec)
|
|
assert.Equal(t, expectedParams, mockRunner.Calls[0].Params)
|
|
}
|
|
assert.Equal(t, "test output", string(output))
|
|
assert.Equal(t, ".pipeline/maven_output.txt", outputFile)
|
|
})
|
|
|
|
t.Run("mavenExecute should NOT write output file", func(t *testing.T) {
|
|
// init
|
|
config := mavenExecuteOptions{
|
|
Goals: []string{"goal"},
|
|
LogSuccessfulMavenTransfers: true,
|
|
}
|
|
|
|
mockRunner := mock.ExecMockRunner{}
|
|
mockRunner.StdoutReturn = map[string]string{}
|
|
mockRunner.StdoutReturn[""] = "test output"
|
|
|
|
var outputFile string
|
|
var output []byte
|
|
|
|
oldWriteFile := writeFile
|
|
writeFile = func(filename string, data []byte, perm os.FileMode) error {
|
|
outputFile = filename
|
|
output = data
|
|
return nil
|
|
}
|
|
defer func() { writeFile = oldWriteFile }()
|
|
|
|
// test
|
|
err := runMavenExecute(config, &mockRunner)
|
|
|
|
// assert
|
|
expectedParams := []string{
|
|
"--batch-mode", "goal",
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
if assert.Equal(t, 1, len(mockRunner.Calls)) {
|
|
assert.Equal(t, "mvn", mockRunner.Calls[0].Exec)
|
|
assert.Equal(t, expectedParams, mockRunner.Calls[0].Params)
|
|
}
|
|
assert.Equal(t, "", string(output))
|
|
assert.Equal(t, "", outputFile)
|
|
})
|
|
}
|