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/descriptorUtils_test.go
Oliver Nocon d47a17c8fc
feat(whitesource): consolidated reporting and versioning alignment (#2571)
* update reporting and add todo comments

* enhance reporting, allow directory creation for reports

* properly pass reports

* update templating and increase verbosity of errors

* add todo

* add detail table

* update sorting

* add test and improve error message

* fix error message in test

* extend tests

* enhance tests

* enhance versioning behavior accoring to #1846

* create markdown overview report

* small fix

* fix small issue

* make sure that report directory exists

* align reporting directory with default directory from UA

* add missing comments

* add policy check incl. tests

* enhance logging and tests

* update versioning to allow custom version usage properly

* fix report paths and golang image

* update styling of md

* update test
2021-02-10 16:18:00 +01:00

118 lines
4.6 KiB
Go

package versioning
import (
"testing"
"github.com/stretchr/testify/assert"
)
type mavenMock struct {
groupID string
artifactID string
version string
packaging string
}
func (m *mavenMock) VersioningScheme() string {
return "maven"
}
func (m *mavenMock) GetVersion() (string, error) {
return m.version, nil
}
func (m *mavenMock) SetVersion(v string) error {
m.version = v
return nil
}
func (m *mavenMock) GetCoordinates() (Coordinates, error) {
return Coordinates{GroupID: m.groupID, ArtifactID: m.artifactID, Version: m.version, Packaging: m.packaging}, nil
}
type pipMock struct {
artifactID string
version string
}
func (p *pipMock) VersioningScheme() string {
return "pep440"
}
func (p *pipMock) GetVersion() (string, error) {
return p.version, nil
}
func (p *pipMock) SetVersion(v string) error {
p.version = v
return nil
}
func (p *pipMock) GetCoordinates() (Coordinates, error) {
return Coordinates{ArtifactID: p.artifactID, Version: p.version}, nil
}
func TestDetermineProjectCoordinatesWithCustomVersion(t *testing.T) {
nameTemplate := `{{list .GroupID .ArtifactID | join "-" | trimAll "-"}}`
t.Run("default", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3"}).GetCoordinates()
name, version := DetermineProjectCoordinatesWithCustomVersion(nameTemplate, "major", "", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "1", version, "Expected different project version")
})
t.Run("custom", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3"}).GetCoordinates()
_, version := DetermineProjectCoordinatesWithCustomVersion(nameTemplate, "major", "customVersion", gav)
assert.Equal(t, "customVersion", version, "Expected different project version")
})
}
func TestDetermineProjectCoordinates(t *testing.T) {
nameTemplate := `{{list .GroupID .ArtifactID | join "-" | trimAll "-"}}`
t.Run("maven", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "major", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "1", version, "Expected different project version")
})
t.Run("maven major-minor", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "major-minor", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "1.2", version, "Expected different project version")
})
t.Run("maven full", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3-7864387648746"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "full", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "1.2.3-7864387648746", version, "Expected different project version")
})
t.Run("maven semantic", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "1.2.3-7864387648746"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "semantic", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "1.2.3", version, "Expected different project version")
})
t.Run("maven empty", func(t *testing.T) {
gav, _ := (&mavenMock{groupID: "com.test.pkg", artifactID: "analyzer", version: "0-SNAPSHOT"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "snapshot", gav)
assert.Equal(t, "com.test.pkg-analyzer", name, "Expected different project name")
assert.Equal(t, "", version, "Expected different project version")
})
t.Run("python", func(t *testing.T) {
gav, _ := (&pipMock{artifactID: "python-test", version: "2.2.3"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "major", gav)
assert.Equal(t, "python-test", name, "Expected different project name")
assert.Equal(t, "2", version, "Expected different project version")
})
t.Run("python semantic", func(t *testing.T) {
gav, _ := (&pipMock{artifactID: "python-test", version: "2.2.3.20200101"}).GetCoordinates()
name, version := DetermineProjectCoordinates(nameTemplate, "semantic", gav)
assert.Equal(t, "python-test", name, "Expected different project name")
assert.Equal(t, "2.2.3", version, "Expected different project version")
})
}