2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2022-03-21 11:17:03 +02:00
|
|
|
package gradle
|
|
|
|
|
|
|
|
import (
|
2022-04-14 14:59:09 +02:00
|
|
|
"errors"
|
2022-03-21 11:17:03 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockUtils struct {
|
2022-04-14 14:59:09 +02:00
|
|
|
existingFiles []string
|
|
|
|
writtenFiles []string
|
|
|
|
removedFiles []string
|
2022-03-21 11:17:03 +02:00
|
|
|
*mock.FilesMock
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:59:09 +02:00
|
|
|
func (m *MockUtils) FileExists(filePath string) (bool, error) {
|
|
|
|
for _, filename := range m.existingFiles {
|
|
|
|
if filename == filePath {
|
|
|
|
return true, nil
|
|
|
|
}
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:59:09 +02:00
|
|
|
func (m *MockUtils) FileWrite(path string, content []byte, perm os.FileMode) error {
|
|
|
|
m.writtenFiles = append(m.writtenFiles, path)
|
2022-03-21 11:17:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:59:09 +02:00
|
|
|
func (m *MockUtils) FileRemove(path string) error {
|
|
|
|
m.removedFiles = append(m.removedFiles, path)
|
2022-03-21 11:17:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecute(t *testing.T) {
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("success - run command use gradle tool", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
existingFiles: []string{"path/to/build.gradle"},
|
|
|
|
}
|
2022-03-21 11:17:03 +02:00
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "",
|
|
|
|
UseWrapper: false,
|
|
|
|
ProjectProperties: map[string]string{"propName": "propValue"},
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(utils.Calls))
|
2022-06-07 10:24:10 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "gradle", Params: []string{"build", "-p", "path/to", "-PpropName=propValue"}}, utils.Calls[0])
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.Equal(t, []string(nil), utils.writtenFiles)
|
|
|
|
assert.Equal(t, []string(nil), utils.removedFiles)
|
|
|
|
})
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("success - run command use gradlew tool", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
2022-06-07 10:24:10 +02:00
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
existingFiles: []string{"path/to/build.gradle", "gradlew"},
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "",
|
|
|
|
UseWrapper: true,
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(utils.Calls))
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "./gradlew", Params: []string{"build", "-p", "path/to"}}, utils.Calls[0])
|
|
|
|
assert.Equal(t, []string(nil), utils.writtenFiles)
|
|
|
|
assert.Equal(t, []string(nil), utils.removedFiles)
|
2022-04-14 14:59:09 +02:00
|
|
|
})
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("use init script to apply plugin", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
2022-06-07 10:24:10 +02:00
|
|
|
existingFiles: []string{"path/to/build.gradle.kts"},
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
2022-03-21 11:17:03 +02:00
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "some content",
|
|
|
|
UseWrapper: false,
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
assert.Equal(t, 2, len(utils.Calls))
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "gradle", Params: []string{"tasks", "-p", "path/to"}}, utils.Calls[0])
|
|
|
|
assert.Equal(t, mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "gradle", Params: []string{"build", "-p", "path/to", "--init-script", "initScript.gradle.tmp"}}, utils.Calls[1])
|
|
|
|
assert.Equal(t, []string{"initScript.gradle.tmp"}, utils.writtenFiles)
|
|
|
|
assert.Equal(t, []string{"initScript.gradle.tmp"}, utils.removedFiles)
|
2022-03-21 11:17:03 +02:00
|
|
|
})
|
2022-04-14 14:59:09 +02:00
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("failed - use init script to apply plugin", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{
|
2022-06-07 10:24:10 +02:00
|
|
|
ShouldFailOnCommand: map[string]error{"gradle tasks -p path/to": errors.New("failed to get tasks")},
|
2022-04-14 14:59:09 +02:00
|
|
|
},
|
2022-06-07 10:24:10 +02:00
|
|
|
existingFiles: []string{"path/to/build.gradle.kts"},
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "some content",
|
|
|
|
UseWrapper: false,
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "failed to get tasks")
|
2022-04-14 14:59:09 +02:00
|
|
|
})
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("use init script to apply an existing plugin", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
2022-06-07 10:24:10 +02:00
|
|
|
existingFiles: []string{"path/to/build.gradle.kts"},
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
2022-06-07 10:24:10 +02:00
|
|
|
utils.StdoutReturn = map[string]string{"gradle tasks -p path/to": "createBom"}
|
2022-04-14 14:59:09 +02:00
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "createBom",
|
|
|
|
InitScriptContent: "some content",
|
|
|
|
UseWrapper: false,
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
2022-04-14 14:59:09 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(utils.Calls))
|
2022-06-07 10:24:10 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "gradle", Params: []string{"tasks", "-p", "path/to"}}, utils.Calls[0])
|
|
|
|
assert.Equal(t, mock.ExecCall{Execution: (*mock.Execution)(nil), Async: false, Exec: "gradle", Params: []string{"createBom", "-p", "path/to"}}, utils.Calls[1])
|
|
|
|
assert.Equal(t, []string(nil), utils.writtenFiles)
|
|
|
|
assert.Equal(t, []string(nil), utils.removedFiles)
|
2022-04-14 14:59:09 +02:00
|
|
|
})
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
t.Run("failed - run command", func(t *testing.T) {
|
2022-04-14 14:59:09 +02:00
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{
|
2022-06-07 10:24:10 +02:00
|
|
|
ShouldFailOnCommand: map[string]error{"gradle build -p path/to": errors.New("failed to build")},
|
2022-04-14 14:59:09 +02:00
|
|
|
},
|
2022-06-07 10:24:10 +02:00
|
|
|
existingFiles: []string{"path/to/build.gradle"},
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
opts := ExecuteOptions{
|
2022-06-07 10:24:10 +02:00
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "",
|
|
|
|
UseWrapper: false,
|
2022-04-14 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
_, err := Execute(&opts, utils)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "failed to build")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failed - missing gradle build script", func(t *testing.T) {
|
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
existingFiles: []string{},
|
|
|
|
}
|
|
|
|
opts := ExecuteOptions{
|
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "",
|
|
|
|
UseWrapper: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := Execute(&opts, utils)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "the specified gradle build script could not be found")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - should return stdOut", func(t *testing.T) {
|
|
|
|
expectedOutput := "mocked output"
|
|
|
|
utils := &MockUtils{
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
existingFiles: []string{"path/to/build.gradle"},
|
|
|
|
}
|
|
|
|
utils.StdoutReturn = map[string]string{"gradle build -p path/to": expectedOutput}
|
|
|
|
opts := ExecuteOptions{
|
|
|
|
BuildGradlePath: "path/to",
|
|
|
|
Task: "build",
|
|
|
|
InitScriptContent: "",
|
|
|
|
UseWrapper: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
actualOutput, err := Execute(&opts, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedOutput, actualOutput)
|
2022-04-14 14:59:09 +02:00
|
|
|
})
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|