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 9, 2017
1 parent 6890c50 commit 53960fe
Show file tree
Hide file tree
Showing 30 changed files with 1,065 additions and 944 deletions.
571 changes: 0 additions & 571 deletions cmd/dep/base_importer_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
// could have been converted into a source by the solver.
if proj.Ident().ProjectRoot == pr {
found = true
pp = getProjectPropertiesFromVersion(proj.Version())
pp = gps.GetProjectPropertiesFromVersion(proj.Version())
break
}
}
Expand Down
29 changes: 1 addition & 28 deletions cmd/dep/gopath_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,6 @@ 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.
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {
pp := gps.ProjectProperties{}

// extract version and ignore if it's revision only
switch tv := v.(type) {
case gps.PairedVersion:
v = tv.Unpair()
case gps.Revision:
return pp
}

switch v.Type() {
case gps.IsBranch, gps.IsVersion:
pp.Constraint = v
case gps.IsSemver:
c, err := gps.NewSemverConstraintIC(v.String())
if err != nil {
panic(err)
}
pp.Constraint = c
}

return pp
}

type projectData struct {
constraints gps.ProjectConstraints // constraints that could be found
dependencies map[gps.ProjectRoot][]string // all dependencies (imports) found by project root
Expand Down Expand Up @@ -230,7 +203,7 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
}

ondisk[pr] = v
pp := getProjectPropertiesFromVersion(v)
pp := gps.GetProjectPropertiesFromVersion(v)
if pp.Constraint != nil || pp.Source != "" {
constraints[pr] = pp
}
Expand Down
21 changes: 18 additions & 3 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,11 +18,24 @@ 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) {
t.Parallel()

h := test.NewHelper(t)
ctx := newTestContext(h)
ctx := NewTestContext(h)

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

h := test.NewHelper(t)
ctx := newTestContext(h)
ctx := NewTestContext(h)

rootM := dep.NewManifest()
pi1 := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(testProject1)}
Expand Down Expand Up @@ -164,7 +179,7 @@ func TestGetProjectPropertiesFromVersion(t *testing.T) {
}

for _, c := range cases {
actualProp := getProjectPropertiesFromVersion(c.version.(gps.Version))
actualProp := gps.GetProjectPropertiesFromVersion(c.version.(gps.Version))
if !reflect.DeepEqual(c.want, actualProp.Constraint) {
t.Fatalf("Constraints are not as expected: \n\t(GOT) %v\n\t(WNT) %v", actualProp.Constraint, c.want)
}
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
20 changes: 3 additions & 17 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 Expand Up @@ -134,7 +120,7 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock,
// New constraints: in new lock and dir dep but not in manifest
if _, ok := a.directDeps[string(pr)]; ok {
if _, ok := m.Constraints[pr]; !ok {
pp := getProjectPropertiesFromVersion(y.Version())
pp := gps.GetProjectPropertiesFromVersion(y.Version())
if pp.Constraint != nil {
m.Constraints[pr] = pp
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
Expand Down
27 changes: 27 additions & 0 deletions internal/gps/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ type ProjectProperties struct {
Constraint Constraint
}

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

// extract version and ignore if it's revision only
switch tv := v.(type) {
case PairedVersion:
v = tv.Unpair()
case Revision:
return pp
}

switch v.Type() {
case IsBranch, IsVersion:
pp.Constraint = v
case IsSemver:
c, err := NewSemverConstraintIC(v.String())
if err != nil {
panic(err)
}
pp.Constraint = c
}

return pp
}

// bimodalIdentifiers are used to track work to be done in the unselected queue.
type bimodalIdentifier struct {
id ProjectIdentifier
Expand Down
Loading

0 comments on commit 53960fe

Please sign in to comment.