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 '=' constraint implicit if operator is missing [experimental] #7

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 14 additions & 5 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,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 @@ -65,30 +65,34 @@ 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()
if peek() == '=' {
next()
v, err := version()
Expand All @@ -104,6 +108,7 @@ func ParseConstraint(in string) (Constraint, error) {
return &GreaterThan{v}, nil
}
case '<':
next()
if peek() == '=' {
next()
v, err := version()
Expand All @@ -119,7 +124,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 &Equals{v}, nil
}
}

Expand Down
37 changes: 31 additions & 6 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,47 @@ func TestConstraintsParser(t *testing.T) {
good := []goodStringTest{
{"", ""}, // always true
{"=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"},
{"<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.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)"},
}
for i, test := range good {
in := test.In
Expand All @@ -115,7 +139,6 @@ func TestConstraintsParser(t *testing.T) {
}

bad := []string{
"1.0.0",
"= 1.0.0",
">= 1.0.0",
"> 1.0.0",
Expand All @@ -124,19 +147,21 @@ func TestConstraintsParser(t *testing.T) {
">>1.0.0",
">1.0.0 =2.0.0",
">1.0.0 &",
"!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