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

adding TestSingleSourceCache #756

Merged
merged 4 commits into from
Jul 21, 2017
Merged
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
38 changes: 38 additions & 0 deletions internal/gps/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package gps
import (
"fmt"
"testing"

"github.com/pkg/errors"
)

// gu - helper func for stringifying what we assume is a VersionPair (otherwise
Expand Down Expand Up @@ -927,3 +929,39 @@ func TestTypedConstraintString(t *testing.T) {
}
}
}

func TestConstraintsIdentical(t *testing.T) {
for _, test := range []struct {
a, b Constraint
eq bool
}{
{Any(), Any(), true},
{none, noneConstraint{}, true},
{NewVersion("test"), NewVersion("test"), true},
{NewVersion("test"), NewVersion("test2"), false},
{NewBranch("test"), NewBranch("test"), true},
{NewBranch("test"), newDefaultBranch("test"), false},
{newDefaultBranch("test"), newDefaultBranch("test"), true},
{Revision("test"), Revision("test"), true},
{Revision("test"), Revision("test2"), false},
{testSemverConstraint(t, "v2.10.7"), testSemverConstraint(t, "v2.10.7"), true},
{versionTypeUnion{NewVersion("test"), NewBranch("branch")},
versionTypeUnion{NewBranch("branch"), NewVersion("test")}, true},
} {
if test.eq != test.a.identical(test.b) {
want := "identical"
if !test.eq {
want = "not " + want
}
t.Errorf("expected %s:\n\t(a) %#v\n\t(b) %#v", want, test.a, test.b)
}
}
}

func testSemverConstraint(t *testing.T, body string) Constraint {
c, err := NewSemverConstraint(body)
if err != nil {
t.Fatal(errors.Wrapf(err, "failed to create semver constraint: %s", body))
}
return c
}
25 changes: 25 additions & 0 deletions internal/gps/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type Constraint interface {
// It also forces Constraint to be a private/sealed interface, which is a
// design goal of the system.
typedString() string

// identical returns true if the constraints are identical.
//
// Identical Constraints behave identically for all methods defined by the
// interface. A Constraint is always identical to itself.
//
// Constraints serialized for caching are de-serialized into identical instances.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the serialization process is stringifying, then this isn't true for branches, because of that default branch prop 😢

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more complicated than that. The encoding scheme is queued up for a future PR after this one (maybe the next, have to dig it out and rebase).

identical(Constraint) bool
}

// NewSemverConstraint attempts to construct a semver Constraint object from the
Expand Down Expand Up @@ -172,6 +180,14 @@ func (c semverConstraint) Intersect(c2 Constraint) Constraint {
return none
}

func (c semverConstraint) identical(c2 Constraint) bool {
sc2, ok := c2.(semverConstraint)
if !ok {
return false
}
return c.c.String() == sc2.c.String()
}

// IsAny indicates if the provided constraint is the wildcard "Any" constraint.
func IsAny(c Constraint) bool {
_, ok := c.(anyConstraint)
Expand Down Expand Up @@ -211,6 +227,10 @@ func (anyConstraint) Intersect(c Constraint) Constraint {
return c
}

func (anyConstraint) identical(c Constraint) bool {
return IsAny(c)
}

// noneConstraint is the empty set - it matches no versions. It mirrors the
// behavior of the semver package's none type.
type noneConstraint struct{}
Expand Down Expand Up @@ -239,6 +259,11 @@ func (noneConstraint) Intersect(Constraint) Constraint {
return none
}

func (noneConstraint) identical(c Constraint) bool {
_, ok := c.(noneConstraint)
return ok
}

// A ProjectConstraint combines a ProjectIdentifier with a Constraint. It
// indicates that, if packages contained in the ProjectIdentifier enter the
// depgraph, they must do so at a version that is allowed by the Constraint.
Expand Down
4 changes: 2 additions & 2 deletions internal/gps/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func prepLock(l Lock) safeLock {
}

// SortLockedProjects sorts a slice of LockedProject in alphabetical order by
// ProjectRoot.
// ProjectIdentifier.
func SortLockedProjects(lps []LockedProject) {
sort.Stable(lpsorter(lps))
}
Expand All @@ -242,5 +242,5 @@ func (lps lpsorter) Len() int {
}

func (lps lpsorter) Less(i, j int) bool {
return lps[i].pi.ProjectRoot < lps[j].pi.ProjectRoot
return lps[i].Ident().less(lps[j].Ident())
}
36 changes: 36 additions & 0 deletions internal/gps/source_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,39 @@ func (c *singleSourceCacheMemory) toUnpaired(v Version) (UnpairedVersion, bool)
panic(fmt.Sprintf("unknown version type %T", v))
}
}

// cachedManifest implements RootManifest and is populated from cached data.
type cachedManifest struct {
constraints, overrides ProjectConstraints
ignored, required map[string]bool
}

func (m *cachedManifest) DependencyConstraints() ProjectConstraints {
return m.constraints
}

func (m *cachedManifest) Overrides() ProjectConstraints {
return m.overrides
}

func (m *cachedManifest) IgnoredPackages() map[string]bool {
return m.ignored
}

func (m *cachedManifest) RequiredPackages() map[string]bool {
return m.required
}

// cachedManifest implements Lock and is populated from cached data.
type cachedLock struct {
inputHash []byte
projects []LockedProject
}

func (l *cachedLock) InputHash() []byte {
return l.inputHash
}

func (l *cachedLock) Projects() []LockedProject {
return l.projects
}
Loading