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

Commit 4f22124

Browse files
spenczarcarolynvs
authored andcommitted
Add importer for github.com/LK4D4/vndr (#978)
1 parent c20daf3 commit 4f22124

12 files changed

+545
-22
lines changed

cmd/dep/glide_importer_test.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"github.com/pkg/errors"
1919
)
2020

21-
const testGlideProjectRoot = "github.com/golang/notexist"
22-
2321
var (
2422
discardLogger = log.New(ioutil.Discard, "", 0)
2523
)
@@ -110,7 +108,7 @@ func TestGlideConfig_Convert(t *testing.T) {
110108
yaml: glideYaml{
111109
ExcludeDirs: []string{"samples"},
112110
},
113-
projectRoot: testGlideProjectRoot,
111+
projectRoot: testProjectRoot,
114112
wantIgnoreCount: 1,
115113
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
116114
},
@@ -119,15 +117,15 @@ func TestGlideConfig_Convert(t *testing.T) {
119117
Name: "github.com/golang/mismatched-package-name",
120118
ExcludeDirs: []string{"samples"},
121119
},
122-
projectRoot: testGlideProjectRoot,
120+
projectRoot: testProjectRoot,
123121
wantIgnoreCount: 1,
124122
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
125123
},
126124
"bad input, empty package name": {
127125
yaml: glideYaml{
128126
Imports: []glidePackage{{Name: ""}},
129127
},
130-
projectRoot: testGlideProjectRoot,
128+
projectRoot: testProjectRoot,
131129
wantConvertErr: true,
132130
},
133131
}
@@ -243,10 +241,10 @@ func TestGlideConfig_Import(t *testing.T) {
243241
h.Must(err)
244242
defer sm.Release()
245243

246-
h.TempDir(filepath.Join("src", testGlideProjectRoot))
247-
h.TempCopy(filepath.Join(testGlideProjectRoot, glideYamlName), "glide/glide.yaml")
248-
h.TempCopy(filepath.Join(testGlideProjectRoot, glideLockName), "glide/glide.lock")
249-
projectRoot := h.Path(testGlideProjectRoot)
244+
h.TempDir(filepath.Join("src", testProjectRoot))
245+
h.TempCopy(filepath.Join(testProjectRoot, glideYamlName), "glide/glide.yaml")
246+
h.TempCopy(filepath.Join(testProjectRoot, glideLockName), "glide/glide.lock")
247+
projectRoot := h.Path(testProjectRoot)
250248

251249
// Capture stderr so we can verify output
252250
verboseOutput := &bytes.Buffer{}
@@ -257,7 +255,7 @@ func TestGlideConfig_Import(t *testing.T) {
257255
t.Fatal("Expected the importer to detect the glide configuration files")
258256
}
259257

260-
m, l, err := g.Import(projectRoot, testGlideProjectRoot)
258+
m, l, err := g.Import(projectRoot, testProjectRoot)
261259
h.Must(err)
262260

263261
if m == nil {
@@ -291,16 +289,16 @@ func TestGlideConfig_Import_MissingLockFile(t *testing.T) {
291289
h.Must(err)
292290
defer sm.Release()
293291

294-
h.TempDir(filepath.Join("src", testGlideProjectRoot))
295-
h.TempCopy(filepath.Join(testGlideProjectRoot, glideYamlName), "glide/glide.yaml")
296-
projectRoot := h.Path(testGlideProjectRoot)
292+
h.TempDir(filepath.Join("src", testProjectRoot))
293+
h.TempCopy(filepath.Join(testProjectRoot, glideYamlName), "glide/glide.yaml")
294+
projectRoot := h.Path(testProjectRoot)
297295

298296
g := newGlideImporter(ctx.Err, true, sm)
299297
if !g.HasDepMetadata(projectRoot) {
300298
t.Fatal("The glide importer should gracefully handle when only glide.yaml is present")
301299
}
302300

303-
m, l, err := g.Import(projectRoot, testGlideProjectRoot)
301+
m, l, err := g.Import(projectRoot, testProjectRoot)
304302
h.Must(err)
305303

306304
if m == nil {
@@ -340,7 +338,7 @@ func TestGlideConfig_Convert_WarnsForUnusedFields(t *testing.T) {
340338
Imports: []glidePackage{pkg},
341339
}
342340

343-
_, _, err = g.convert(testGlideProjectRoot)
341+
_, _, err = g.convert(testProjectRoot)
344342
if err != nil {
345343
t.Fatal(err)
346344
}

cmd/dep/godep_importer_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18-
const testGodepProjectRoot = "github.com/golang/notexist"
18+
const testProjectRoot = "github.com/golang/notexist"
1919

2020
func TestGodepConfig_Convert(t *testing.T) {
2121
testCases := map[string]struct {
@@ -198,10 +198,10 @@ func TestGodepConfig_Import(t *testing.T) {
198198
cacheDir := "gps-repocache"
199199
h.TempDir(cacheDir)
200200
h.TempDir("src")
201-
h.TempDir(filepath.Join("src", testGodepProjectRoot))
202-
h.TempCopy(filepath.Join(testGodepProjectRoot, godepPath), "godep/Godeps.json")
201+
h.TempDir(filepath.Join("src", testProjectRoot))
202+
h.TempCopy(filepath.Join(testProjectRoot, godepPath), "godep/Godeps.json")
203203

204-
projectRoot := h.Path(testGodepProjectRoot)
204+
projectRoot := h.Path(testProjectRoot)
205205
sm, err := gps.NewSourceManager(h.Path(cacheDir))
206206
h.Must(err)
207207
defer sm.Release()
@@ -215,7 +215,7 @@ func TestGodepConfig_Import(t *testing.T) {
215215
t.Fatal("Expected the importer to detect godep configuration file")
216216
}
217217

218-
m, l, err := g.Import(projectRoot, testGodepProjectRoot)
218+
m, l, err := g.Import(projectRoot, testProjectRoot)
219219
h.Must(err)
220220

221221
if m == nil {
@@ -261,9 +261,9 @@ func TestGodepConfig_JsonLoad(t *testing.T) {
261261

262262
ctx := newTestContext(h)
263263

264-
h.TempCopy(filepath.Join(testGodepProjectRoot, godepPath), "godep/Godeps.json")
264+
h.TempCopy(filepath.Join(testProjectRoot, godepPath), "godep/Godeps.json")
265265

266-
projectRoot := h.Path(testGodepProjectRoot)
266+
projectRoot := h.Path(testProjectRoot)
267267

268268
g := newGodepImporter(ctx.Err, true, nil)
269269
err := g.load(projectRoot)

cmd/dep/root_analyzer.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
7373
importers := []importer{
7474
newGlideImporter(logger, a.ctx.Verbose, a.sm),
7575
newGodepImporter(logger, a.ctx.Verbose, a.sm),
76+
newVndrImporter(logger, a.ctx.Verbose, a.sm),
7677
}
7778

7879
for _, i := range importers {

cmd/dep/testdata/harness_tests/init/vndr/case1/final/Gopkg.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptestdos"
4+
version = "2.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/sdboyer/deptestdos"
11+
)
12+
13+
func main() {
14+
var x deptestdos.Bar
15+
fmt.Println(x)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Comment on its own line
2+
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f https://github.com/sdboyer/deptest.git
3+
github.com/sdboyer/deptestdos v2.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"error-expected": "",
6+
"gopath-initial": {
7+
"github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f"
8+
},
9+
"vendor-final": [
10+
"github.com/sdboyer/deptest",
11+
"github.com/sdboyer/deptestdos"
12+
]
13+
}

cmd/dep/testdata/vndr/golden.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Detected vndr configuration file...
2+
Converting from vendor.conf...
3+
Using 3f4c3bea144e112a69bbe5d8d01c1b09a544253f as initial hint for imported dep github.com/sdboyer/deptest
4+
Trying v0.8.1 (3f4c3be) as initial lock for imported dep github.com/sdboyer/deptest
5+
Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos
6+
Trying * (v2.0.0) as initial lock for imported dep github.com/sdboyer/deptestdos

cmd/dep/testdata/vndr/vendor.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f https://github.com/sdboyer/deptest.git # trailing comment
2+
# line comment
3+
4+
github.com/sdboyer/deptestdos v2.0.0 # trailing comment

cmd/dep/vndr_importer.go

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"bufio"
9+
"log"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
14+
"github.com/golang/dep"
15+
fb "github.com/golang/dep/internal/feedback"
16+
"github.com/golang/dep/internal/gps"
17+
"github.com/pkg/errors"
18+
)
19+
20+
func vndrFile(dir string) string {
21+
return filepath.Join(dir, "vendor.conf")
22+
}
23+
24+
type vndrImporter struct {
25+
packages []vndrPackage
26+
27+
logger *log.Logger
28+
verbose bool
29+
sm gps.SourceManager
30+
}
31+
32+
func newVndrImporter(log *log.Logger, verbose bool, sm gps.SourceManager) *vndrImporter {
33+
return &vndrImporter{
34+
logger: log,
35+
verbose: verbose,
36+
sm: sm,
37+
}
38+
}
39+
40+
func (v *vndrImporter) Name() string { return "vndr" }
41+
42+
func (v *vndrImporter) HasDepMetadata(dir string) bool {
43+
_, err := os.Stat(vndrFile(dir))
44+
return err == nil
45+
}
46+
47+
func (v *vndrImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
48+
v.logger.Println("Detected vndr configuration file...")
49+
50+
err := v.loadVndrFile(dir)
51+
if err != nil {
52+
return nil, nil, errors.Wrapf(err, "unable to load vndr file")
53+
}
54+
55+
return v.convert(pr)
56+
}
57+
58+
func (v *vndrImporter) loadVndrFile(dir string) error {
59+
v.logger.Printf("Converting from vendor.conf...")
60+
61+
f, err := os.Open(vndrFile(dir))
62+
if err != nil {
63+
return errors.Wrapf(err, "Unable to open %s", vndrFile(dir))
64+
}
65+
defer f.Close()
66+
67+
scanner := bufio.NewScanner(f)
68+
for scanner.Scan() {
69+
pkg, err := parseVndrLine(scanner.Text())
70+
if err != nil {
71+
return errors.Wrapf(err, "unable to parse line")
72+
}
73+
if pkg == nil {
74+
// Could be an empty line or one which is just a comment
75+
continue
76+
}
77+
v.packages = append(v.packages, *pkg)
78+
}
79+
80+
if scanner.Err() != nil {
81+
return errors.Wrapf(err, "unable to read %s", vndrFile(dir))
82+
}
83+
84+
return nil
85+
}
86+
87+
func (v *vndrImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
88+
var (
89+
manifest = &dep.Manifest{
90+
Constraints: make(gps.ProjectConstraints),
91+
}
92+
lock = &dep.Lock{}
93+
err error
94+
)
95+
96+
for _, pkg := range v.packages {
97+
pc := gps.ProjectConstraint{
98+
Ident: gps.ProjectIdentifier{
99+
ProjectRoot: gps.ProjectRoot(pkg.importPath),
100+
Source: pkg.repository,
101+
},
102+
}
103+
pc.Constraint, err = v.sm.InferConstraint(pkg.revision, pc.Ident)
104+
if err != nil {
105+
return nil, nil, errors.Wrapf(err, "Unable to interpret revision specifier '%s' for package %s", pkg.importPath, pkg.revision)
106+
}
107+
108+
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{
109+
Source: pc.Ident.Source,
110+
Constraint: pc.Constraint,
111+
}
112+
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(v.logger)
113+
114+
revision := gps.Revision(pkg.revision)
115+
version, err := lookupVersionForLockedProject(pc.Ident, pc.Constraint, revision, v.sm)
116+
if err != nil {
117+
v.logger.Println(err.Error())
118+
}
119+
120+
lp := gps.NewLockedProject(pc.Ident, version, nil)
121+
122+
lock.P = append(lock.P, lp)
123+
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(v.logger)
124+
}
125+
126+
return manifest, lock, nil
127+
}
128+
129+
type vndrPackage struct {
130+
importPath string
131+
revision string
132+
repository string
133+
}
134+
135+
func parseVndrLine(line string) (*vndrPackage, error) {
136+
commentIdx := strings.Index(line, "#")
137+
if commentIdx >= 0 {
138+
line = line[:commentIdx]
139+
}
140+
line = strings.TrimSpace(line)
141+
142+
if line == "" {
143+
return nil, nil
144+
}
145+
146+
parts := strings.Fields(line)
147+
148+
if !(len(parts) == 2 || len(parts) == 3) {
149+
return nil, errors.Errorf("invalid config format: %q", line)
150+
}
151+
152+
pkg := &vndrPackage{
153+
importPath: parts[0],
154+
revision: parts[1],
155+
}
156+
if len(parts) == 3 {
157+
pkg.repository = parts[2]
158+
}
159+
160+
return pkg, nil
161+
}

0 commit comments

Comments
 (0)