зеркало из https://github.com/golang/dep.git
build import map and then check if constraint match
added tests for projectImports collect applicable constraints only test update term used in err msg break up import functions
This commit is contained in:
Родитель
08214b1a31
Коммит
b209495ec4
|
@ -20,6 +20,7 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -793,12 +794,23 @@ 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 {
|
||||
continue
|
||||
}
|
||||
|
||||
tempCC := append(
|
||||
constraintCollection[string(pr)],
|
||||
projectConstraint{proj.Ident().ProjectRoot, pp.Constraint},
|
||||
|
@ -826,6 +838,29 @@ 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) }
|
||||
|
|
|
@ -382,6 +382,23 @@ func TestCollectConstraints(t *testing.T) {
|
|||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "collect only applicable constraints",
|
||||
lock: dep.Lock{
|
||||
P: []gps.LockedProject{
|
||||
gps.NewLockedProject(
|
||||
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/JackyChiu/deptest")},
|
||||
gps.NewVersion("v1.0.0"),
|
||||
[]string{"."},
|
||||
),
|
||||
},
|
||||
},
|
||||
wantConstraints: constraintsCollection{
|
||||
"github.com/sdboyer/deptest": []projectConstraint{
|
||||
{"github.com/JackyChiu/deptest", ver08},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
h := test.NewHelper(t)
|
||||
|
@ -423,6 +440,65 @@ 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
|
||||
|
|
Загрузка…
Ссылка в новой задаче