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
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

247 lines
6.4 KiB
Go

//go:build unit
// +build unit
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")
})
}