Browse Source

Version matching; Fixed epoch bug

v0
maze 3 years ago
parent
commit
fc05fa8ef8
3 changed files with 58 additions and 8 deletions
  1. +23
    -5
      version/dependency.go
  2. +25
    -0
      version/dependency_test.go
  3. +10
    -3
      version/version.go

+ 23
- 5
version/dependency.go View File

@ -85,15 +85,33 @@ func ParseDependency(s string) (string, Dependency, error) {
if dep.Operator, ends, err = parseOperator(ops, false); err != nil {
return "", Dependency{}, err
}
if strings.ContainsAny(ops[ends:], ":-") {
dep.Version = Parse(ops[ends:])
} else {
dep.Version.Version = ops[ends:]
}
dep.Version = Parse(ops[ends:])
}
return name, dep, nil
}
// Match this dependency against a version
func (dep Dependency) Match(version Version) bool {
if dep.Operator == None {
return true
}
cmp := Compare(version, dep.Version)
switch dep.Operator {
case LessThan:
return cmp == -1
case LessThenOrEqualTo:
return cmp <= 0
case EqualTo:
return cmp == 0
case MoreThanOrEqualTo:
return cmp >= 0
case MoreThan:
return cmp == 1
default:
return false
}
}
func (dep Dependency) String() string {
if dep.Operator == None {
return ""


+ 25
- 0
version/dependency_test.go View File

@ -69,3 +69,28 @@ func TestDependency(t *testing.T) {
})
}
}
func TestDependencyMatch(t *testing.T) {
_, dep, err := ParseDependency(">=1.2")
if err != nil {
t.Fatal(err)
}
var (
testsa = []string{"1.2", "1.2.1", "2"}
testsb = []string{"1.1", "1.0", "0", "1.alpha"}
)
for _, test := range testsa {
t.Run(test+dep.String(), func(t *testing.T) {
if !dep.Match(Parse(test)) {
t.Fatalf(`expected %q to match >=1.2`, test)
}
})
}
for _, test := range testsb {
t.Run("!"+test+dep.String(), func(t *testing.T) {
if dep.Match(Parse(test)) {
t.Fatalf(`expected %q to not match >=1.2`, test)
}
})
}
}

+ 10
- 3
version/version.go View File

@ -1,3 +1,9 @@
/*
Package version implements version and dependency parsing
The version sorting algorithm is as implemented by the RedHat Package Manager
(RPM) algorithm.
*/
package version
import (
@ -31,10 +37,10 @@ func Parse(evr string) Version {
if i := strings.IndexByte(evr, ':'); i != -1 {
epoch, evr = evr[:i], evr[i+1:]
} else {
epoch = "0"
}
if epoch == "0" {
epoch = ""
}
if i := strings.IndexByte(evr, '-'); i != -1 {
version, release = evr[:i], evr[i+1:]
} else {
@ -51,6 +57,7 @@ func (v Version) LessThan(other Version) bool {
var alphanumPattern = regexp.MustCompile("([a-zA-Z]+)|([0-9]+)|(~)")
// rpmvercmp implements https://github.com/rpm-software-management/rpm/blob/master/lib/rpmvercmp.c
func rpmvercmp(a, b string) int {
// shortcut for equality
if a == b {


Loading…
Cancel
Save