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 a104b2a06d
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 14:52:48 +01:00

246 lines
6.4 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()
pipDescriptor := coordinates.(*PipDescriptor)
assert.NoError(t, err)
assert.Equal(t, "simple-python", pipDescriptor.ArtifactID)
assert.Equal(t, "1.2.3", pipDescriptor.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()
pipDescriptor := coordinates.(*PipDescriptor)
assert.NoError(t, err)
assert.Equal(t, "", pipDescriptor.ArtifactID)
assert.Equal(t, "1.2.3", pipDescriptor.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")
})
}