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

Commit f40b53f

Browse files
committed
gb project importer
1 parent 0a63c47 commit f40b53f

File tree

11 files changed

+441
-1
lines changed

11 files changed

+441
-1
lines changed

cmd/dep/gb_importer.go

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
"encoding/json"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
"path/filepath"
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+
// gbImporter imports gb configuration into the dep configuration format.
21+
type gbImporter struct {
22+
manifest gbManifest
23+
logger *log.Logger
24+
verbose bool
25+
sm gps.SourceManager
26+
}
27+
28+
func newGbImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *gbImporter {
29+
return &gbImporter{
30+
logger: logger,
31+
verbose: verbose,
32+
sm: sm,
33+
}
34+
}
35+
36+
// gbManifest represents the manifest file for GB projects
37+
type gbManifest struct {
38+
Dependencies []gbDependency `json:"dependencies"`
39+
}
40+
41+
type gbDependency struct {
42+
Importpath string `json:"importpath"`
43+
Repository string `json:"repository"`
44+
45+
// All gb vendored dependencies have a specific revision
46+
Revision string `json:"revision"`
47+
48+
// Branch may be HEAD or an actual branch. In the case of HEAD, that means
49+
// the user vendored a dependency by specifying a tag or a specific revision
50+
// which results in a detached HEAD
51+
Branch string `json:"branch"`
52+
}
53+
54+
func (i *gbImporter) Name() string {
55+
return "gb"
56+
}
57+
58+
func (i *gbImporter) HasDepMetadata(dir string) bool {
59+
// gb stores the manifest in the vendor tree
60+
var m = filepath.Join(dir, "vendor", "manifest")
61+
if _, err := os.Stat(m); err != nil {
62+
return false
63+
}
64+
65+
return true
66+
}
67+
68+
func (i *gbImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
69+
err := i.load(dir)
70+
if err != nil {
71+
return nil, nil, err
72+
}
73+
74+
return i.convert(pr)
75+
}
76+
77+
// load the gb manifest
78+
func (i *gbImporter) load(projectDir string) error {
79+
i.logger.Println("Detected gb manifest file...")
80+
var mf = filepath.Join(projectDir, "vendor", "manifest")
81+
if i.verbose {
82+
i.logger.Printf(" Loading %s", mf)
83+
}
84+
85+
var buf []byte
86+
var err error
87+
if buf, err = ioutil.ReadFile(mf); err != nil {
88+
return errors.Wrapf(err, "Unable to read %s", mf)
89+
}
90+
if err := json.Unmarshal(buf, &i.manifest); err != nil {
91+
return errors.Wrapf(err, "Unable to parse %s", mf)
92+
}
93+
94+
return nil
95+
}
96+
97+
// convert the gb manifest into dep configuration files.
98+
func (i *gbImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
99+
i.logger.Println("Converting from gb manifest...")
100+
101+
manifest := &dep.Manifest{
102+
Constraints: make(gps.ProjectConstraints),
103+
}
104+
105+
lock := &dep.Lock{}
106+
107+
for _, pkg := range i.manifest.Dependencies {
108+
pc, lp, err := i.convertOne(pkg)
109+
if err != nil {
110+
return nil, nil, err
111+
}
112+
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint}
113+
lock.P = append(lock.P, lp)
114+
}
115+
116+
return manifest, lock, nil
117+
}
118+
119+
func (i *gbImporter) convertOne(pkg gbDependency) (pc gps.ProjectConstraint, lp gps.LockedProject, err error) {
120+
if pkg.Importpath == "" {
121+
err = errors.New("Invalid gb configuration, package import path is required")
122+
return
123+
}
124+
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+
140+
pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Importpath), Source: pkg.Repository}
141+
142+
// Generally, gb tracks revisions
143+
var revision = gps.Revision(pkg.Revision)
144+
145+
// But if the branch field is not "HEAD", we can use that as the initial constraint
146+
var constraint gps.Constraint
147+
if pkg.Branch != "HEAD" {
148+
constraint = gps.NewBranch(pkg.Branch)
149+
}
150+
151+
// See if we can get a version from that constraint
152+
version, err := lookupVersionForLockedProject(pc.Ident, constraint, revision, i.sm)
153+
if err != nil {
154+
// Log the error, but don't fail it. It's okay if we can't find a version
155+
i.logger.Println(err.Error())
156+
}
157+
158+
// And now try to infer a constraint from the returned version
159+
pc.Constraint, err = i.sm.InferConstraint(version.String(), pc.Ident)
160+
if err != nil {
161+
return
162+
}
163+
164+
lp = gps.NewLockedProject(pc.Ident, version, nil)
165+
166+
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(i.logger)
167+
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(i.logger)
168+
169+
return
170+
}

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/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+
newGbImporter(logger, a.ctx.Verbose, a.sm),
7677
}
7778

7879
for _, i := range importers {

cmd/dep/testdata/gb/golden.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Detected gb manifest file...
2+
Converting from gb manifest...
3+
Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos
4+
Trying v2.0.0 (5c60720) as initial lock for imported dep github.com/sdboyer/deptestdos
5+
Using ^1.0.0 as initial constraint for imported dep github.com/sdboyer/deptest
6+
Trying v1.0.0 (ff2948a) as initial lock for imported dep github.com/sdboyer/deptest

cmd/dep/testdata/gb/manifest

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": 0,
3+
"dependencies": [
4+
{
5+
"importpath": "github.com/sdboyer/deptestdos",
6+
"repository": "https://github.com/sdboyer/deptestdos",
7+
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
8+
"branch": "master"
9+
},
10+
{
11+
"importpath": "github.com/sdboyer/deptest",
12+
"repository": "https://github.com/sdboyer/deptest",
13+
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
14+
"branch": "HEAD"
15+
}
16+
]
17+
}

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

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)