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/pip_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

244 lines
6.3 KiB
Go

package versioning
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestPipGetVersion(t *testing.T) {
t.Parallel()
t.Run("success case - setup.py", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",version="1.2.3"`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
version, err := pip.GetVersion()
assert.NoError(t, err)
assert.Equal(t, "1.2.3", version)
})
t.Run("success case - setup.py & version.txt", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
fileUtils.AddFile("version.txt", []byte(`1.2.4`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
version, err := pip.GetVersion()
assert.NoError(t, err)
assert.Equal(t, "1.2.4", version)
})
t.Run("success case - setup.py & VERSION", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
fileUtils.AddFile("VERSION", []byte(`1.2.5`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
version, err := pip.GetVersion()
assert.NoError(t, err)
assert.Equal(t, "1.2.5", version)
})
t.Run("error to read file", func(t *testing.T) {
fileUtils := mock.FilesMock{}
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
_, err := pip.GetVersion()
assert.Contains(t, fmt.Sprint(err), "failed to read file 'setup.py'")
})
t.Run("error to retrieve version", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
_, err := pip.GetVersion()
assert.Contains(t, fmt.Sprint(err), "failed to retrieve version")
})
}
func TestPipSetVersion(t *testing.T) {
t.Run("success case - setup.py", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",version="1.2.3"`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
err := pip.SetVersion("2.0.0")
assert.NoError(t, err)
content, _ := fileUtils.FileRead("setup.py")
assert.Contains(t, string(content), `version="2.0.0"`)
})
t.Run("success case - setup.py & version.txt", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
fileUtils.AddFile("version.txt", []byte(`1.2.3`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
err := pip.SetVersion("2.0.0")
assert.NoError(t, err)
content, _ := fileUtils.FileRead("version.txt")
assert.Equal(t, "2.0.0", string(content))
})
t.Run("success case - setup.py & VERSION", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
fileUtils.AddFile("VERSION", []byte(`1.2.3`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
err := pip.SetVersion("2.0.0")
assert.NoError(t, err)
content, _ := fileUtils.FileRead("VERSION")
assert.Equal(t, "2.0.0", string(content))
})
t.Run("error to read file", func(t *testing.T) {
fileUtils := mock.FilesMock{}
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
err := pip.SetVersion("2.0.0")
assert.Contains(t, fmt.Sprint(err), "failed to read file 'setup.py'")
})
t.Run("error to retrieve version", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
err := pip.SetVersion("2.0.0")
assert.Contains(t, fmt.Sprint(err), "failed to retrieve version")
})
}
func TestPipGetCoordinates(t *testing.T) {
t.Run("success case - setup.py", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python",version="1.2.3"`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
coordinates, err := pip.GetCoordinates()
assert.NoError(t, err)
assert.Equal(t, "simple-python", coordinates.ArtifactID)
assert.Equal(t, "1.2.3", coordinates.Version)
})
t.Run("success case - only version", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(version="1.2.3"`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
coordinates, err := pip.GetCoordinates()
assert.NoError(t, err)
assert.Equal(t, "", coordinates.ArtifactID)
assert.Equal(t, "1.2.3", coordinates.Version)
})
t.Run("error to retrieve setup.py", func(t *testing.T) {
fileUtils := mock.FilesMock{}
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
_, err := pip.GetCoordinates()
assert.Contains(t, fmt.Sprint(err), "failed to read file 'setup.py'")
})
t.Run("error to retrieve version", func(t *testing.T) {
fileUtils := mock.FilesMock{}
fileUtils.AddFile("setup.py", []byte(`setup(name="simple-python"`))
pip := Pip{
path: "setup.py",
fileExists: fileUtils.FileExists,
readFile: fileUtils.FileRead,
writeFile: fileUtils.FileWrite,
}
_, err := pip.GetCoordinates()
assert.Contains(t, fmt.Sprint(err), "failed to retrieve version")
})
}