Skip to content

Commit

Permalink
DoesRangeInclude function (#886)
Browse files Browse the repository at this point in the history
* added functionality to DoesRangeInclde + tests

Signed-off-by: Rebecca Metzman <[email protected]>

* style

Signed-off-by: Rebecca Metzman <[email protected]>

* adding sign-off

Signed-off-by: Rebecca Metzman <[email protected]>

* adding testing for errors

Signed-off-by: Rebecca Metzman <[email protected]>

* resolved comments

Signed-off-by: Rebecca Metzman <[email protected]>

---------

Signed-off-by: Rebecca Metzman <[email protected]>
Co-authored-by: Rebecca Metzman <[email protected]>
  • Loading branch information
rmetzman and Rebecca Metzman authored May 30, 2023
1 parent 7a87726 commit 0d557cb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/misc/depversion/depversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ func ParseVersionValue(s string) VersionValue {
return vv
}

// TODO: implement for more efficient traversal later
func DoesRangeInclude(versions []string, versionRange string) (bool, error) {
return false, fmt.Errorf("unimplemented")
versionMap, err := WhichVersionMatches(versions, versionRange)

if err != nil {
return false, fmt.Errorf("error for DoesRangeInclude %v", err)
}

for len(versionMap) > 0 {
return true, nil
}

return false, nil
}

type VersionRange struct {
Expand Down
56 changes: 56 additions & 0 deletions pkg/misc/depversion/depversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,59 @@ func Test_WhichVersionMatches(t *testing.T) {
})
}
}

func Test_DoesRangeInclude(t *testing.T) {
testCases := []struct {
versions []string
versionRange string
expect bool
}{
{
versionRange: ">=1.0,<=2.0",
versions: []string{"1.5"},
expect: true,
},
{
versionRange: ">=1.0,<=2.0",
versions: []string{"1.0", "2.0", "3.0"},
expect: true,
},
{
versionRange: ">=1.0,<=2.0",
versions: []string{"3.0", "2.1"},
expect: false,
},
{
versionRange: ">1.0,<2.0",
versions: []string{"3.0", "1.0", "2.0"},
expect: false,
},
}

for _, tt := range testCases {
t.Run(fmt.Sprintf("does range include %s", tt.versionRange), func(t *testing.T) {
got, err := DoesRangeInclude(tt.versions, tt.versionRange)
if err != nil {
t.Errorf("got err from DoesRangeInclude: %v", err)
return
}

if diff := cmp.Diff(tt.expect, got); len(diff) > 0 {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}

func Test_DoesRangeInclude_Errors(t *testing.T) {
res1, err1 := DoesRangeInclude([]string{"3.0", "1.0", "2.0"}, ">1.0 , <2.0")
res2, err2 := DoesRangeInclude([]string{"anythinggoes", "1.0", "2.0"}, "bad range")

if err1 != nil || err2 != nil {
t.Errorf("expected error for DoesRangeInclude and did not receive an error")
}

if res1 != false || res2 != false {
t.Errorf("expected error for DoesRangeInclude and did not receive false result as expected")
}
}

0 comments on commit 0d557cb

Please sign in to comment.