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

Commit ba11bbe

Browse files
committed
First pass at a gb manifest importer
1 parent 0fa775c commit ba11bbe

File tree

4 files changed

+224
-1
lines changed

4 files changed

+224
-1
lines changed

cmd/dep/gb_importer.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,34 @@ func (i *gbImporter) buildProjectConstraint(pkg gbDependency) (pc gps.ProjectCon
122122
return
123123
}
124124

125+
/*
126+
gb's vendor plugin (gb vendor), which manages the vendor tree and manifest
127+
file, supports fetching by a specific tag or revision, but if you specify
128+
either of those it's a detached checkout and the "branch" field is HEAD.
129+
The only time the "branch" field is not "HEAD" is if you do not specify a
130+
tag or revision, otherwise it's either "master" or the value of the -branch
131+
flag
132+
133+
This means that, generally, the only possible "constraint" we can really specify is
134+
the branch name if the branch name is not HEAD. Otherwise, it's a specific revision.
135+
136+
However, if we can infer a tag that points to the revision or the branch, we may be able
137+
to use that as the constraint
138+
*/
139+
125140
pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Importpath), Source: pkg.Repository}
126-
pc.Constraint, err = i.sm.InferConstraint(pkg.Revision, pc.Ident)
141+
142+
// The default constraint is just a revision
143+
var revision = gps.Revision(pkg.Revision)
144+
145+
// See if we can get a version from that constraint
146+
version, err := lookupVersionForLockedProject(pc.Ident, revision, revision, i.sm)
147+
if err != nil {
148+
return
149+
}
150+
151+
// And now try to infer a constraint from the returned version
152+
pc.Constraint, err = i.sm.InferConstraint(version.String(), pc.Ident)
127153
if err != nil {
128154
return
129155
}

cmd/dep/gb_importer_test.go

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
"bytes"
9+
"log"
10+
"path/filepath"
11+
"testing"
12+
13+
"github.com/golang/dep/internal/gps"
14+
"github.com/golang/dep/internal/test"
15+
"github.com/pkg/errors"
16+
)
17+
18+
const testGbProjectRoot = "github.com/golang/notexist"
19+
20+
func TestGbConfig_Import(t *testing.T) {
21+
h := test.NewHelper(t)
22+
defer h.Cleanup()
23+
24+
ctx := newTestContext(h)
25+
sm, err := ctx.SourceManager()
26+
h.Must(err)
27+
defer sm.Release()
28+
29+
h.TempDir(filepath.Join("src", testGbProjectRoot, "vendor"))
30+
h.TempCopy(filepath.Join(testGbProjectRoot, "vendor", "manifest"), "gb/manifest")
31+
projectRoot := h.Path(testGbProjectRoot)
32+
33+
// Capture stderr so we can verify output
34+
verboseOutput := &bytes.Buffer{}
35+
ctx.Err = log.New(verboseOutput, "", 0)
36+
37+
g := newGbImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run
38+
if !g.HasDepMetadata(projectRoot) {
39+
t.Fatal("Expected the importer to detect the gb manifest file")
40+
}
41+
42+
m, l, err := g.Import(projectRoot, testGbProjectRoot)
43+
h.Must(err)
44+
45+
if m == nil {
46+
t.Fatal("Expected the manifest to be generated")
47+
}
48+
49+
if l == nil {
50+
t.Fatal("Expected the lock to be generated")
51+
}
52+
53+
goldenFile := "gb/golden.txt"
54+
got := verboseOutput.String()
55+
want := h.GetTestFileString(goldenFile)
56+
if want != got {
57+
if *test.UpdateGolden {
58+
if err := h.WriteTestFile(goldenFile, got); err != nil {
59+
t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
60+
}
61+
} else {
62+
t.Fatalf("expected %s, got %s", want, got)
63+
}
64+
}
65+
}
66+
67+
func TestGbConfig_Convert_Project(t *testing.T) {
68+
h := test.NewHelper(t)
69+
defer h.Cleanup()
70+
71+
ctx := newTestContext(h)
72+
sm, err := ctx.SourceManager()
73+
h.Must(err)
74+
defer sm.Release()
75+
76+
pkg := "github.com/sdboyer/deptest"
77+
repo := "https://github.com/sdboyer/deptest.git"
78+
79+
g := newGbImporter(ctx.Err, true, sm)
80+
g.manifest = gbManifest{
81+
Dependencies: []gbDependency{
82+
{
83+
Importpath: pkg,
84+
Repository: repo,
85+
Revision: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
86+
},
87+
},
88+
}
89+
90+
manifest, lock, err := g.convert(testGbProjectRoot)
91+
if err != nil {
92+
t.Fatal(err)
93+
}
94+
95+
d, ok := manifest.Constraints[gps.ProjectRoot(pkg)]
96+
if !ok {
97+
t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none")
98+
}
99+
100+
wantC := "^1.0.0"
101+
gotC := d.Constraint.String()
102+
if gotC != wantC {
103+
t.Fatalf("Expected manifest constraint to be %s, got %s", wantC, gotC)
104+
}
105+
106+
gotS := d.Source
107+
if gotS != repo {
108+
t.Fatalf("Expected manifest source to be %s, got %s", repo, gotS)
109+
}
110+
111+
wantP := 1
112+
gotP := len(lock.P)
113+
if gotP != 1 {
114+
t.Fatalf("Expected the lock to contain %d project but got %d", wantP, gotP)
115+
}
116+
117+
p := lock.P[0]
118+
gotPr := string(p.Ident().ProjectRoot)
119+
if gotPr != pkg {
120+
t.Fatalf("Expected the lock to have a project for %s but got '%s'", pkg, gotPr)
121+
}
122+
123+
gotS = p.Ident().Source
124+
if gotS != repo {
125+
t.Fatalf("Expected locked source to be %s, got '%s'", repo, gotS)
126+
}
127+
128+
lv := p.Version()
129+
lpv, ok := lv.(gps.PairedVersion)
130+
if !ok {
131+
t.Fatalf("Expected locked version to be a PairedVersion but got %T", lv)
132+
}
133+
134+
wantRev := "ff2948a2ac8f538c4ecd55962e919d1e13e74baf"
135+
gotRev := lpv.Revision().String()
136+
if gotRev != wantRev {
137+
t.Fatalf("Expected locked revision to be %s, got %s", wantRev, gotRev)
138+
}
139+
140+
wantV := "v1.0.0"
141+
gotV := lpv.String()
142+
if gotV != wantV {
143+
t.Fatalf("Expected locked version to be %s, got %s", wantV, gotV)
144+
}
145+
}
146+
147+
func TestGbConfig_Convert_BadInput_EmptyPackageName(t *testing.T) {
148+
h := test.NewHelper(t)
149+
defer h.Cleanup()
150+
151+
ctx := newTestContext(h)
152+
sm, err := ctx.SourceManager()
153+
h.Must(err)
154+
defer sm.Release()
155+
156+
g := newGbImporter(ctx.Err, true, sm)
157+
g.manifest = gbManifest{
158+
Dependencies: []gbDependency{{Importpath: ""}},
159+
}
160+
161+
_, _, err = g.convert(testGbProjectRoot)
162+
if err == nil {
163+
t.Fatal("Expected conversion to fail because the package name is empty")
164+
}
165+
}

cmd/dep/testdata/gb/golden.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Detected gb manifest file...
2+
Converting from gb manifest...
3+
Using master as initial constraint for imported dep github.com/kr/fs
4+
Trying master (2788f0d) as initial lock for imported dep github.com/kr/fs
5+
Using f234c3c6540c0358b1802f7fd90c0879af9232eb as initial hint for imported dep github.com/pkg/sftp
6+
Using ^1.0.0 as initial constraint for imported dep github.com/sdboyer/deptest
7+
Trying v1.0.0 (ff2948a) as initial lock for imported dep github.com/sdboyer/deptest

cmd/dep/testdata/gb/manifest

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": 0,
3+
"dependencies": [
4+
{
5+
"importpath": "github.com/kr/fs",
6+
"repository": "https://github.com/kr/fs",
7+
"revision": "2788f0dbd16903de03cb8186e5c7d97b69ad387b",
8+
"branch": "master",
9+
"path": ""
10+
},
11+
{
12+
"importpath": "github.com/pkg/sftp",
13+
"repository": "https://github.com/pkg/sftp",
14+
"revision": "f234c3c6540c0358b1802f7fd90c0879af9232eb",
15+
"branch": "master",
16+
"path": ""
17+
},
18+
{
19+
"importpath": "github.com/sdboyer/deptest",
20+
"repository": "https://github.com/sdboyer/deptest",
21+
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
22+
"branch": "HEAD"
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)