2020-04-24 10:41:49 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-06 17:43:32 +02:00
|
|
|
"os"
|
2020-04-24 10:41:49 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMavenExecute(t *testing.T) {
|
2020-05-06 17:43:32 +02:00
|
|
|
t.Run("mavenExecute should write output file", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
config := mavenExecuteOptions{
|
|
|
|
Goals: []string{"goal"},
|
|
|
|
LogSuccessfulMavenTransfers: true,
|
|
|
|
ReturnStdout: true,
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
mockRunner := mock.ExecMockRunner{}
|
2020-05-06 17:43:32 +02:00
|
|
|
mockRunner.StdoutReturn = map[string]string{}
|
|
|
|
mockRunner.StdoutReturn[""] = "test output"
|
2020-04-24 10:41:49 +02:00
|
|
|
|
2020-05-06 17:43:32 +02:00
|
|
|
var outputFile string
|
|
|
|
var output []byte
|
|
|
|
|
|
|
|
oldWriteFile := writeFile
|
|
|
|
writeFile = func(filename string, data []byte, perm os.FileMode) error {
|
|
|
|
outputFile = filename
|
|
|
|
output = data
|
|
|
|
return nil
|
2020-04-24 10:41:49 +02:00
|
|
|
}
|
2020-05-06 17:43:32 +02:00
|
|
|
defer func() { writeFile = oldWriteFile }()
|
2020-04-24 10:41:49 +02:00
|
|
|
|
2020-05-06 17:43:32 +02:00
|
|
|
// test
|
2020-04-24 10:41:49 +02:00
|
|
|
err := runMavenExecute(config, &mockRunner)
|
|
|
|
|
2020-05-06 17:43:32 +02:00
|
|
|
// assert
|
2020-04-24 10:41:49 +02:00
|
|
|
expectedParams := []string{
|
2020-05-06 17:43:32 +02:00
|
|
|
"--batch-mode", "goal",
|
2020-04-24 10:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:43:32 +02:00
|
|
|
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)
|
2020-04-24 10:41:49 +02:00
|
|
|
})
|
|
|
|
|
2020-05-06 17:43:32 +02:00
|
|
|
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)
|
|
|
|
})
|
2020-04-24 10:41:49 +02:00
|
|
|
}
|