Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider compatible-with ^ constraint implicit if operator is missing #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A library for handling a superset of semantic versioning in golang.

## Documentation and examples

See the godoc here: https://godoc.org/go.bug.st/relaxed-semver
See the godoc here: <https://godoc.org/go.bug.st/relaxed-semver>

## Semantic versioning specification followed in this library

Expand Down Expand Up @@ -39,20 +39,22 @@ To parse a `RelaxedVersion` you can use the `ParseRelaxed` function.

Dependency version matching can be specified via version constraints, which might be a version range or an exact version.

The following operators are supported:

| | |
| -------- | ------------------------ |
| `=` | equal to |
| `>` | greater than |
| `>=` | greater than or equal to |
| `<` | less than |
| `<=` | less than or equal to |
| `^` | compatible-with |
| `!` | NOT |
| `&&` | AND |
| `\|\|` | OR |
| `(`, `)` | constraint group |
The following range operators are supported:

| Operator | Meaning |
| ----------- | ------------------------ |
| `^` or None | compatible-with |
| `=` | equal to |
| `>` | greater than |
| `>=` | greater than or equal to |
| `<` | less than |
| `<=` | less than or equal to |
| `!` | NOT |
| `&&` | AND |
| `\|\|` | OR |
| `(`, `)` | constraint group |

In a constraint if a version is written without the operator the "compatible-with" operator is applied.

### Examples

Expand Down Expand Up @@ -82,6 +84,8 @@ constraints conditions would match as follows:
| `<1.0.0 \|\| >2.0.0` | `0.1.0`, `0.1.1`, `0.2.0`, `2.0.5`, `2.0.6`, `2.1.0`, `3.0.0` |
| `(>0.1.0 && <2.0.0) \|\| >2.0.5` | `0.1.1`, `0.2.0`, `1.0.0`, `2.0.6`, `2.1.0`, `3.0.0` |
| `^2.0.5` | `2.0.5`, `2.0.6`, `2.1.0` |
| `2.0.5` | `2.0.5`, `2.0.6`, `2.1.0` (same as `^2.0.5`) |
| `2.0.5 && !=2.0.6` | `2.0.5`, `2.1.0` (same as `^2.0.5 && !=2.0.6`) |
| `^0.1.0` | `0.1.0`, `0.1.1` |

## Json parsable
Expand Down
20 changes: 15 additions & 5 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func ParseConstraint(in string) (Constraint, error) {
n := peek()
if !isIdentifier(n) && !isVersionSeparator(n) {
if start == curr {
return nil, fmt.Errorf("invalid version")
return nil, fmt.Errorf("invalid version: unexpected char '%c'", rune(n))
}
return Parse(in[start:curr])
}
curr++
next()
}
}

Expand All @@ -66,36 +66,41 @@ func ParseConstraint(in string) (Constraint, error) {

terminal = func() (Constraint, error) {
skipSpace()
switch next() {
switch peek() {
case '!':
next()
expr, err := terminal()
if err != nil {
return nil, err
}
return &Not{expr}, nil
case '(':
next()
expr, err := constraint()
if err != nil {
return nil, err
}
skipSpace()
if c := next(); c != ')' {
return nil, fmt.Errorf("unexpected char at: %s", in[curr-1:])
return nil, fmt.Errorf("unexpected char: %c", rune(c))
}
return expr, nil
case '=':
next()
v, err := version()
if err != nil {
return nil, err
}
return &Equals{v}, nil
case '^':
next()
v, err := version()
if err != nil {
return nil, err
}
return &CompatibleWith{v}, nil
case '>':
next()
if peek() == '=' {
next()
v, err := version()
Expand All @@ -111,6 +116,7 @@ func ParseConstraint(in string) (Constraint, error) {
return &GreaterThan{v}, nil
}
case '<':
next()
if peek() == '=' {
next()
v, err := version()
Expand All @@ -126,7 +132,11 @@ func ParseConstraint(in string) (Constraint, error) {
return &LessThan{v}, nil
}
default:
return nil, fmt.Errorf("unexpected char at: %s", in[curr-1:])
v, err := version()
if err != nil {
return nil, err
}
return &CompatibleWith{v}, nil
}
}

Expand Down
43 changes: 37 additions & 6 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ func TestConstraintsParser(t *testing.T) {
good := []goodStringTest{
{"", ""}, // always true
{"=1.3.0", "=1.3.0"},
{"!=1.3.0", "!(=1.3.0)"},
{" !=1.3.0", "!(=1.3.0)"},
{"! =1.3.0", "!(=1.3.0)"},
{"1.3.0", "^1.3.0"},
{"!1.0.0", "!(^1.0.0)"},
{" =1.3.0 ", "=1.3.0"},
{" 1.3.0 ", "^1.3.0"},
{"=1.3.0 ", "=1.3.0"},
{"1.3.0 ", "^1.3.0"},
{" =1.3.0", "=1.3.0"},
{" 1.3.0", "^1.3.0"},
{">=1.3.0", ">=1.3.0"},
{">1.3.0", ">1.3.0"},
{"<=1.3.0", "<=1.3.0"},
Expand All @@ -111,16 +119,37 @@ func TestConstraintsParser(t *testing.T) {
{"^1.3.0 ", "^1.3.0"},
{" ^1.3.0 ", "^1.3.0"},
{"(=1.4.0)", "=1.4.0"},
{"(1.4.0)", "^1.4.0"},
{"!(=1.4.0)", "!(=1.4.0)"},
{"!(1.4.0)", "!(^1.4.0)"},
{"!(((=1.4.0)))", "!(=1.4.0)"},
{"!(((1.4.0)))", "!(^1.4.0)"},
{"=1.2.4 && =1.3.0", "(=1.2.4 && =1.3.0)"},
{"=1.2.4 && ^1.3.0", "(=1.2.4 && ^1.3.0)"},
{"1.2.4 && 1.3.0", "(^1.2.4 && ^1.3.0)"},
{"=1.2.4 && =1.3.0 && =1.2.0", "(=1.2.4 && =1.3.0 && =1.2.0)"},
{"1.2.4 && 1.3.0 && 1.2.0", "(^1.2.4 && ^1.3.0 && ^1.2.0)"},
{"=1.2.4 && =1.3.0 || =1.2.0", "((=1.2.4 && =1.3.0) || =1.2.0)"},
{"1.2.4 && 1.3.0 || 1.2.0", "((^1.2.4 && ^1.3.0) || ^1.2.0)"},
{"=1.2.4 || =1.3.0 && =1.2.0", "(=1.2.4 || (=1.3.0 && =1.2.0))"},
{"(=1.2.4 || =1.3.0) && =1.2.0", "((=1.2.4 || =1.3.0) && =1.2.0)"},
{"(1.2.4 || 1.3.0) && 1.2.0", "((^1.2.4 || ^1.3.0) && ^1.2.0)"},
{"(=1.2.4 || !>1.3.0) && =1.2.0", "((=1.2.4 || !(>1.3.0)) && =1.2.0)"},
{"(1.2.4 || !>1.3.0) && 1.2.0", "((^1.2.4 || !(>1.3.0)) && ^1.2.0)"},
{"!(=1.2.4 || >1.3.0) && =1.2.0", "(!(=1.2.4 || >1.3.0) && =1.2.0)"},
{"!(1.2.4 || >1.3.0) && 1.2.0", "(!(^1.2.4 || >1.3.0) && ^1.2.0)"},
{">1.0.0 && 2.0.0", "(>1.0.0 && ^2.0.0)"},
{">1.0.0 && =2.0.0", "(>1.0.0 && =2.0.0)"},
{">1.0.0 || 2.0.0", "(>1.0.0 || ^2.0.0)"},
{">1.0.0 || =2.0.0", "(>1.0.0 || =2.0.0)"},
{"(>1.0.0) || 2.0.0", "(>1.0.0 || ^2.0.0)"},
{"(>1.0.0) || =2.0.0", "(>1.0.0 || =2.0.0)"},
{">1.0.0 || (2.0.0)", "(>1.0.0 || ^2.0.0)"},
{">1.0.0 || (=2.0.0)", "(>1.0.0 || =2.0.0)"},
{"!>1.0.0 || (=2.0.0)", "(!(>1.0.0) || =2.0.0)"},
{"!(>1.0.0 || =2.0.0)", "!(>1.0.0 || =2.0.0)"},
{"((>1.0.0) || (2.0.0))", "(>1.0.0 || ^2.0.0)"},
{"((>1.0.0) || (=2.0.0))", "(>1.0.0 || =2.0.0)"},
}
for i, test := range good {
in := test.In
Expand All @@ -134,8 +163,8 @@ func TestConstraintsParser(t *testing.T) {
}

bad := []string{
"1.0.0",
"= 1.0.0",
"!= 1.3.9",
">= 1.0.0",
"> 1.0.0",
"<= 1.0.0",
Expand All @@ -144,19 +173,21 @@ func TestConstraintsParser(t *testing.T) {
">1.0.0 =2.0.0",
">1.0.0 &",
"^1.1.1.1",
"!1.0.0",
">1.0.0 && 2.0.0",
">1.0.0 | =2.0.0",
"(>1.0.0 | =2.0.0)",
"(>1.0.0 || =2.0.0",
">1.0.0 || 2.0.0",
"!1.0.0.0",
"!1.0.0 && !1.0.0.0",
"(!1.0.0 && !1.0.0",
"!1.0.0 || !1.0.0.0",
"(!1.0.0 || !1.0.0",
}
for i, s := range bad {
in := s
t.Run(fmt.Sprintf("BadString%03d", i), func(t *testing.T) {
p, err := ParseConstraint(in)
require.Nil(t, p)
require.Error(t, err)
require.Nil(t, p, "parsing: %s", in)
require.Error(t, err, "parsing: %s", in)
fmt.Printf("'%s' parse error: %s\n", in, err)
})
}
Expand Down
Loading