Skip to content

Commit

Permalink
[cpackget] must strip meta data from version #268 (#275)
Browse files Browse the repository at this point in the history
additional bugfixes + tests
  • Loading branch information
edriouk authored Mar 20, 2024
1 parent 99532aa commit 09b0b17
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

(C) 2021-2023 Linaro, 2023 Arm Ltd.
(C) 2021-2023 Linaro, 2024 Arm Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 10 additions & 5 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *PackType) purge() error {

fileNamePattern := p.Vendor + "\\." + p.Name
if len(p.Version) > 0 {
fileNamePattern += "\\." + p.Version
fileNamePattern += "\\." + p.GetVersionNoMeta() + ".*"
} else {
fileNamePattern += "\\..*?"
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
return err
}

packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersion())
packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersionNoMeta())
packBackupPath := filepath.Join(Installation.DownloadDir, p.PackFileName())

if len(p.Pdsc.License) > 0 {
Expand Down Expand Up @@ -365,7 +365,7 @@ func (p *PackType) uninstall(installation *PacksInstallationType) error {
log.Debugf("Uninstalling \"%v\"", p.path)

// Remove Vendor/Pack/x.y.z
packPath := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersion())
packPath := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersionNoMeta())
if err := os.RemoveAll(packPath); err != nil {
return err
}
Expand Down Expand Up @@ -609,7 +609,7 @@ func (p *PackType) PackID() string {

// PackIDWithVersion returns the packID with version: Vendor.PackName.x.y.z
func (p *PackType) PackIDWithVersion() string {
return p.PackID() + "." + utils.SemverStripMeta(p.GetVersion())
return p.PackID() + "." + p.GetVersionNoMeta()
}

// PackFileName returns a string with how the pack file name would be: Vendor.PackName.x.y.z.pack
Expand All @@ -636,10 +636,15 @@ func (p *PackType) GetVersion() string {
return p.Version
}

// GetVersionNoMeta return the version without meta information
func (p *PackType) GetVersionNoMeta() string {
return utils.SemverStripMeta(p.GetVersion())
}

// toggleReadOnly will be used by Lock() and Unlock() to set or unset Read-Only flag on all pack files
func (p *PackType) toggleReadOnly(setReadOnly bool) {
// Vendor/Pack/x.y.z/
packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, utils.SemverStripMeta(p.GetVersion()))
packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersionNoMeta())

// .Download/Vendor.Pack.z.y.z.pack
packBackupPath := filepath.Join(Installation.DownloadDir, p.PackFileName())
Expand Down
19 changes: 7 additions & 12 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func AddPack(packPath string, checkEula, extractEula, forceReinstall, noRequirem
log.Debugf("Making temporary backup of pack \"%s\"", packPath)

// Get target pack's full path and move it to a temporary "_tmp" directory
fullPackPath = filepath.Join(Installation.PackRoot, pack.Vendor, pack.Name, pack.Version)
fullPackPath = filepath.Join(Installation.PackRoot, pack.Vendor, pack.Name, pack.GetVersionNoMeta())
backupPackPath = fullPackPath + "_tmp"

if err := utils.MoveFile(fullPackPath, backupPackPath); err != nil {
Expand All @@ -101,7 +101,7 @@ func AddPack(packPath string, checkEula, extractEula, forceReinstall, noRequirem
log.Debugf("Moved pack to temporary path \"%s\"", backupPackPath)
dropPreInstalled = true
} else {
log.Errorf("Pack \"%s\" is already installed here: \"%s\", use the --force-reinstall (-F) flag to force installation", packPath, filepath.Join(Installation.PackRoot, pack.Vendor, pack.Name, pack.GetVersion()))
log.Errorf("Pack \"%s\" is already installed here: \"%s\", use the --force-reinstall (-F) flag to force installation", packPath, filepath.Join(Installation.PackRoot, pack.Vendor, pack.Name, pack.GetVersionNoMeta()))
return nil
}
}
Expand Down Expand Up @@ -990,7 +990,7 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType) bool {

// Exact version is easy, just find a matching installation folder
if pack.versionModifier == utils.ExactVersion {
packDir := filepath.Join(installationDir, pack.Version)
packDir := filepath.Join(installationDir, pack.GetVersionNoMeta())
log.Debugf("Checking if \"%s\" exists", packDir)
return utils.DirExists(packDir)
}
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType) bool {
log.Debugf("Checking for installed packs >=%s", pack.Version)
for _, version := range installedVersions {
log.Debugf("- checking if %s >= %s", version, pack.Version)
if semver.Compare("v"+version, "v"+pack.Version) >= 0 {
if utils.SemverCompare(version, pack.Version) >= 0 {
log.Debugf("- found newer version %s", version)
pack.targetVersion = version
return true
Expand All @@ -1040,7 +1040,7 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType) bool {
for _, version := range installedVersions {
log.Debugf("- checking against: %s", version)
sameMajor := semver.Major("v"+version) == semver.Major("v"+pack.Version)
if sameMajor && semver.Compare("v"+version, "v"+pack.Version) >= 0 {
if sameMajor && utils.SemverCompare(version, pack.Version) >= 0 {
pack.targetVersion = version
return true
}
Expand All @@ -1050,14 +1050,9 @@ func (p *PacksInstallationType) PackIsInstalled(pack *PackType) bool {
}

if pack.versionModifier == utils.RangeVersion {
minVersion := strings.Split(pack.Version, ":")[0]
maxVersion := strings.Split(pack.Version, ":")[1]
for _, version := range installedVersions {
if semver.Compare("v"+minVersion, "v"+version) <= 0 {
if (maxVersion == "_") || (semver.Compare("v"+version, "v"+maxVersion) <= 0) {
pack.targetVersion = version
return true
}
if utils.SemverCompareRange(version, pack.Version) == 0 {
return true
}
}
return false
Expand Down
5 changes: 3 additions & 2 deletions cmd/installer/root_pack_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ func TestAddPack(t *testing.T) {
err = installer.AddPack(packPath, !CheckEula, !ExtractEula, !ForceReinstall, !NoRequirements, Timeout)
assert.Nil(err)

pack.Version = "1.2.3"
pack.Version = "1.2.3+metaInjected"
pack.IsPublic = true
checkPackIsInstalled(t, pack)
})
Expand All @@ -717,7 +717,7 @@ func TestAddPack(t *testing.T) {
packInfo, err := utils.ExtractPackInfo(packPath)
assert.Nil(err)
pack := packInfoToType(packInfo)
pack.Version = "1.2.3"
pack.Version = "1.2.3+meta"

// Prep the pdsc file to go in .Web/
packPdscFilePath := filepath.Join(installer.Installation.WebDir, pack.PdscFileName())
Expand Down Expand Up @@ -916,6 +916,7 @@ func TestAddPack(t *testing.T) {

// Make sure there's no pack folder in Vendor/PackName/x.y.z/, Vendor/PackName/ and Vendor/
assert.False(utils.DirExists(filepath.Join(installer.Installation.PackRoot, pack.Vendor, pack.Name, pack.Version)))
assert.False(utils.DirExists(filepath.Join(installer.Installation.PackRoot, pack.Vendor, pack.Name, pack.GetVersionNoMeta())))
assert.False(utils.DirExists(filepath.Join(installer.Installation.PackRoot, pack.Vendor, pack.Name)))
assert.False(utils.DirExists(filepath.Join(installer.Installation.PackRoot, pack.Vendor)))

Expand Down
6 changes: 5 additions & 1 deletion cmd/utils/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import (
// to re-use `semver` Compare and Major functions
func stripLeadingZeros(version string) string {
regex := regexp.MustCompile(`\.0*(\d+)`)
version = strings.TrimLeft(version, "0")
version = regex.ReplaceAllString(version, ".$1")
version = strings.TrimLeft(version, "0") // trim leading zeros
if strings.HasPrefix(version, ".") {
// restore the only zero
version = "0" + version
}
return version
}

Expand Down
13 changes: 13 additions & 0 deletions cmd/utils/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func TestSemverCompare(t *testing.T) {
assert.True(utils.SemverCompare(version1, version2) < 0)
assert.True(utils.SemverCompare(version2, version1) > 0)
})
t.Run("test comparing two equal versions with and without leading zeros", func(t *testing.T) {
version1 := "01.02.03"
version2 := "1.2.3"
assert.True(utils.SemverCompare(version1, version2) == 0)
})
t.Run("test comparing two zero major versions with and without leading zeros", func(t *testing.T) {
version1 := "00.02.03"
version2 := "0.2.3"
version3 := "0.2.4"
assert.True(utils.SemverCompare(version1, version2) == 0)
assert.True(utils.SemverCompare(version1, version3) < 0)
assert.True(utils.SemverCompare(version2, version3) < 0)
})
}

func TestSemverMajor(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package main

var version string

const copyRight = "(C) 2021-2023 Linaro, 2023 Arm Ltd."
const copyRight = "(C) 2021-2023 Linaro, 2024 Arm Ltd."
Binary file added testdata/TheVendor.ThePack.1.2.3+meta.pack
Binary file not shown.
Binary file modified testdata/TheVendor.ThePack.1.2.3.pack
Binary file not shown.
Binary file modified testdata/devpack_Install/TheVendor.ThePack.1.2.3.pack
Binary file not shown.
Binary file modified testdata/integration/1.2.3/TheVendor.PublicLocalPack.1.2.3.pack
Binary file not shown.

0 comments on commit 09b0b17

Please sign in to comment.