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

Commit d2109d3

Browse files
committed
Skip duplicate and sub-package imports
- Adds skipping duplicate and sub-package imports using projectExistsInLock(). - Adds tests for missing Rev in godep import package list. - Removes `Name` field from godepImporter.
1 parent f27c34e commit d2109d3

File tree

2 files changed

+126
-9
lines changed

2 files changed

+126
-9
lines changed

cmd/dep/godep_importer.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func newGodepImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *g
3636
}
3737

3838
type godepJSON struct {
39-
Name string `json:"ImportPath"`
4039
Imports []godepPackage `json:"Deps"`
4140
}
4241

@@ -101,6 +100,18 @@ func (g *godepImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, e
101100
return nil, nil, err
102101
}
103102

103+
// Obtain ProjectRoot. Required for avoiding sub-package imports.
104+
ip, err := g.sm.DeduceProjectRoot(pkg.ImportPath)
105+
if err != nil {
106+
return nil, nil, err
107+
}
108+
pkg.ImportPath = string(ip)
109+
110+
// Check if it already existing in locked projects
111+
if projectExistsInLock(lock, pkg.ImportPath) {
112+
continue
113+
}
114+
104115
// Rev must not be empty
105116
if pkg.Rev == "" {
106117
err := errors.New("Invalid godep configuration, Rev is required")
@@ -133,7 +144,7 @@ func (g *godepImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, e
133144
if err != nil {
134145
return nil, nil, err
135146
}
136-
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint}
147+
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Constraint: pc.Constraint}
137148
}
138149

139150
lp := g.buildLockedProject(pkg)
@@ -167,3 +178,15 @@ func (g *godepImporter) buildLockedProject(pkg godepPackage) gps.LockedProject {
167178
feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger)
168179
return gps.NewLockedProject(pi, version, nil)
169180
}
181+
182+
// projectExistsInLock checks if the given import path already existing in
183+
// locked projects.
184+
func projectExistsInLock(l *dep.Lock, ip string) bool {
185+
for _, lp := range l.P {
186+
if ip == string(lp.Ident().ProjectRoot) {
187+
return true
188+
}
189+
}
190+
191+
return false
192+
}

cmd/dep/godep_importer_test.go

+101-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"testing"
1212

13+
"github.com/golang/dep"
1314
"github.com/golang/dep/internal/gps"
1415
"github.com/golang/dep/internal/test"
1516
"github.com/pkg/errors"
@@ -69,7 +70,6 @@ func TestGodepConfig_Import(t *testing.T) {
6970
func TestGodepConfig_JsonLoad(t *testing.T) {
7071
// This is same as cmd/dep/testdata/Godeps.json
7172
wantJSON := godepJSON{
72-
Name: "github.com/golang/notexist",
7373
Imports: []godepPackage{
7474
{
7575
ImportPath: "github.com/sdboyer/deptest",
@@ -98,10 +98,6 @@ func TestGodepConfig_JsonLoad(t *testing.T) {
9898
t.Fatalf("Error while loading... %v", err)
9999
}
100100

101-
if g.json.Name != wantJSON.Name {
102-
t.Fatalf("Expected project name to be %v, but got %v", wantJSON.Name, g.json.Name)
103-
}
104-
105101
if !equalImports(g.json.Imports, wantJSON.Imports) {
106102
t.Fatalf("Expected imports to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.json.Imports, wantJSON.Imports)
107103
}
@@ -118,7 +114,6 @@ func TestGodepConfig_ConvertProject(t *testing.T) {
118114

119115
g := newGodepImporter(discardLogger, true, sm)
120116
g.json = godepJSON{
121-
Name: "github.com/foo/bar",
122117
Imports: []godepPackage{
123118
{
124119
ImportPath: "github.com/sdboyer/deptest",
@@ -178,7 +173,6 @@ func TestGodepConfig_ConvertProject_EmptyComment(t *testing.T) {
178173

179174
g := newGodepImporter(discardLogger, true, sm)
180175
g.json = godepJSON{
181-
Name: "github.com/foo/bar",
182176
Imports: []godepPackage{
183177
{
184178
ImportPath: "github.com/sdboyer/deptest",
@@ -237,6 +231,106 @@ func TestGodepConfig_Convert_BadInput_EmptyPackageName(t *testing.T) {
237231
}
238232
}
239233

234+
func TestGodepConfig_Convert_BadInput_EmptyRev(t *testing.T) {
235+
h := test.NewHelper(t)
236+
defer h.Cleanup()
237+
h.TempDir("src")
238+
239+
ctx := newTestContext(h)
240+
sm, err := ctx.SourceManager()
241+
h.Must(err)
242+
defer sm.Release()
243+
244+
g := newGodepImporter(discardLogger, true, sm)
245+
g.json = godepJSON{
246+
Imports: []godepPackage{
247+
{
248+
ImportPath: "github.com/sdboyer/deptest",
249+
},
250+
},
251+
}
252+
253+
_, _, err = g.convert("")
254+
if err == nil {
255+
t.Fatal("Expected conversion to fail because the Rev is empty")
256+
}
257+
}
258+
259+
func TestGodepConfig_Convert_SubPackages(t *testing.T) {
260+
h := test.NewHelper(t)
261+
defer h.Cleanup()
262+
h.TempDir("src")
263+
264+
ctx := newTestContext(h)
265+
sm, err := ctx.SourceManager()
266+
h.Must(err)
267+
defer sm.Release()
268+
269+
g := newGodepImporter(discardLogger, true, sm)
270+
g.json = godepJSON{
271+
Imports: []godepPackage{
272+
{
273+
ImportPath: "github.com/sdboyer/deptest",
274+
Rev: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
275+
},
276+
{
277+
ImportPath: "github.com/sdboyer/deptest/foo",
278+
Rev: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
279+
},
280+
},
281+
}
282+
283+
manifest, lock, err := g.convert("")
284+
if err != nil {
285+
t.Fatal(err)
286+
}
287+
288+
if _, present := manifest.Constraints["github.com/sdboyer/deptest"]; !present {
289+
t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest'")
290+
}
291+
292+
if _, present := manifest.Constraints["github.com/sdboyer/deptest/foo"]; present {
293+
t.Fatal("Expected the manifest to not have a dependency for 'github.com/sdboyer/deptest/foo'")
294+
}
295+
296+
if len(lock.P) != 1 {
297+
t.Fatalf("Expected lock to have 1 project, got %v", len(lock.P))
298+
}
299+
300+
if string(lock.P[0].Ident().ProjectRoot) != "github.com/sdboyer/deptest" {
301+
t.Fatal("Expected lock to have 'github.com/sdboyer/deptest'")
302+
}
303+
}
304+
305+
func TestGodepConfig_ProjectExistsInLock(t *testing.T) {
306+
lock := &dep.Lock{}
307+
pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}
308+
ver := gps.NewVersion("v1.0.0")
309+
lock.P = append(lock.P, gps.NewLockedProject(pi, ver, nil))
310+
311+
cases := []struct {
312+
importPath string
313+
want bool
314+
}{
315+
{
316+
importPath: "github.com/sdboyer/deptest",
317+
want: true,
318+
},
319+
{
320+
importPath: "github.com/golang/notexist",
321+
want: false,
322+
},
323+
}
324+
325+
for _, c := range cases {
326+
result := projectExistsInLock(lock, c.importPath)
327+
328+
if result != c.want {
329+
t.Fatalf("projectExistsInLock result is not as expected: \n\t(GOT) %v \n\t(WNT) %v", result, c.want)
330+
}
331+
}
332+
}
333+
240334
// Compares two slices of godepPackage and checks if they are equal.
241335
func equalImports(a, b []godepPackage) bool {
242336

0 commit comments

Comments
 (0)