-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom maven comparator (#1571)
This PR takes the recommendation from #1526 and adapts the go-mvn-version to be used as a custom comparator for matching against packages that have the JavaPkg type. Packages of type JavaPkg will no longer use the stock matcher. --------- Signed-off-by: Christopher Phillips <[email protected]> Co-authored-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
19 changed files
with
327 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package version | ||
|
||
import "fmt" | ||
|
||
type mavenConstraint struct { | ||
raw string | ||
expression constraintExpression | ||
} | ||
|
||
func newMavenConstraint(raw string) (mavenConstraint, error) { | ||
if raw == "" { | ||
// empty constraints are always satisfied | ||
return mavenConstraint{}, nil | ||
} | ||
|
||
constraints, err := newConstraintExpression(raw, newMavenComparator) | ||
if err != nil { | ||
return mavenConstraint{}, fmt.Errorf("unable to parse maven constraint phrase: %w", err) | ||
} | ||
|
||
return mavenConstraint{ | ||
raw: raw, | ||
expression: constraints, | ||
}, nil | ||
} | ||
|
||
func newMavenComparator(unit constraintUnit) (Comparator, error) { | ||
ver, err := newMavenVersion(unit.version) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse constraint version (%s): %w", unit.version, err) | ||
} | ||
|
||
return ver, nil | ||
} | ||
|
||
func (c mavenConstraint) supported(format Format) bool { | ||
return format == MavenFormat | ||
} | ||
|
||
func (c mavenConstraint) Satisfied(version *Version) (satisfied bool, err error) { | ||
if c.raw == "" && version != nil { | ||
// empty constraints are always satisfied | ||
return true, nil | ||
} | ||
|
||
if version == nil { | ||
if c.raw != "" { | ||
// a non-empty constraint with no version given should always fail | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
if !c.supported(version.Format) { | ||
return false, fmt.Errorf("(maven) unsupported format: %s", version.Format) | ||
} | ||
|
||
if version.rich.mavenVer == nil { | ||
return false, fmt.Errorf("no rich apk version given: %+v", version) | ||
} | ||
|
||
return c.expression.satisfied(version) | ||
} | ||
|
||
func (c mavenConstraint) String() string { | ||
if c.raw == "" { | ||
return "none (maven)" | ||
} | ||
|
||
return fmt.Sprintf("%s (maven)", c.raw) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVersionConstraintJava(t *testing.T) { | ||
tests := []testCase{ | ||
{version: "1", constraint: "< 2.5", satisfied: true}, | ||
{version: "1.0", constraint: "< 1.1", satisfied: true}, | ||
{version: "1.1", constraint: "< 1.2", satisfied: true}, | ||
{version: "1.0.0", constraint: "< 1.1", satisfied: true}, | ||
{version: "1.0.1", constraint: "< 1.1", satisfied: true}, | ||
{version: "1.1", constraint: "> 1.2.0", satisfied: false}, | ||
{version: "1.0-alpha-1", constraint: "> 1.0", satisfied: false}, | ||
{version: "1.0-alpha-1", constraint: "> 1.0-alpha-2", satisfied: false}, | ||
{version: "1.0-alpha-1", constraint: "< 1.0-beta-1", satisfied: true}, | ||
{version: "1.0-beta-1", constraint: "< 1.0-SNAPSHOT", satisfied: true}, | ||
{version: "1.0-SNAPSHOT", constraint: "< 1.0", satisfied: true}, | ||
{version: "1.0-alpha-1-SNAPSHOT", constraint: "> 1.0-alpha-1", satisfied: false}, | ||
{version: "1.0", constraint: "< 1.0-1", satisfied: true}, | ||
{version: "1.0-1", constraint: "< 1.0-2", satisfied: true}, | ||
{version: "1.0.0", constraint: "< 1.0-1", satisfied: true}, | ||
{version: "2.0-1", constraint: "> 2.0.1", satisfied: false}, | ||
{version: "2.0.1-klm", constraint: "> 2.0.1-lmn", satisfied: false}, | ||
{version: "2.0.1", constraint: "< 2.0.1-xyz", satisfied: true}, | ||
{version: "2.0.1", constraint: "< 2.0.1-123", satisfied: true}, | ||
{version: "2.0.1-xyz", constraint: "< 2.0.1-123", satisfied: true}, | ||
{version: "2.414.2-cb-5", constraint: "> 2.414.2", satisfied: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
constraint, err := newMavenConstraint(test.constraint) | ||
|
||
assert.NoError(t, err, "unexpected error from newMavenConstraint %s: %v", test.version, err) | ||
test.assertVersionConstraint(t, MavenFormat, constraint) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestVersionEqualityJava(t *testing.T) { | ||
tests := []testCase{ | ||
{version: "1", constraint: "1", satisfied: true}, | ||
{version: "1", constraint: "1.0", satisfied: true}, | ||
{version: "1", constraint: "1.0.0", satisfied: true}, | ||
{version: "1.0", constraint: "1.0.0", satisfied: true}, | ||
{version: "1", constraint: "1-0", satisfied: true}, | ||
{version: "1", constraint: "1.0-0", satisfied: true}, | ||
{version: "1.0", constraint: "1.0-0", satisfied: true}, | ||
{version: "1a", constraint: "1-a", satisfied: true}, | ||
{version: "1a", constraint: "1.0-a", satisfied: true}, | ||
{version: "1a", constraint: "1.0.0-a", satisfied: true}, | ||
{version: "1.0a", constraint: "1-a", satisfied: true}, | ||
{version: "1.0.0a", constraint: "1-a", satisfied: true}, | ||
{version: "1x", constraint: "1-x", satisfied: true}, | ||
{version: "1x", constraint: "1.0-x", satisfied: true}, | ||
{version: "1x", constraint: "1.0.0-x", satisfied: true}, | ||
{version: "1.0x", constraint: "1-x", satisfied: true}, | ||
{version: "1.0.0x", constraint: "1-x", satisfied: true}, | ||
{version: "1ga", constraint: "1", satisfied: true}, | ||
{version: "1release", constraint: "1", satisfied: true}, | ||
{version: "1final", constraint: "1", satisfied: true}, | ||
{version: "1cr", constraint: "1rc", satisfied: true}, | ||
{version: "1a1", constraint: "1-alpha-1", satisfied: true}, | ||
{version: "1b2", constraint: "1-beta-2", satisfied: true}, | ||
{version: "1m3", constraint: "1-milestone-3", satisfied: true}, | ||
{version: "1X", constraint: "1x", satisfied: true}, | ||
{version: "1A", constraint: "1a", satisfied: true}, | ||
{version: "1B", constraint: "1b", satisfied: true}, | ||
{version: "1M", constraint: "1m", satisfied: true}, | ||
{version: "1Ga", constraint: "1", satisfied: true}, | ||
{version: "1GA", constraint: "1", satisfied: true}, | ||
{version: "1RELEASE", constraint: "1", satisfied: true}, | ||
{version: "1release", constraint: "1", satisfied: true}, | ||
{version: "1RELeaSE", constraint: "1", satisfied: true}, | ||
{version: "1Final", constraint: "1", satisfied: true}, | ||
{version: "1FinaL", constraint: "1", satisfied: true}, | ||
{version: "1FINAL", constraint: "1", satisfied: true}, | ||
{version: "1Cr", constraint: "1Rc", satisfied: true}, | ||
{version: "1cR", constraint: "1rC", satisfied: true}, | ||
{version: "1m3", constraint: "1Milestone3", satisfied: true}, | ||
{version: "1m3", constraint: "1MileStone3", satisfied: true}, | ||
{version: "1m3", constraint: "1MILESTONE3", satisfied: true}, | ||
{version: "1", constraint: "01", satisfied: true}, | ||
{version: "1", constraint: "001", satisfied: true}, | ||
{version: "1.1", constraint: "1.01", satisfied: true}, | ||
{version: "1.1", constraint: "1.001", satisfied: true}, | ||
{version: "1-1", constraint: "1-01", satisfied: true}, | ||
{version: "1-1", constraint: "1-001", satisfied: true}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
constraint, err := newMavenConstraint(test.constraint) | ||
|
||
assert.NoError(t, err, "unexpected error from newMavenConstraint %s: %v", test.version, err) | ||
test.assertVersionConstraint(t, MavenFormat, constraint) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
|
||
mvnv "github.com/masahiro331/go-mvn-version" | ||
) | ||
|
||
type mavenVersion struct { | ||
raw string | ||
version mvnv.Version | ||
} | ||
|
||
func newMavenVersion(raw string) (*mavenVersion, error) { | ||
ver, err := mvnv.NewVersion(raw) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not generate new java version from: %s; %w", raw, err) | ||
} | ||
|
||
return &mavenVersion{ | ||
raw: raw, | ||
version: ver, | ||
}, nil | ||
} | ||
|
||
// Compare returns 0 if j2 == j, 1 if j2 > j, and -1 if j2 < j. | ||
// If an error returns the int value is -1 | ||
func (j *mavenVersion) Compare(j2 *Version) (int, error) { | ||
if j2.Format != MavenFormat { | ||
return -1, fmt.Errorf("unable to compare java to given format: %s", j2.Format) | ||
} | ||
if j2.rich.mavenVer == nil { | ||
return -1, fmt.Errorf("given empty mavenVersion object") | ||
} | ||
|
||
submittedVersion := j2.rich.mavenVer.version | ||
if submittedVersion.Equal(j.version) { | ||
return 0, nil | ||
} | ||
if submittedVersion.LessThan(j.version) { | ||
return -1, nil | ||
} | ||
if submittedVersion.GreaterThan(j.version) { | ||
return 1, nil | ||
} | ||
|
||
return -1, fmt.Errorf( | ||
"could not compare java versions: %v with %v", | ||
submittedVersion.String(), | ||
j.version.String()) | ||
} |
Oops, something went wrong.