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

Commit cac9c6b

Browse files
committed
simple check if constrains are directDeps, and updated tests to add
applicable constrain fixtures
1 parent b209495 commit cac9c6b

File tree

2 files changed

+19
-93
lines changed

2 files changed

+19
-93
lines changed

cmd/dep/status.go

+2-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/golang/dep"
2121
"github.com/golang/dep/gps"
2222
"github.com/golang/dep/gps/paths"
23-
"github.com/golang/dep/gps/pkgtree"
2423
"github.com/pkg/errors"
2524
)
2625

@@ -794,20 +793,14 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
794793
// Get project constraints.
795794
pc := manifest.DependencyConstraints()
796795

797-
imports, err := projectImports(sm, proj)
798-
if err != nil {
799-
errCh <- errors.Wrapf(err, "error listing imports used in %s", proj.Ident().ProjectRoot)
800-
return
801-
}
802-
803796
// Obtain a lock for constraintCollection.
804797
mutex.Lock()
805798
defer mutex.Unlock()
806799
// Iterate through the project constraints to get individual dependency
807800
// project and constraint values.
808801
for pr, pp := range pc {
809-
// Check if the project constraint is imported in the project
810-
if _, ok := imports[string(pr)]; !ok {
802+
// Check if the project constraint is imported in the root project
803+
if _, ok := directDeps[string(pr)]; !ok {
811804
continue
812805
}
813806

@@ -838,29 +831,6 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
838831
return constraintCollection, errs
839832
}
840833

841-
// projectImports creates a set of all imports paths used in a project.
842-
// The set of imports be used to check if an package is imported in a project.
843-
func projectImports(sm gps.SourceManager, proj gps.LockedProject) (map[string]bool, error) {
844-
pkgTree, err := sm.ListPackages(proj.Ident(), proj.Version())
845-
if err != nil {
846-
return nil, err
847-
}
848-
return packageTreeImports(pkgTree), nil
849-
}
850-
851-
func packageTreeImports(pkgTree pkgtree.PackageTree) map[string]bool {
852-
imports := make(map[string]bool)
853-
for _, pkg := range pkgTree.Packages {
854-
if pkg.Err != nil {
855-
continue
856-
}
857-
for _, imp := range pkg.P.Imports {
858-
imports[imp] = true
859-
}
860-
}
861-
return imports
862-
}
863-
864834
type byProject []projectConstraint
865835

866836
func (p byProject) Len() int { return len(p) }

cmd/dep/status_test.go

+17-61
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func TestCollectConstraints(t *testing.T) {
305305
ver1, _ := gps.NewSemverConstraintIC("v1.0.0")
306306
ver08, _ := gps.NewSemverConstraintIC("v0.8.0")
307307
ver2, _ := gps.NewSemverConstraintIC("v2.0.0")
308+
master := gps.NewBranch("master")
308309

309310
cases := []struct {
310311
name string
@@ -387,15 +388,18 @@ func TestCollectConstraints(t *testing.T) {
387388
lock: dep.Lock{
388389
P: []gps.LockedProject{
389390
gps.NewLockedProject(
390-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/JackyChiu/deptest")},
391+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/JackyChiu/dep-applicable-constraints")},
391392
gps.NewVersion("v1.0.0"),
392393
[]string{"."},
393394
),
394395
},
395396
},
396397
wantConstraints: constraintsCollection{
398+
"github.com/boltdb/bolt": []projectConstraint{
399+
{"github.com/JackyChiu/dep-applicable-constraints", master},
400+
},
397401
"github.com/sdboyer/deptest": []projectConstraint{
398-
{"github.com/JackyChiu/deptest", ver08},
402+
{"github.com/JackyChiu/dep-applicable-constraints", ver08},
399403
},
400404
},
401405
},
@@ -406,6 +410,17 @@ func TestCollectConstraints(t *testing.T) {
406410

407411
h.TempDir("src")
408412
pwd := h.Path(".")
413+
h.TempFile(filepath.Join("src", "dep.go"), `
414+
package dep
415+
import (
416+
_ "github.com/boltdb/bolt"
417+
_ "github.com/sdboyer/deptest"
418+
_ "github.com/sdboyer/dep-test"
419+
_ "github.com/sdboyer/deptestdos"
420+
)
421+
type FooBar int
422+
`)
423+
409424
discardLogger := log.New(ioutil.Discard, "", 0)
410425

411426
ctx := &dep.Ctx{
@@ -440,65 +455,6 @@ func TestCollectConstraints(t *testing.T) {
440455
}
441456
}
442457

443-
func TestProjectImports(t *testing.T) {
444-
cases := []struct {
445-
name string
446-
proj gps.LockedProject
447-
expected map[string]bool
448-
}{
449-
{
450-
name: "no imports",
451-
proj: gps.NewLockedProject(
452-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
453-
gps.NewVersion("v1.0.0"),
454-
[]string{"."},
455-
),
456-
expected: map[string]bool{},
457-
},
458-
{
459-
name: "mutilple imports",
460-
proj: gps.NewLockedProject(
461-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
462-
gps.NewVersion("v0.1.0"),
463-
[]string{"."},
464-
),
465-
expected: map[string]bool{
466-
"fmt": true,
467-
"github.com/sdboyer/deptest": true,
468-
},
469-
},
470-
}
471-
472-
h := test.NewHelper(t)
473-
defer h.Cleanup()
474-
475-
h.TempDir("src")
476-
pwd := h.Path(".")
477-
discardLogger := log.New(ioutil.Discard, "", 0)
478-
479-
ctx := &dep.Ctx{
480-
GOPATH: pwd,
481-
Out: discardLogger,
482-
Err: discardLogger,
483-
}
484-
485-
sm, err := ctx.SourceManager()
486-
h.Must(err)
487-
defer sm.Release()
488-
489-
for _, c := range cases {
490-
t.Run(c.name, func(t *testing.T) {
491-
imports, err := projectImports(sm, c.proj)
492-
if err != nil {
493-
t.Fatalf("unexpected error while getting project imports: %v", err)
494-
}
495-
if !reflect.DeepEqual(imports, c.expected) {
496-
t.Fatalf("unexpected project imports: \n\t(GOT): %v\n\t(WNT): %v", imports, c.expected)
497-
}
498-
})
499-
}
500-
}
501-
502458
func TestValidateFlags(t *testing.T) {
503459
testCases := []struct {
504460
name string

0 commit comments

Comments
 (0)