Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit b368973

Browse files
committed
Merge pull request #30 from odacremolbap/master
Fixes #29. Duplicated dependencies use only first occurrence
2 parents 00bd0b1 + f161c4d commit b368973

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

conf/yaml.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package conf
22

33
import (
4-
yaml "github.com/cloudfoundry-incubator/candiedyaml"
54
"os"
5+
6+
yaml "github.com/cloudfoundry-incubator/candiedyaml"
67
)
78

89
type Trash struct {
@@ -25,5 +26,20 @@ func Parse(path string) (*Trash, error) {
2526
if err := yaml.NewDecoder(file).Decode(trash); err != nil {
2627
return nil, err
2728
}
29+
trash.deleteDups()
2830
return trash, nil
2931
}
32+
33+
// deleteDups delete duplicate imports
34+
func (t *Trash) deleteDups() {
35+
seen := make(map[string]bool)
36+
uniq := make([]Import, 0, len(t.Imports))
37+
for _, i := range t.Imports {
38+
if _, ok := seen[i.Package]; ok {
39+
continue
40+
}
41+
uniq = append(uniq, i)
42+
seen[i.Package] = true
43+
}
44+
t.Imports = uniq
45+
}

conf/yaml_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package conf
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDuplicates(t *testing.T) {
8+
9+
testData := []struct {
10+
imports []Import
11+
duplicates int
12+
}{
13+
{[]Import{
14+
{"package1", "version1", ""},
15+
}, 0},
16+
{[]Import{
17+
{"package1", "version1", ""},
18+
{"package2", "version1", "repoA"},
19+
}, 0},
20+
{[]Import{
21+
{"package1", "version1", ""},
22+
{"package2", "version1", "repoA"},
23+
{"package1", "version1", ""},
24+
}, 1},
25+
{[]Import{
26+
{"package1", "version1", ""},
27+
{"package2", "version1", "repoA"},
28+
{"package1", "version1", ""},
29+
{"package1", "version1", ""},
30+
}, 2},
31+
{[]Import{
32+
{"package1", "version1", ""},
33+
{"package2", "version1", "repoA"},
34+
{"package1", "version1", ""},
35+
{"package1", "version1", ""},
36+
{"package2", "version2", "repoB"},
37+
{"package3", "version1", "repoA"},
38+
}, 3},
39+
}
40+
41+
for i, d := range testData {
42+
trash := Trash{"", d.imports}
43+
trash.deleteDups()
44+
45+
if d.duplicates != len(d.imports)-len(trash.Imports) {
46+
t.Errorf("Case %d failed: expected %d duplicates but removed %d", i, d.duplicates, len(d.imports)-len(trash.Imports))
47+
}
48+
49+
}
50+
51+
}

trash.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func vendor(keep bool, trashDir, dir string, trashConf *conf.Trash) error {
154154
}
155155
return nil
156156
}); err != nil {
157-
logrus.Error("Error stripping .git dirs: %s", err)
157+
logrus.Errorf("Error stripping .git dirs: %s", err)
158158
return err
159159
}
160160
}
@@ -339,7 +339,7 @@ func listImports(rootPackage, pkg string) <-chan util.Packages {
339339
if os.IsNotExist(err) {
340340
logrus.Debugf("listImports, pkgPath does not exist: %s", err)
341341
} else {
342-
logrus.Error("Error parsing imports, pkgPath: '%s', err: '%s'", pkgPath, err)
342+
logrus.Errorf("Error parsing imports, pkgPath: '%s', err: '%s'", pkgPath, err)
343343
}
344344
return
345345
}
@@ -521,7 +521,7 @@ func cleanup(dir string, trashConf *conf.Trash) error {
521521
logrus.Info("Trying to guess the root package from directory structure")
522522
srcPath := path.Join(dir, "..", "..", "..", "..", "src")
523523
if _, err := os.Stat(srcPath); err != nil {
524-
logrus.Fatal("It didn't work: '%s' does not exist or something: %s", srcPath, err)
524+
logrus.Fatalf("It didn't work: '%s' does not exist or something: %s", srcPath, err)
525525
}
526526
srcPath = filepath.Clean(srcPath)
527527
logrus.Debugf("srcPath: '%s'", srcPath)

0 commit comments

Comments
 (0)