1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/versioning/versioning_test.go

205 lines
6.8 KiB
Go
Raw Normal View History

package versioning
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetArtifact(t *testing.T) {
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)
assert.Equal(t, "docker", docker.VersioningScheme())
})
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())
})
t.Run("golang", func(t *testing.T) {
fileExists = func(string) (bool, error) { return true, nil }
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())
})
t.Run("golang - error", func(t *testing.T) {
fileExists = func(string) (bool, error) { return false, nil }
_, err := GetArtifact("golang", "", &Options{}, nil)
Whitesource scan (MVP) (#1658) * Whitesource MVP for Gradle, Golang, and NPM/Yarn * Refactoring * Refactor and cleanup, better error checking * publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support * Run gofmt and cleanup comments * Resolve PR comments * Update resources/metadata/whitesource.yaml Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Only determine project coordinates if they are missing Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> * Gradle versioning artifact * fix gradle artifact version regexp and refactor * Fix token extraction from output buffer * Fix some issues with pip and jsonfile versioning logic * Remove useless spacing * Remove unnecessary test file and fix naming style for JSONDescriptor * Automatically download wss-unified-agent if file does not exist * adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring * adds config.ReportDirectoryName, improves readability * Version-wide reporting for vulnerabilities and list of libraries. * Refactor and improve build accuracy * fix sed command * Add includes file pattern config option * Adds --exclude command line flag * run go mod tidy and regenerate step framework * Fix unit tests * revert changes * poll project status before downloading reports * merge with master * go mod tidy, go fmt, and fix whitesource unit test * sync go.mod * sync go.mod again Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
assert.EqualError(t, err, "no build descriptor available, supported: [VERSION version.txt go.mod]")
})
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())
})
t.Run("maven", func(t *testing.T) {
opts := Options{
ProjectSettingsFile: "projectsettings.xml",
GlobalSettingsFile: "globalsettings.xml",
M2Path: "m2/path",
}
maven, err := GetArtifact("maven", "", &opts, nil)
assert.NoError(t, err)
theType, ok := maven.(*Maven)
assert.True(t, ok)
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)
assert.Equal(t, "maven", maven.VersioningScheme())
})
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())
})
t.Run("npm", func(t *testing.T) {
npm, err := GetArtifact("npm", "", &Options{VersionField: "theversion"}, nil)
assert.NoError(t, err)
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)
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("pip", func(t *testing.T) {
fileExists = func(string) (bool, error) { return true, nil }
pip, err := GetArtifact("pip", "", &Options{}, nil)
assert.NoError(t, err)
theType, ok := pip.(*Pip)
assert.True(t, ok)
feat(whitesourceExecuteScan): UA for all build tools, e.g. maven & npm (#2501) * feat(whitesource): add config helper this helps to ease & enforce config settings * fix accidential change of class * add todos wrt java download * use existing scanOptions, add option to download jre * update generation * fix generation * allow running UA via go library * correct image, improve logging * add removal of downloaded JVM * update java creation and deletion * refactor and add log output * remove obsolete ToDo * increase test coverage * increase test coverage * adding aliases and tests * make go modules as default * maven: update behavior of projectNaming * add Docker capabilities * correct parameter name * retrieve Docker coordinates * docker coordinates only to provide artifact * add ToDos * add mta capability * add aliases, mvn arguments for settings * clean up groovy part * update defaults * add container for pip * add defaults, add maven specifics, ... * properly download settings * maven: check existence of excluded files * fix reporting * Update CommonStepsTest.groovy * update comment * fix CodeClimate finding * add tests for pip & fix minor issues * fix order of pip build descriptors * update pip container options * fix pip virtualEnv parameter * update report permissions * fix test * update container options * add use fileUtils to load properties file * update parameter description * adding Docker scanning defaults * clean up configHelper * consider also npm tool cache * add todos
2021-02-03 15:52:48 +02:00
assert.Equal(t, "setup.py", theType.path)
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)
feat(whitesourceExecuteScan): UA for all build tools, e.g. maven & npm (#2501) * feat(whitesource): add config helper this helps to ease & enforce config settings * fix accidential change of class * add todos wrt java download * use existing scanOptions, add option to download jre * update generation * fix generation * allow running UA via go library * correct image, improve logging * add removal of downloaded JVM * update java creation and deletion * refactor and add log output * remove obsolete ToDo * increase test coverage * increase test coverage * adding aliases and tests * make go modules as default * maven: update behavior of projectNaming * add Docker capabilities * correct parameter name * retrieve Docker coordinates * docker coordinates only to provide artifact * add ToDos * add mta capability * add aliases, mvn arguments for settings * clean up groovy part * update defaults * add container for pip * add defaults, add maven specifics, ... * properly download settings * maven: check existence of excluded files * fix reporting * Update CommonStepsTest.groovy * update comment * fix CodeClimate finding * add tests for pip & fix minor issues * fix order of pip build descriptors * update pip container options * fix pip virtualEnv parameter * update report permissions * fix test * update container options * add use fileUtils to load properties file * update parameter description * adding Docker scanning defaults * clean up configHelper * consider also npm tool cache * add todos
2021-02-03 15:52:48 +02:00
assert.EqualError(t, err, "no build descriptor available, supported: [setup.py version.txt VERSION]")
})
t.Run("sbt", func(t *testing.T) {
fileExists = func(string) (bool, error) { return true, nil }
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())
})
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")
})
}
func TestCustomArtifact(t *testing.T) {
tt := []struct {
file string
field string
section string
scheme string
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"}},
{file: "test.ini", field: "testField", section: "testSection", scheme: "maven", expected: &INIfile{path: "test.ini", versionField: "testField", versionSection: "testSection", versioningScheme: "maven"}},
{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"}},
{file: "test", scheme: "maven", expected: &Versionfile{path: "test", versioningScheme: "maven"}},
}
for _, test := range tt {
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)
}
})
}
}