Skip to content

Commit

Permalink
issue #360 wrong version compare of requirements (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgn42 authored Oct 23, 2024
1 parent 68f1a10 commit 45e8b39
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
6 changes: 1 addition & 5 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,7 @@ func (p *PackType) loadDependencies() error {
// Need to convert the spec <package/requirements/packages> "version" to the one internally used
if version != "latest" {
if len(strings.Split(version, ":")) >= 1 {
if string(version[len(version)-1]) == "_" {
pack.versionModifier = utils.GreaterVersion
} else {
pack.versionModifier = utils.RangeVersion
}
pack.versionModifier = utils.RangeVersion
} else {
pack.versionModifier = utils.ExactVersion
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,9 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType, noLocal bool) bo
}

if pack.versionModifier == utils.RangeVersion {
log.Debugf("Checking for installed packs %s", pack.Version)
for _, version := range installedVersions {
log.Debugf("- checking against: %s", version)
if utils.SemverCompareRange(version, pack.Version) == 0 {
return true
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/utils/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func stripLeadingZeros(version string) string {
// SemverCompare extends `semver.Compare` to work with leading zeros
func SemverCompare(version1, version2 string) int {
version1 = "v" + stripLeadingZeros(version1)
version1, _, _ = strings.Cut(version1, ":")
version2 = "v" + stripLeadingZeros(version2)
version2, _, _ = strings.Cut(version2, ":")
return semver.Compare(version1, version2)
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/utils/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func TestSemverCompare(t *testing.T) {
assert.True(utils.SemverCompare(version1, version3) < 0)
assert.True(utils.SemverCompare(version2, version3) < 0)
})
t.Run("test comparing two versions with range suffix", func(t *testing.T) {
version1 := "1.2.3"
version2 := "1.2.4:2.3.4"
assert.True(utils.SemverCompare(version1, version2) < 0)
assert.True(utils.SemverCompare(version2, version1) > 0)
})

}

func TestSemverMajor(t *testing.T) {
Expand All @@ -58,6 +65,18 @@ func TestSemverMajor(t *testing.T) {
assert.Equal("1", utils.SemverMajor("1.02.03"))
})
}

func TestSemverMajorMinor(t *testing.T) {
assert := assert.New(t)

t.Run("test major minor version", func(t *testing.T) {
assert.Equal("1.2", utils.SemverMajorMinor("1.2.3"))
assert.Equal("1.2", utils.SemverMajorMinor("01.2.3"))
assert.Equal("1.2", utils.SemverMajorMinor("01.02.03"))
assert.Equal("1.2", utils.SemverMajorMinor("1.02.03"))
})
}

func TestSemverStripMeta(t *testing.T) {
assert := assert.New(t)

Expand Down
7 changes: 4 additions & 3 deletions cmd/xml/pdsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (p *PdscXML) Dependencies() [][]string {
if p.RequirementsTag.Packages == nil {
return nil
}
for i, pack := range p.RequirementsTag.Packages {
for _, pack := range p.RequirementsTag.Packages {
for _, pk := range pack.Packages {
// empty -> install the latest
if pk.Version == "" {
Expand All @@ -160,8 +160,9 @@ func (p *PdscXML) Dependencies() [][]string {
pk.Version = pk.Version + ":_"
}
}
dependencies = append(dependencies, []string{pk.Name, pk.Vendor, pk.Version})
log.Debugf("found %v dependency", dependencies[i])
dependency := []string{pk.Name, pk.Vendor, pk.Version}
log.Debugf("found %v dependency", dependency)
dependencies = append(dependencies, dependency)
}
}
return dependencies
Expand Down
67 changes: 67 additions & 0 deletions cmd/xml/pdsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,71 @@ func TestPdscXML(t *testing.T) {
assert.Equal(expectedURL, pdscXML.PackURL(""))
assert.Equal(expectedURL, pdscXML.PackURL(version))
})

t.Run("test Dependencies1", func(t *testing.T) {
var name = "TheName"
var vendor = "TheVendor"
var version = "0.0.1"
pdscXML := xml.PdscXML{}
pack := xml.PackageTag{
Vendor: vendor,
Name: name,
Version: version,
}
packs := xml.PackagesTag{}
packs.Packages = append(packs.Packages, pack)
pdscXML.RequirementsTag.Packages = append(pdscXML.RequirementsTag.Packages, packs)

expectedDeps := [][]string{}
expectedDep := []string{name, vendor, version + ":_"}
expectedDeps = append(expectedDeps, expectedDep)
assert.Equal(expectedDeps, pdscXML.Dependencies())
})

t.Run("test Dependencies2", func(t *testing.T) {
var name = "TheName"
var vendor = "TheVendor"
var version = "0.0.1:0.0.2"
pdscXML := xml.PdscXML{}
pack := xml.PackageTag{
Vendor: vendor,
Name: name,
Version: version,
}
packs := xml.PackagesTag{}
packs.Packages = append(packs.Packages, pack)
pdscXML.RequirementsTag.Packages = append(pdscXML.RequirementsTag.Packages, packs)

expectedDeps := [][]string{}
expectedDep := []string{name, vendor, version}
expectedDeps = append(expectedDeps, expectedDep)
assert.Equal(expectedDeps, pdscXML.Dependencies())
})

t.Run("test Dependencies3", func(t *testing.T) {
var name = "TheName"
var vendor = "TheVendor"
var version = ""
pdscXML := xml.PdscXML{}
pack := xml.PackageTag{
Vendor: vendor,
Name: name,
Version: version,
}
packs := xml.PackagesTag{}
packs.Packages = append(packs.Packages, pack)
pdscXML.RequirementsTag.Packages = append(pdscXML.RequirementsTag.Packages, packs)

expectedDeps := [][]string{}
expectedDep := []string{name, vendor, "latest"}
expectedDeps = append(expectedDeps, expectedDep)
assert.Equal(expectedDeps, pdscXML.Dependencies())
})

t.Run("test Dependencies4", func(t *testing.T) {
pdscXML := xml.PdscXML{}

pdscXML.RequirementsTag.Packages = nil
assert.Nil(pdscXML.Dependencies())
})
}

0 comments on commit 45e8b39

Please sign in to comment.