Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Select locked version compatible with the constraint
Browse files Browse the repository at this point in the history
When selecting a preferred version for a lock (before solve)
if the constraint is a semver range, ensure the version
selected is compatible.
  • Loading branch information
carolynvs committed Jul 6, 2017
1 parent c300e1c commit db62987
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 9 additions & 0 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ func lookupVersionForLockedProject(pi gps.ProjectIdentifier, c gps.Constraint, r
gps.SortPairedForUpgrade(versions) // Sort versions in asc order
for _, v := range versions {
if v.Revision() == rev {
// If the constraint is semver, make sure the version is acceptable.
// This prevents us from suggesting an incompatible version, which
// helps narrow the field when there are multiple matching versions.
if c != nil {
_, err := gps.NewSemverConstraint(c.String())
if err == nil && !c.Matches(v) {
continue
}
}
return v, nil
}
}
Expand Down
26 changes: 24 additions & 2 deletions cmd/dep/root_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func TestLookupVersionForLockedProject_MatchRevisionToTag(t *testing.T) {
defer sm.Release()

pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}
c, _ := gps.NewSemverConstraint("^0.8.1")
rev := gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf")
v, err := lookupVersionForLockedProject(pi, c, rev, sm)
v, err := lookupVersionForLockedProject(pi, nil, rev, sm)
h.Must(err)

wantV := "v1.0.0"
Expand All @@ -47,6 +46,29 @@ func TestLookupVersionForLockedProject_MatchRevisionToTag(t *testing.T) {
}
}

func TestLookupVersionForLockedProject_MatchRevisionToMultipleTags(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

ctx := newTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}
// Both 0.8.0 and 1.0.0 use the same rev, force dep to pick the lower version
c, _ := gps.NewSemverConstraint("<1.0.0")
rev := gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf")
v, err := lookupVersionForLockedProject(pi, c, rev, sm)
h.Must(err)

wantV := "v0.8.0"
gotV := v.String()
if gotV != wantV {
t.Fatalf("Expected the locked version to satisfy the manifest's semver constraint: wanted '%s', got '%s'", wantV, gotV)
}
}

func TestLookupVersionForLockedProject_FallbackToConstraint(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
Expand Down

0 comments on commit db62987

Please sign in to comment.