2022-03-29 19:01:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-26 14:15:24 +02:00
|
|
|
"path/filepath"
|
2022-03-29 19:01:44 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-07-21 09:04:21 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2022-03-29 19:01:44 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pythonBuildMockUtils struct {
|
|
|
|
config *pythonBuildOptions
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPythonBuildTestsUtils() pythonBuildMockUtils {
|
|
|
|
utils := pythonBuildMockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
return utils
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *pythonBuildMockUtils) GetConfig() *pythonBuildOptions {
|
|
|
|
return f.config
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunPythonBuild(t *testing.T) {
|
2022-04-05 09:16:46 +02:00
|
|
|
cpe := pythonBuildCommonPipelineEnvironment{}
|
2022-03-29 19:01:44 +02:00
|
|
|
t.Run("success - build", func(t *testing.T) {
|
2022-04-26 14:15:24 +02:00
|
|
|
config := pythonBuildOptions{
|
|
|
|
VirutalEnvironmentName: "dummy",
|
|
|
|
}
|
2022-03-29 19:01:44 +02:00
|
|
|
utils := newPythonBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-04-26 14:15:24 +02:00
|
|
|
runPythonBuild(&config, &telemetryData, utils, &cpe)
|
2022-03-29 19:01:44 +02:00
|
|
|
assert.Equal(t, "python3", utils.ExecMockRunner.Calls[0].Exec)
|
2022-04-26 14:15:24 +02:00
|
|
|
assert.Equal(t, []string{"-m", "venv", "dummy"}, utils.ExecMockRunner.Calls[0].Params)
|
2022-03-29 19:01:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - build failure", func(t *testing.T) {
|
|
|
|
config := pythonBuildOptions{}
|
|
|
|
utils := newPythonBuildTestsUtils()
|
2022-04-26 14:15:24 +02:00
|
|
|
utils.ShouldFailOnCommand = map[string]error{"python setup.py sdist bdist_wheel": fmt.Errorf("build failure")}
|
2022-03-29 19:01:44 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-04-05 09:16:46 +02:00
|
|
|
err := runPythonBuild(&config, &telemetryData, utils, &cpe)
|
2022-03-29 19:01:44 +02:00
|
|
|
assert.EqualError(t, err, "Python build failed with error: build failure")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - publishes binaries", func(t *testing.T) {
|
|
|
|
config := pythonBuildOptions{
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
2022-04-26 14:15:24 +02:00
|
|
|
VirutalEnvironmentName: "dummy",
|
2022-03-29 19:01:44 +02:00
|
|
|
}
|
|
|
|
utils := newPythonBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-04-26 14:15:24 +02:00
|
|
|
runPythonBuild(&config, &telemetryData, utils, &cpe)
|
2022-03-29 19:01:44 +02:00
|
|
|
assert.Equal(t, "python3", utils.ExecMockRunner.Calls[0].Exec)
|
2022-04-26 14:15:24 +02:00
|
|
|
assert.Equal(t, []string{"-m", "venv", config.VirutalEnvironmentName}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "bash", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"-c", "source " + filepath.Join("dummy", "bin", "activate")}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
assert.Equal(t, "python", utils.ExecMockRunner.Calls[2].Exec)
|
|
|
|
assert.Equal(t, []string{"setup.py", "sdist", "bdist_wheel"}, utils.ExecMockRunner.Calls[2].Params)
|
|
|
|
assert.Equal(t, filepath.Join("dummy", "bin", "pip"), utils.ExecMockRunner.Calls[3].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "--upgrade", "twine"}, utils.ExecMockRunner.Calls[3].Params)
|
|
|
|
assert.Equal(t, filepath.Join("dummy", "bin", "twine"), utils.ExecMockRunner.Calls[4].Exec)
|
2022-03-29 19:01:44 +02:00
|
|
|
assert.Equal(t, []string{"upload", "--username", config.TargetRepositoryUser,
|
|
|
|
"--password", config.TargetRepositoryPassword, "--repository-url", config.TargetRepositoryURL,
|
2022-06-28 12:15:25 +02:00
|
|
|
"--disable-progress-bar", "dist/*"}, utils.ExecMockRunner.Calls[4].Params)
|
2022-03-29 19:01:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - create BOM", func(t *testing.T) {
|
|
|
|
config := pythonBuildOptions{
|
2022-04-26 14:15:24 +02:00
|
|
|
CreateBOM: true,
|
|
|
|
Publish: false,
|
|
|
|
VirutalEnvironmentName: "dummy",
|
2022-03-29 19:01:44 +02:00
|
|
|
}
|
|
|
|
utils := newPythonBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-04-26 14:15:24 +02:00
|
|
|
runPythonBuild(&config, &telemetryData, utils, &cpe)
|
|
|
|
// assert.NoError(t, err)
|
2022-03-29 19:01:44 +02:00
|
|
|
assert.Equal(t, "python3", utils.ExecMockRunner.Calls[0].Exec)
|
2022-04-26 14:15:24 +02:00
|
|
|
assert.Equal(t, []string{"-m", "venv", config.VirutalEnvironmentName}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "bash", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"-c", "source " + filepath.Join("dummy", "bin", "activate")}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
assert.Equal(t, "python", utils.ExecMockRunner.Calls[2].Exec)
|
|
|
|
assert.Equal(t, []string{"setup.py", "sdist", "bdist_wheel"}, utils.ExecMockRunner.Calls[2].Params)
|
|
|
|
assert.Equal(t, filepath.Join("dummy", "bin", "pip"), utils.ExecMockRunner.Calls[3].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "--upgrade", "cyclonedx-bom"}, utils.ExecMockRunner.Calls[3].Params)
|
|
|
|
assert.Equal(t, filepath.Join("dummy", "bin", "cyclonedx-bom"), utils.ExecMockRunner.Calls[4].Exec)
|
2022-08-01 13:38:49 +02:00
|
|
|
assert.Equal(t, []string{"--e", "--output", "bom-pip.xml"}, utils.ExecMockRunner.Calls[4].Params)
|
2022-03-29 19:01:44 +02:00
|
|
|
})
|
|
|
|
}
|