simple check if constrains are directDeps, and updated tests to add

applicable constrain fixtures
This commit is contained in:
Jacky Chiu 2017-12-25 17:14:21 -05:00
Родитель b209495ec4
Коммит cac9c6bf83
2 изменённых файлов: 19 добавлений и 93 удалений

Просмотреть файл

@ -20,7 +20,6 @@ import (
"github.com/golang/dep"
"github.com/golang/dep/gps"
"github.com/golang/dep/gps/paths"
"github.com/golang/dep/gps/pkgtree"
"github.com/pkg/errors"
)
@ -794,20 +793,14 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
// Get project constraints.
pc := manifest.DependencyConstraints()
imports, err := projectImports(sm, proj)
if err != nil {
errCh <- errors.Wrapf(err, "error listing imports used in %s", proj.Ident().ProjectRoot)
return
}
// Obtain a lock for constraintCollection.
mutex.Lock()
defer mutex.Unlock()
// Iterate through the project constraints to get individual dependency
// project and constraint values.
for pr, pp := range pc {
// Check if the project constraint is imported in the project
if _, ok := imports[string(pr)]; !ok {
// Check if the project constraint is imported in the root project
if _, ok := directDeps[string(pr)]; !ok {
continue
}
@ -838,29 +831,6 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
return constraintCollection, errs
}
// projectImports creates a set of all imports paths used in a project.
// The set of imports be used to check if an package is imported in a project.
func projectImports(sm gps.SourceManager, proj gps.LockedProject) (map[string]bool, error) {
pkgTree, err := sm.ListPackages(proj.Ident(), proj.Version())
if err != nil {
return nil, err
}
return packageTreeImports(pkgTree), nil
}
func packageTreeImports(pkgTree pkgtree.PackageTree) map[string]bool {
imports := make(map[string]bool)
for _, pkg := range pkgTree.Packages {
if pkg.Err != nil {
continue
}
for _, imp := range pkg.P.Imports {
imports[imp] = true
}
}
return imports
}
type byProject []projectConstraint
func (p byProject) Len() int { return len(p) }

Просмотреть файл

@ -305,6 +305,7 @@ func TestCollectConstraints(t *testing.T) {
ver1, _ := gps.NewSemverConstraintIC("v1.0.0")
ver08, _ := gps.NewSemverConstraintIC("v0.8.0")
ver2, _ := gps.NewSemverConstraintIC("v2.0.0")
master := gps.NewBranch("master")
cases := []struct {
name string
@ -387,15 +388,18 @@ func TestCollectConstraints(t *testing.T) {
lock: dep.Lock{
P: []gps.LockedProject{
gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/JackyChiu/deptest")},
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/JackyChiu/dep-applicable-constraints")},
gps.NewVersion("v1.0.0"),
[]string{"."},
),
},
},
wantConstraints: constraintsCollection{
"github.com/boltdb/bolt": []projectConstraint{
{"github.com/JackyChiu/dep-applicable-constraints", master},
},
"github.com/sdboyer/deptest": []projectConstraint{
{"github.com/JackyChiu/deptest", ver08},
{"github.com/JackyChiu/dep-applicable-constraints", ver08},
},
},
},
@ -406,6 +410,17 @@ func TestCollectConstraints(t *testing.T) {
h.TempDir("src")
pwd := h.Path(".")
h.TempFile(filepath.Join("src", "dep.go"), `
package dep
import (
_ "github.com/boltdb/bolt"
_ "github.com/sdboyer/deptest"
_ "github.com/sdboyer/dep-test"
_ "github.com/sdboyer/deptestdos"
)
type FooBar int
`)
discardLogger := log.New(ioutil.Discard, "", 0)
ctx := &dep.Ctx{
@ -440,65 +455,6 @@ func TestCollectConstraints(t *testing.T) {
}
}
func TestProjectImports(t *testing.T) {
cases := []struct {
name string
proj gps.LockedProject
expected map[string]bool
}{
{
name: "no imports",
proj: gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
gps.NewVersion("v1.0.0"),
[]string{"."},
),
expected: map[string]bool{},
},
{
name: "mutilple imports",
proj: gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
gps.NewVersion("v0.1.0"),
[]string{"."},
),
expected: map[string]bool{
"fmt": true,
"github.com/sdboyer/deptest": true,
},
},
}
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("src")
pwd := h.Path(".")
discardLogger := log.New(ioutil.Discard, "", 0)
ctx := &dep.Ctx{
GOPATH: pwd,
Out: discardLogger,
Err: discardLogger,
}
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
imports, err := projectImports(sm, c.proj)
if err != nil {
t.Fatalf("unexpected error while getting project imports: %v", err)
}
if !reflect.DeepEqual(imports, c.expected) {
t.Fatalf("unexpected project imports: \n\t(GOT): %v\n\t(WNT): %v", imports, c.expected)
}
})
}
}
func TestValidateFlags(t *testing.T) {
testCases := []struct {
name string