2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
2022-03-15 10:08:24 +02:00
|
|
|
"net/http"
|
2020-04-03 16:34:40 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-03-15 10:08:24 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2020-04-03 16:34:40 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-15 10:08:24 +02:00
|
|
|
type versioningMockUtils struct {
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func newVersioningMockUtils() *versioningMockUtils {
|
|
|
|
utils := versioningMockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *versioningMockUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
|
|
|
// so far no dedicated logic required for testing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
func TestGetArtifact(t *testing.T) {
|
2020-04-15 13:12:43 +02:00
|
|
|
t.Run("custom", func(t *testing.T) {
|
|
|
|
custom, err := GetArtifact("custom", "test.ini", &Options{VersionField: "theversion", VersionSection: "test"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := custom.(*INIfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "test.ini", theType.path)
|
|
|
|
assert.Equal(t, "theversion", theType.versionField)
|
|
|
|
assert.Equal(t, "test", theType.versionSection)
|
|
|
|
assert.Equal(t, "semver2", custom.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("docker", func(t *testing.T) {
|
|
|
|
docker, err := GetArtifact("docker", "test.ini", &Options{VersionSource: "custom", VersionField: "theversion", VersionSection: "test"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := docker.(*Docker)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "test.ini", theType.path)
|
|
|
|
assert.Equal(t, "theversion", theType.options.VersionField)
|
|
|
|
assert.Equal(t, "test", theType.options.VersionSection)
|
2020-05-06 22:07:27 +02:00
|
|
|
assert.Equal(t, "docker", docker.VersioningScheme())
|
2020-04-15 13:12:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("dub", func(t *testing.T) {
|
|
|
|
dub, err := GetArtifact("dub", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := dub.(*JSONfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "dub.json", theType.path)
|
|
|
|
assert.Equal(t, "version", theType.versionField)
|
|
|
|
assert.Equal(t, "semver2", dub.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2022-03-31 17:07:10 +02:00
|
|
|
t.Run("golang - version file", func(t *testing.T) {
|
|
|
|
fileExists = func(s string) (bool, error) {
|
|
|
|
if s == "go.mod" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
golang, err := GetArtifact("golang", "", &Options{}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := golang.(*Versionfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "VERSION", theType.path)
|
|
|
|
assert.Equal(t, "semver2", golang.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2022-03-31 17:07:10 +02:00
|
|
|
t.Run("golang - gomod", func(t *testing.T) {
|
|
|
|
fileExists = func(s string) (bool, error) {
|
|
|
|
if s == "go.mod" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
golang, err := GetArtifact("golang", "", &Options{}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := golang.(*GoMod)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "go.mod", theType.path)
|
|
|
|
assert.Equal(t, "semver2", golang.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
t.Run("golang - error", func(t *testing.T) {
|
|
|
|
fileExists = func(string) (bool, error) { return false, nil }
|
|
|
|
_, err := GetArtifact("golang", "", &Options{}, nil)
|
|
|
|
|
2022-03-31 17:07:10 +02:00
|
|
|
assert.EqualError(t, err, "no build descriptor available, supported: [go.mod VERSION version.txt]")
|
2020-04-15 13:12:43 +02:00
|
|
|
})
|
|
|
|
|
2020-11-06 10:20:08 +02:00
|
|
|
t.Run("gradle", func(t *testing.T) {
|
|
|
|
gradle, err := GetArtifact("gradle", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := gradle.(*Gradle)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "gradle.properties", theType.path)
|
|
|
|
assert.Equal(t, "theversion", theType.versionField)
|
|
|
|
assert.Equal(t, "semver2", gradle.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2022-03-15 10:08:24 +02:00
|
|
|
t.Run("helm", func(t *testing.T) {
|
|
|
|
helm, err := GetArtifact("helm", "testchart/Chart.yaml", &Options{}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := helm.(*HelmChart)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "testchart/Chart.yaml", theType.path)
|
|
|
|
assert.Equal(t, "semver2", helm.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
t.Run("maven", func(t *testing.T) {
|
2020-04-15 13:12:43 +02:00
|
|
|
opts := Options{
|
|
|
|
ProjectSettingsFile: "projectsettings.xml",
|
|
|
|
GlobalSettingsFile: "globalsettings.xml",
|
|
|
|
M2Path: "m2/path",
|
|
|
|
}
|
|
|
|
maven, err := GetArtifact("maven", "", &opts, nil)
|
2020-04-03 16:34:40 +02:00
|
|
|
assert.NoError(t, err)
|
2020-04-15 13:12:43 +02:00
|
|
|
|
|
|
|
theType, ok := maven.(*Maven)
|
|
|
|
assert.True(t, ok)
|
2020-06-11 14:02:54 +02:00
|
|
|
assert.Equal(t, "pom.xml", theType.options.PomPath)
|
|
|
|
assert.Equal(t, opts.ProjectSettingsFile, theType.options.ProjectSettingsFile)
|
|
|
|
assert.Equal(t, opts.GlobalSettingsFile, theType.options.GlobalSettingsFile)
|
|
|
|
assert.Equal(t, opts.M2Path, theType.options.M2Path)
|
2020-04-03 16:34:40 +02:00
|
|
|
assert.Equal(t, "maven", maven.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
t.Run("mta", func(t *testing.T) {
|
|
|
|
mta, err := GetArtifact("mta", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := mta.(*YAMLfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "mta.yaml", theType.path)
|
|
|
|
assert.Equal(t, "version", theType.versionField)
|
|
|
|
assert.Equal(t, "semver2", mta.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
t.Run("npm", func(t *testing.T) {
|
2020-04-15 13:12:43 +02:00
|
|
|
npm, err := GetArtifact("npm", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
assert.NoError(t, err)
|
2021-03-26 10:28:40 +02:00
|
|
|
|
|
|
|
theType, ok := npm.(*JSONfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "package.json", theType.path)
|
|
|
|
assert.Equal(t, "version", theType.versionField)
|
|
|
|
assert.Equal(t, "semver2", npm.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("yarn", func(t *testing.T) {
|
|
|
|
npm, err := GetArtifact("yarn", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2020-04-15 13:12:43 +02:00
|
|
|
|
|
|
|
theType, ok := npm.(*JSONfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "package.json", theType.path)
|
|
|
|
assert.Equal(t, "version", theType.versionField)
|
2020-04-03 16:34:40 +02:00
|
|
|
assert.Equal(t, "semver2", npm.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
t.Run("pip", func(t *testing.T) {
|
|
|
|
fileExists = func(string) (bool, error) { return true, nil }
|
|
|
|
pip, err := GetArtifact("pip", "", &Options{}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-05-25 19:48:59 +02:00
|
|
|
theType, ok := pip.(*Pip)
|
2020-04-15 13:12:43 +02:00
|
|
|
assert.True(t, ok)
|
2021-02-03 15:52:48 +02:00
|
|
|
assert.Equal(t, "setup.py", theType.path)
|
2020-04-15 13:12:43 +02:00
|
|
|
assert.Equal(t, "pep440", pip.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("pip - error", func(t *testing.T) {
|
|
|
|
fileExists = func(string) (bool, error) { return false, nil }
|
|
|
|
_, err := GetArtifact("pip", "", &Options{}, nil)
|
|
|
|
|
2021-02-03 15:52:48 +02:00
|
|
|
assert.EqualError(t, err, "no build descriptor available, supported: [setup.py version.txt VERSION]")
|
2020-04-15 13:12:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sbt", func(t *testing.T) {
|
2020-09-29 12:44:31 +02:00
|
|
|
fileExists = func(string) (bool, error) { return true, nil }
|
2020-04-15 13:12:43 +02:00
|
|
|
sbt, err := GetArtifact("sbt", "", &Options{VersionField: "theversion"}, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
theType, ok := sbt.(*JSONfile)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "sbtDescriptor.json", theType.path)
|
|
|
|
assert.Equal(t, "version", theType.versionField)
|
|
|
|
assert.Equal(t, "semver2", sbt.VersioningScheme())
|
|
|
|
})
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
t.Run("not supported build tool", func(t *testing.T) {
|
|
|
|
_, err := GetArtifact("nosupport", "whatever", &Options{}, nil)
|
|
|
|
assert.EqualError(t, err, "build tool 'nosupport' not supported")
|
|
|
|
})
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
|
|
|
|
func TestCustomArtifact(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
file string
|
|
|
|
field string
|
|
|
|
section string
|
2020-04-24 20:52:16 +02:00
|
|
|
scheme string
|
2020-04-15 13:12:43 +02:00
|
|
|
expected Artifact
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{file: "not.supported", expectedErr: "file type not supported: 'not.supported'"},
|
|
|
|
{file: "test.cfg", field: "testField", section: "testSection", expected: &INIfile{path: "test.cfg", versionField: "testField", versionSection: "testSection"}},
|
|
|
|
{file: "test.ini", field: "testField", section: "testSection", expected: &INIfile{path: "test.ini", versionField: "testField", versionSection: "testSection"}},
|
2020-04-24 20:52:16 +02:00
|
|
|
{file: "test.ini", field: "testField", section: "testSection", scheme: "maven", expected: &INIfile{path: "test.ini", versionField: "testField", versionSection: "testSection", versioningScheme: "maven"}},
|
2020-04-15 13:12:43 +02:00
|
|
|
{file: "test.json", field: "testField", expected: &JSONfile{path: "test.json", versionField: "testField"}},
|
|
|
|
{file: "test.yaml", field: "testField", expected: &YAMLfile{path: "test.yaml", versionField: "testField"}},
|
|
|
|
{file: "test.yml", field: "testField", expected: &YAMLfile{path: "test.yml", versionField: "testField"}},
|
|
|
|
{file: "test.txt", expected: &Versionfile{path: "test.txt"}},
|
|
|
|
{file: "test", expected: &Versionfile{path: "test"}},
|
2020-04-24 20:52:16 +02:00
|
|
|
{file: "test", scheme: "maven", expected: &Versionfile{path: "test", versioningScheme: "maven"}},
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tt {
|
2021-06-11 16:17:40 +02:00
|
|
|
t.Run(test.file, func(t *testing.T) {
|
|
|
|
res, err := customArtifact(test.file, test.field, test.section, test.scheme)
|
|
|
|
if len(test.expectedErr) == 0 {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.expected, res)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedErr)
|
|
|
|
}
|
|
|
|
})
|
2020-04-15 13:12:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|