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

Commit

Permalink
cmd/dep: Move importers under internal/importers
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs committed Sep 11, 2017
1 parent 28fb6b0 commit 6ec3f52
Show file tree
Hide file tree
Showing 28 changed files with 1,018 additions and 879 deletions.
574 changes: 0 additions & 574 deletions cmd/dep/base_importer_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/dep/gopath_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func contains(a []string, b string) bool {
return false
}

// getProjectPropertiesFromVersion takes a gps.Version and returns a proper
// gps.ProjectProperties with Constraint value based on the provided version.
// getProjectPropertiesFromVersion takes a Version and returns a proper
// ProjectProperties with Constraint value based on the provided version.
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {
pp := gps.ProjectProperties{}

Expand Down
19 changes: 17 additions & 2 deletions cmd/dep/gopath_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package main

import (
"io/ioutil"
"log"
"reflect"
"testing"

Expand All @@ -16,12 +18,25 @@ import (
const testProject1 string = "github.com/sdboyer/deptest"
const testProject2 string = "github.com/sdboyer/deptestdos"

// NewTestContext creates a unique context with its own GOPATH for a single test.
func NewTestContext(h *test.Helper) *dep.Ctx {
h.TempDir("src")
pwd := h.Path(".")
discardLogger := log.New(ioutil.Discard, "", 0)

return &dep.Ctx{
GOPATH: pwd,
Out: discardLogger,
Err: discardLogger,
}
}

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

ctx := newTestContext(h)
ctx := NewTestContext(h)

pi1 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject1)}
pi2 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject2)}
Expand Down Expand Up @@ -74,7 +89,7 @@ func TestGopathScanner_OverlayLockProjects(t *testing.T) {
h.Parallel()
defer h.Cleanup()

ctx := newTestContext(h)
ctx := NewTestContext(h)

rootM := dep.NewManifest()
pi1 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject1)}
Expand Down
5 changes: 2 additions & 3 deletions cmd/dep/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
package main

import (
"testing"

"path/filepath"
"testing"

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
Expand All @@ -18,7 +17,7 @@ func TestGetDirectDependencies_ConsolidatesRootProjects(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

ctx := newTestContext(h)
ctx := NewTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()
Expand Down
4 changes: 4 additions & 0 deletions cmd/dep/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package main

import (
"io/ioutil"
"log"
"path/filepath"
"reflect"
"sort"
Expand All @@ -27,6 +29,8 @@ func TestCalculatePrune(t *testing.T) {
filepath.FromSlash("github.com/keep/pkg/sub"),
}

discardLogger := log.New(ioutil.Discard, "", 0)

got, err := calculatePrune(h.Path(vendorDir), toKeep, discardLogger)
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 2 additions & 16 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ import (
"github.com/golang/dep"
fb "github.com/golang/dep/internal/feedback"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/importers"
)

// importer handles importing configuration from other dependency managers into
// the dep configuration format.
type importer interface {
Name() string
Import(path string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error)
HasDepMetadata(dir string) bool
}

// rootAnalyzer supplies manifest/lock data from both dep and external tool's
// configuration files.
// * When used on the root project, it imports only from external tools.
Expand Down Expand Up @@ -66,14 +59,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
logger = log.New(ioutil.Discard, "", 0)
}

importers := []importer{
newGlideImporter(logger, a.ctx.Verbose, a.sm),
newGodepImporter(logger, a.ctx.Verbose, a.sm),
newVndrImporter(logger, a.ctx.Verbose, a.sm),
newGovendImporter(logger, a.ctx.Verbose, a.sm),
}

for _, i := range importers {
for _, i := range importers.BuildAll(logger, a.ctx.Verbose, a.sm) {
if i.HasDepMetadata(dir) {
a.ctx.Err.Printf("Importing configuration from %s. These are only initial constraints, and are further refined during the solve process.", i.Name())
m, l, err := i.Import(dir, pr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main
package base

import (
"log"
Expand All @@ -13,29 +13,30 @@ import (
"github.com/pkg/errors"
)

// baseImporter provides a common implementation for importing from other
// Importer provides a common implementation for importing from other
// dependency managers.
type baseImporter struct {
logger *log.Logger
verbose bool
sm gps.SourceManager
manifest *dep.Manifest
lock *dep.Lock
type Importer struct {
sm gps.SourceManager

Logger *log.Logger
Verbose bool
Manifest *dep.Manifest
Lock *dep.Lock
}

// newBaseImporter creates a new baseImporter for embedding in an importer.
func newBaseImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *baseImporter {
return &baseImporter{
logger: logger,
verbose: verbose,
manifest: dep.NewManifest(),
lock: &dep.Lock{},
// NewImporter creates a new Importer for embedding in an importer.
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
return &Importer{
Logger: logger,
Verbose: verbose,
Manifest: dep.NewManifest(),
Lock: &dep.Lock{},
sm: sm,
}
}

// isTag determines if the specified value is a tag (plain or semver).
func (i *baseImporter) isTag(pi gps.ProjectIdentifier, value string) (bool, gps.Version, error) {
func (i *Importer) isTag(pi gps.ProjectIdentifier, value string) (bool, gps.Version, error) {
versions, err := i.sm.ListVersions(pi)
if err != nil {
return false, nil, errors.Wrapf(err, "unable to list versions for %s(%s)", pi.ProjectRoot, pi.Source)
Expand All @@ -58,7 +59,7 @@ func (i *baseImporter) isTag(pi gps.ProjectIdentifier, value string) (bool, gps.
// project based on the locked revision and the constraint from the manifest.
// First try matching the revision to a version, then try the constraint from the
// manifest, then finally the revision.
func (i *baseImporter) lookupVersionForLockedProject(pi gps.ProjectIdentifier, c gps.Constraint, rev gps.Revision) (gps.Version, error) {
func (i *Importer) lookupVersionForLockedProject(pi gps.ProjectIdentifier, c gps.Constraint, rev gps.Revision) (gps.Version, error) {
// Find the version that goes with this revision, if any
versions, err := i.sm.ListVersions(pi)
if err != nil {
Expand Down Expand Up @@ -98,9 +99,9 @@ func (i *baseImporter) lookupVersionForLockedProject(pi gps.ProjectIdentifier, c
return rev, nil
}

// importedPackage is a common intermediate representation of a package imported
// ImportedPackage is a common intermediate representation of a package imported
// from an external tool's configuration.
type importedPackage struct {
type ImportedPackage struct {
// Required. The package path, not necessarily the project root.
Name string

Expand All @@ -118,11 +119,11 @@ type importedPackage struct {
// for the same project root.
type importedProject struct {
Root gps.ProjectRoot
importedPackage
ImportedPackage
}

// loadPackages consolidates all package references into a set of project roots.
func (i *baseImporter) loadPackages(packages []importedPackage) ([]importedProject, error) {
func (i *Importer) loadPackages(packages []ImportedPackage) ([]importedProject, error) {
// preserve the original order of the packages so that messages that
// are printed as they are processed are in a consistent order.
orderedProjects := make([]importedProject, 0, len(packages))
Expand Down Expand Up @@ -161,7 +162,7 @@ func (i *baseImporter) loadPackages(packages []importedPackage) ([]importedProje
return orderedProjects, nil
}

// importPackages loads imported packages into the manifest and lock.
// ImportPackages loads imported packages into the manifest and lock.
// - defaultConstraintFromLock specifies if a constraint should be defaulted
// based on the locked version when there wasn't a constraint hint.
//
Expand All @@ -172,7 +173,7 @@ func (i *baseImporter) loadPackages(packages []importedPackage) ([]importedProje
// * Revision constraints are ignored.
// * Versions that don't satisfy the constraint, drop the constraint.
// * Untagged revisions ignore non-branch constraint hints.
func (i *baseImporter) importPackages(packages []importedPackage, defaultConstraintFromLock bool) (err error) {
func (i *Importer) ImportPackages(packages []ImportedPackage, defaultConstraintFromLock bool) (err error) {
projects, err := i.loadPackages(packages)
if err != nil {
return err
Expand Down Expand Up @@ -206,54 +207,54 @@ func (i *baseImporter) importPackages(packages []importedPackage, defaultConstra
version, err = i.lookupVersionForLockedProject(pc.Ident, pc.Constraint, revision)
if err != nil {
version = nil
i.logger.Println(err)
i.Logger.Println(err)
}
}

// Default the constraint based on the locked version
if defaultConstraintFromLock && prj.ConstraintHint == "" && version != nil {
props := getProjectPropertiesFromVersion(version)
if props.Constraint != nil {
pc.Constraint = props.Constraint
c := i.convertToConstraint(version)
if c != nil {
pc.Constraint = c
}
}
}

// Ignore pinned constraints
if i.isConstraintPinned(pc.Constraint) {
if i.verbose {
i.logger.Printf(" Ignoring pinned constraint %v for %v.\n", pc.Constraint, pc.Ident)
if i.Verbose {
i.Logger.Printf(" Ignoring pinned constraint %v for %v.\n", pc.Constraint, pc.Ident)
}
pc.Constraint = gps.Any()
}

// Ignore constraints which conflict with the locked revision, so that
// solve doesn't later change the revision to satisfy the constraint.
if !i.testConstraint(pc.Constraint, version) {
if i.verbose {
i.logger.Printf(" Ignoring constraint %v for %v because it would invalidate the locked version %v.\n", pc.Constraint, pc.Ident, version)
if i.Verbose {
i.Logger.Printf(" Ignoring constraint %v for %v because it would invalidate the locked version %v.\n", pc.Constraint, pc.Ident, version)
}
pc.Constraint = gps.Any()
}

i.manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{
i.Manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{
Source: pc.Ident.Source,
Constraint: pc.Constraint,
}
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(i.logger)
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(i.Logger)

if version != nil {
lp := gps.NewLockedProject(pc.Ident, version, nil)
i.lock.P = append(i.lock.P, lp)
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(i.logger)
i.Lock.P = append(i.Lock.P, lp)
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(i.Logger)
}
}

return nil
}

// isConstraintPinned returns if a constraint is pinned to a specific revision.
func (i *baseImporter) isConstraintPinned(c gps.Constraint) bool {
func (i *Importer) isConstraintPinned(c gps.Constraint) bool {
if version, isVersion := c.(gps.Version); isVersion {
switch version.Type() {
case gps.IsRevision, gps.IsVersion:
Expand All @@ -264,7 +265,7 @@ func (i *baseImporter) isConstraintPinned(c gps.Constraint) bool {
}

// testConstraint verifies that the constraint won't invalidate the locked version.
func (i *baseImporter) testConstraint(c gps.Constraint, v gps.Version) bool {
func (i *Importer) testConstraint(c gps.Constraint, v gps.Version) bool {
// Assume branch constraints are satisfied
if version, isVersion := c.(gps.Version); isVersion {
if version.Type() == gps.IsBranch {
Expand All @@ -275,3 +276,18 @@ func (i *baseImporter) testConstraint(c gps.Constraint, v gps.Version) bool {

return c.Matches(v)
}

// convertToConstraint turns a version into a constraint.
// Semver tags are converted to a range with the caret operator.
func (i *Importer) convertToConstraint(v gps.Version) gps.Constraint {
if v.Type() == gps.IsSemver {
c, err := gps.NewSemverConstraintIC(v.String())
if err != nil {
// This should never fail, because the type is semver.
// If it does fail somehow, don't let that impact the import.
return nil
}
return c
}
return v
}
Loading

0 comments on commit 6ec3f52

Please sign in to comment.