Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type Constraint struct {
original string
}

// Constraints is a slice of constraints. We make a custom type so that
// we can add methods to it.
type Constraints []*Constraint
// Constraints is a 2D slice of constraints. We make a custom type so
// that we can add methods to it.
type Constraints [][]*Constraint

type constraintFunc func(v, c *Version) bool

Expand Down Expand Up @@ -49,42 +49,60 @@ func init() {
}

// NewConstraint will parse one or more constraints from the given
// constraint string. The string must be a comma-separated list of
// constraints.
func NewConstraint(v string) (Constraints, error) {
vs := strings.Split(v, ",")
result := make([]*Constraint, len(vs))
for i, single := range vs {
c, err := parseSingle(single)
if err != nil {
return nil, err
// constraint string. The string must be a comma or pipe separated
// list of constraints.
func NewConstraint(cs string) (Constraints, error) {
ors := strings.Split(cs, "||")
or := make([][]*Constraint, len(ors))
for k, v := range ors {
vs := strings.Split(v, ",")
result := make([]*Constraint, len(vs))
for i, single := range vs {
c, err := parseSingle(single)
if err != nil {
return nil, err
}

result[i] = c
}

result[i] = c
or[k] = result
}

return Constraints(result), nil
return Constraints(or), nil
}

// Check tests if a version satisfies all the constraints.
func (cs Constraints) Check(v *Version) bool {
for _, c := range cs {
if !c.Check(v) {
return false
for _, o := range cs {
ok := true
for _, c := range o {
if !c.Check(v) {
ok = false
break
}
}

if ok {
return true
}
}

return true
return false
}

// Returns the string format of the constraints
func (cs Constraints) String() string {
csStr := make([]string, len(cs))
for i, c := range cs {
csStr[i] = c.String()
orStr := make([]string, len(cs))
for i, o := range cs {
csStr := make([]string, len(o))
for j, c := range o {
csStr[j] = c.String()
}

orStr[i] = strings.Join(csStr, ",")
}

return strings.Join(csStr, ",")
return strings.Join(orStr, "||")
}

// Check tests if a constraint is validated by the given version.
Expand Down
30 changes: 23 additions & 7 deletions constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
func TestNewConstraint(t *testing.T) {
cases := []struct {
input string
ors int
count int
err bool
}{
{">= 1.2", 1, false},
{"1.0", 1, false},
{">= 1.x", 0, true},
{">= 1.2, < 1.0", 2, false},
{">= 1.2", 1, 1, false},
{"1.0", 1, 1, false},
{">= 1.x", 0, 0, true},
{">= 1.2, < 1.0", 1, 2, false},
{">= 1.2, < 1.0 || ~> 2.0, < 3", 2, 2, false},

// Out of bounds
{"11387778780781445675529500000000000000000", 0, true},
{"11387778780781445675529500000000000000000", 0, 0, true},
}

for _, tc := range cases {
Expand All @@ -26,10 +28,20 @@ func TestNewConstraint(t *testing.T) {
} else if !tc.err && err != nil {
t.Fatalf("error for input %s: %s", tc.input, err)
}
if tc.err {
continue
}

actual := len(v)
if actual != tc.ors {
t.Fatalf("input: %s\nexpected ors: %d\nactual: %d",
tc.input, tc.ors, actual)
}

if len(v) != tc.count {
actual = len(v[0])
if actual != tc.count {
t.Fatalf("input: %s\nexpected len: %d\nactual: %d",
tc.input, tc.count, len(v))
tc.input, tc.count, actual)
}
}
}
Expand All @@ -42,6 +54,9 @@ func TestConstraintCheck(t *testing.T) {
}{
{">= 1.0, < 1.2", "1.1.5", true},
{"< 1.0, < 1.2", "1.1.5", false},
{">= 1.0, < 1.2 || > 1.3", "1.1.5", true},
{">= 1.0, < 1.2 || > 1.3", "1.3.2", true},
{">= 1.0, < 1.2 || > 1.3", "1.2.3", false},
{"= 1.0", "1.1.5", false},
{"= 1.0", "1.0.0", true},
{"1.0", "1.0.0", true},
Expand Down Expand Up @@ -104,6 +119,7 @@ func TestConstraintsString(t *testing.T) {
}{
{">= 1.0, < 1.2", ""},
{"~> 1.0.7", ""},
{">= 1.0, < 1.2 || > 1.3", ""},
}

for _, tc := range cases {
Expand Down