Compare commits

..

2 Commits

Author SHA1 Message Date
knoflook baa299faa8
test: increase code coverage from 62% to 99.5% 2021-10-11 14:36:12 +02:00
knoflook 054056163a
refactor!: create and use TagDelta struct et al
add a TagDelta struct with accompanying function String()
rename UpgradeElement() to UpgradeDelta() and change the return type
move UpgradeType() to be a function of TagDelta
add ParseDelta() that returns a TagDelta from string
2021-10-11 14:36:12 +02:00
4 changed files with 14 additions and 113 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
fmtcoverage.html

View File

@ -12,6 +12,7 @@ Package tagcmp provides image tag comparison operations\.
- [Variables](<#variables>)
- [func IsParsable(tag string) bool](<#func-isparsable>)
- [func UpgradeType(t Tag) int](<#func-upgradetype>)
- [type ByTagAsc](<#type-bytagasc>)
- [func (t ByTagAsc) Len() int](<#func-bytagasc-len>)
- [func (t ByTagAsc) Less(i, j int) bool](<#func-bytagasc-less>)
@ -27,11 +28,7 @@ Package tagcmp provides image tag comparison operations\.
- [func (t Tag) IsGreaterThan(tag Tag) bool](<#func-tag-isgreaterthan>)
- [func (t Tag) IsLessThan(tag Tag) bool](<#func-tag-islessthan>)
- [func (t Tag) String() string](<#func-tag-string>)
- [func (curTag Tag) UpgradeDelta(newTag Tag) (TagDelta, error)](<#func-tag-upgradedelta>)
- [type TagDelta](<#type-tagdelta>)
- [func ParseDelta(delta string) (TagDelta, error)](<#func-parsedelta>)
- [func (t TagDelta) String() string](<#func-tagdelta-string>)
- [func (d TagDelta) UpgradeType() int](<#func-tagdelta-upgradetype>)
- [func (curTag Tag) UpgradeElement(newTag Tag) (Tag, error)](<#func-tag-upgradeelement>)
## Variables
@ -74,6 +71,14 @@ func IsParsable(tag string) bool
IsParsable determines if a tag is supported by this library
## func UpgradeType
```go
func UpgradeType(t Tag) int
```
UpgradeType takes exit from UpgradeElemene and returns a numeric representation of upgrade or downgrade 1/\-1: patch 2/\-2: minor 4/\-4: major 0: no change
## type ByTagAsc
ByTagAsc sorts tags in ascending order where the last element is the latest tag\.
@ -135,9 +140,8 @@ type Tag struct {
MissingMinor bool // whether or not the minor semver part was left out
Patch string `json:",omitempty"` // patch semver part
MissingPatch bool // whether or not he patch semver part was left out
Suffix string // tag suffix (e.g. "-alpine") [would be release candidate in semver]
Suffix string // tag suffix (e.g. "-alpine")
UsesV bool // whether or not the tag uses the "v" prefix
Metadata string // metadata: what's after + and after the first "-"
}
```
@ -189,45 +193,13 @@ func (t Tag) String() string
String formats a Tag correctly in string representation
### func \(Tag\) UpgradeDelta
### func \(Tag\) UpgradeElement
```go
func (curTag Tag) UpgradeDelta(newTag Tag) (TagDelta, error)
func (curTag Tag) UpgradeElement(newTag Tag) (Tag, error)
```
UpgradeDelta returns a TagDelta object which is the difference between an old and new tag It can contain negative numbers if comparing with an older tag\.
## type TagDelta
```go
type TagDelta struct {
Major int // major semver difference
Minor int // minor semver difference
Patch int // patch semver difference
}
```
### func ParseDelta
```go
func ParseDelta(delta string) (TagDelta, error)
```
ParseDelta converts a tag difference in the format of X\, X\.Y or X\.Y\.Z where X\, Y\, Z are positive or negative integers or 0
### func \(TagDelta\) String
```go
func (t TagDelta) String() string
```
### func \(TagDelta\) UpgradeType
```go
func (d TagDelta) UpgradeType() int
```
UpgradeType takes exit from UpgradeElemene and returns a numeric representation of upgrade or downgrade 1/\-1: patch 2/\-2: minor 4/\-4: major 0: no change
UpgradeElement returns a Tag object which is the difference between an old and new tag It can contain negative numbers if comparing with an older tag\.

View File

@ -187,30 +187,6 @@ func (t Tag) IsCompatible(tag Tag) bool {
return true
}
// IsUpgradeCompatible chekcs if upTag is compatible with a pinned version tag.
// I.e. pinning to 22-fpm should return true if upTag is 22.2.0-fpm but not 22.2.0-alpine or 23.0.0-fpm
func (pin Tag) IsUpgradeCompatible(upTag Tag) bool {
if pin.Suffix != upTag.Suffix {
return false
}
if pin.Major != upTag.Major {
return false
}
if pin.MissingMinor {
return true
}
if pin.Minor != upTag.Minor {
return false
}
if pin.MissingPatch {
return true
}
if pin.Patch != upTag.Patch {
return false
}
return true
}
// UpgradeDelta returns a TagDelta object which is the difference between an old and new tag
// It can contain negative numbers if comparing with an older tag.
func (curTag Tag) UpgradeDelta(newTag Tag) (TagDelta, error) {

View File

@ -886,52 +886,6 @@ func TestDeltaString(t *testing.T) {
}
}
func TestIsUpgradeCompatible(t *testing.T) {
testsTrue := [][]string{
{"22-fpm", "22-fpm"},
{"22-fpm", "22.0-fpm"},
{"22-fpm", "22.0.0-fpm"},
{"22-fpm", "22.2.0-fpm"},
{"22-fpm", "22.0.0-fpm"},
{"22.2-fpm", "22.2-fpm"},
{"22.2-fpm", "22.2.0-fpm"},
{"22.2.2-fpm", "22.2.2-fpm"},
}
testsFalse := [][]string{
{"22-fpm", "22-alpine"},
{"22-fpm", "23-fpm"},
{"22-fpm", "21-fpm"},
{"22.2-fpm", "22.0.2-fpm"},
{"22.2.0-fpm", "22.2.2-fpm"},
}
for _, test := range testsTrue {
pin, err := tagcmp.Parse(test[0])
if err != nil {
t.Error(err)
}
upTag, err := tagcmp.Parse(test[1])
if err != nil {
t.Error(err)
}
if !pin.IsUpgradeCompatible(upTag) {
t.Errorf("pin %s should be upgradable to %s but returned false", test[0], test[1])
}
}
for _, test := range testsFalse {
pin, err := tagcmp.Parse(test[0])
if err != nil {
t.Error(err)
}
upTag, err := tagcmp.Parse(test[1])
if err != nil {
t.Error(err)
}
if pin.IsUpgradeCompatible(upTag) {
t.Errorf("pin %s should not be upgradable to %s but returned true", test[0], test[1])
}
}
}
func TestUpgradeDelta(t *testing.T) {
pairs := []struct {
t1 string