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

Commit 53960fe

Browse files
committed
cmd/dep: Move importers under internal/importers
1 parent 6890c50 commit 53960fe

30 files changed

+1065
-944
lines changed

cmd/dep/base_importer_test.go

Lines changed: 0 additions & 571 deletions
This file was deleted.

cmd/dep/ensure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
619619
// could have been converted into a source by the solver.
620620
if proj.Ident().ProjectRoot == pr {
621621
found = true
622-
pp = getProjectPropertiesFromVersion(proj.Version())
622+
pp = gps.GetProjectPropertiesFromVersion(proj.Version())
623623
break
624624
}
625625
}

cmd/dep/gopath_scanner.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,6 @@ func contains(a []string, b string) bool {
150150
return false
151151
}
152152

153-
// getProjectPropertiesFromVersion takes a gps.Version and returns a proper
154-
// gps.ProjectProperties with Constraint value based on the provided version.
155-
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {
156-
pp := gps.ProjectProperties{}
157-
158-
// extract version and ignore if it's revision only
159-
switch tv := v.(type) {
160-
case gps.PairedVersion:
161-
v = tv.Unpair()
162-
case gps.Revision:
163-
return pp
164-
}
165-
166-
switch v.Type() {
167-
case gps.IsBranch, gps.IsVersion:
168-
pp.Constraint = v
169-
case gps.IsSemver:
170-
c, err := gps.NewSemverConstraintIC(v.String())
171-
if err != nil {
172-
panic(err)
173-
}
174-
pp.Constraint = c
175-
}
176-
177-
return pp
178-
}
179-
180153
type projectData struct {
181154
constraints gps.ProjectConstraints // constraints that could be found
182155
dependencies map[gps.ProjectRoot][]string // all dependencies (imports) found by project root
@@ -230,7 +203,7 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
230203
}
231204

232205
ondisk[pr] = v
233-
pp := getProjectPropertiesFromVersion(v)
206+
pp := gps.GetProjectPropertiesFromVersion(v)
234207
if pp.Constraint != nil || pp.Source != "" {
235208
constraints[pr] = pp
236209
}

cmd/dep/gopath_scanner_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package main
66

77
import (
8+
"io/ioutil"
9+
"log"
810
"reflect"
911
"testing"
1012

@@ -16,11 +18,24 @@ import (
1618
const testProject1 string = "github.com/sdboyer/deptest"
1719
const testProject2 string = "github.com/sdboyer/deptestdos"
1820

21+
// NewTestContext creates a unique context with its own GOPATH for a single test.
22+
func NewTestContext(h *test.Helper) *dep.Ctx {
23+
h.TempDir("src")
24+
pwd := h.Path(".")
25+
discardLogger := log.New(ioutil.Discard, "", 0)
26+
27+
return &dep.Ctx{
28+
GOPATH: pwd,
29+
Out: discardLogger,
30+
Err: discardLogger,
31+
}
32+
}
33+
1934
func TestGopathScanner_OverlayManifestConstraints(t *testing.T) {
2035
t.Parallel()
2136

2237
h := test.NewHelper(t)
23-
ctx := newTestContext(h)
38+
ctx := NewTestContext(h)
2439

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

7489
h := test.NewHelper(t)
75-
ctx := newTestContext(h)
90+
ctx := NewTestContext(h)
7691

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

166181
for _, c := range cases {
167-
actualProp := getProjectPropertiesFromVersion(c.version.(gps.Version))
182+
actualProp := gps.GetProjectPropertiesFromVersion(c.version.(gps.Version))
168183
if !reflect.DeepEqual(c.want, actualProp.Constraint) {
169184
t.Fatalf("Constraints are not as expected: \n\t(GOT) %v\n\t(WNT) %v", actualProp.Constraint, c.want)
170185
}

cmd/dep/init_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
package main
66

77
import (
8-
"testing"
9-
108
"path/filepath"
9+
"testing"
1110

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

21-
ctx := newTestContext(h)
20+
ctx := NewTestContext(h)
2221
sm, err := ctx.SourceManager()
2322
h.Must(err)
2423
defer sm.Release()

cmd/dep/prune_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package main
66

77
import (
8+
"io/ioutil"
9+
"log"
810
"path/filepath"
911
"reflect"
1012
"sort"
@@ -27,6 +29,8 @@ func TestCalculatePrune(t *testing.T) {
2729
filepath.FromSlash("github.com/keep/pkg/sub"),
2830
}
2931

32+
discardLogger := log.New(ioutil.Discard, "", 0)
33+
3034
got, err := calculatePrune(h.Path(vendorDir), toKeep, discardLogger)
3135
if err != nil {
3236
t.Fatal(err)

cmd/dep/root_analyzer.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@ import (
1111
"github.com/golang/dep"
1212
fb "github.com/golang/dep/internal/feedback"
1313
"github.com/golang/dep/internal/gps"
14+
"github.com/golang/dep/internal/importers"
1415
)
1516

16-
// importer handles importing configuration from other dependency managers into
17-
// the dep configuration format.
18-
type importer interface {
19-
Name() string
20-
Import(path string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error)
21-
HasDepMetadata(dir string) bool
22-
}
23-
2417
// rootAnalyzer supplies manifest/lock data from both dep and external tool's
2518
// configuration files.
2619
// * When used on the root project, it imports only from external tools.
@@ -66,14 +59,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
6659
logger = log.New(ioutil.Discard, "", 0)
6760
}
6861

69-
importers := []importer{
70-
newGlideImporter(logger, a.ctx.Verbose, a.sm),
71-
newGodepImporter(logger, a.ctx.Verbose, a.sm),
72-
newVndrImporter(logger, a.ctx.Verbose, a.sm),
73-
newGovendImporter(logger, a.ctx.Verbose, a.sm),
74-
}
75-
76-
for _, i := range importers {
62+
for _, i := range importers.BuildAll(logger, a.ctx.Verbose, a.sm) {
7763
if i.HasDepMetadata(dir) {
7864
a.ctx.Err.Printf("Importing configuration from %s. These are only initial constraints, and are further refined during the solve process.", i.Name())
7965
m, l, err := i.Import(dir, pr)
@@ -134,7 +120,7 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock,
134120
// New constraints: in new lock and dir dep but not in manifest
135121
if _, ok := a.directDeps[string(pr)]; ok {
136122
if _, ok := m.Constraints[pr]; !ok {
137-
pp := getProjectPropertiesFromVersion(y.Version())
123+
pp := gps.GetProjectPropertiesFromVersion(y.Version())
138124
if pp.Constraint != nil {
139125
m.Constraints[pr] = pp
140126
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}

internal/gps/identifier.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ type ProjectProperties struct {
167167
Constraint Constraint
168168
}
169169

170+
// GetProjectPropertiesFromVersion takes a Version and returns a proper
171+
// ProjectProperties with Constraint value based on the provided version.
172+
func GetProjectPropertiesFromVersion(v Version) ProjectProperties {
173+
pp := ProjectProperties{}
174+
175+
// extract version and ignore if it's revision only
176+
switch tv := v.(type) {
177+
case PairedVersion:
178+
v = tv.Unpair()
179+
case Revision:
180+
return pp
181+
}
182+
183+
switch v.Type() {
184+
case IsBranch, IsVersion:
185+
pp.Constraint = v
186+
case IsSemver:
187+
c, err := NewSemverConstraintIC(v.String())
188+
if err != nil {
189+
panic(err)
190+
}
191+
pp.Constraint = c
192+
}
193+
194+
return pp
195+
}
196+
170197
// bimodalIdentifiers are used to track work to be done in the unselected queue.
171198
type bimodalIdentifier struct {
172199
id ProjectIdentifier

0 commit comments

Comments
 (0)