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

Commit

Permalink
First pass at a gb manifest importer
Browse files Browse the repository at this point in the history
  • Loading branch information
avidal committed Jul 15, 2017
1 parent 0fa775c commit ba11bbe
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 1 deletion.
28 changes: 27 additions & 1 deletion cmd/dep/gb_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,34 @@ func (i *gbImporter) buildProjectConstraint(pkg gbDependency) (pc gps.ProjectCon
return
}

/*
gb's vendor plugin (gb vendor), which manages the vendor tree and manifest
file, supports fetching by a specific tag or revision, but if you specify
either of those it's a detached checkout and the "branch" field is HEAD.
The only time the "branch" field is not "HEAD" is if you do not specify a
tag or revision, otherwise it's either "master" or the value of the -branch
flag
This means that, generally, the only possible "constraint" we can really specify is
the branch name if the branch name is not HEAD. Otherwise, it's a specific revision.
However, if we can infer a tag that points to the revision or the branch, we may be able
to use that as the constraint
*/

pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Importpath), Source: pkg.Repository}
pc.Constraint, err = i.sm.InferConstraint(pkg.Revision, pc.Ident)

// The default constraint is just a revision
var revision = gps.Revision(pkg.Revision)

// See if we can get a version from that constraint
version, err := lookupVersionForLockedProject(pc.Ident, revision, revision, i.sm)
if err != nil {
return
}

// And now try to infer a constraint from the returned version
pc.Constraint, err = i.sm.InferConstraint(version.String(), pc.Ident)
if err != nil {
return
}
Expand Down
165 changes: 165 additions & 0 deletions cmd/dep/gb_importer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"bytes"
"log"
"path/filepath"
"testing"

"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/test"
"github.com/pkg/errors"
)

const testGbProjectRoot = "github.com/golang/notexist"

func TestGbConfig_Import(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

ctx := newTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

h.TempDir(filepath.Join("src", testGbProjectRoot, "vendor"))
h.TempCopy(filepath.Join(testGbProjectRoot, "vendor", "manifest"), "gb/manifest")
projectRoot := h.Path(testGbProjectRoot)

// Capture stderr so we can verify output
verboseOutput := &bytes.Buffer{}
ctx.Err = log.New(verboseOutput, "", 0)

g := newGbImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run
if !g.HasDepMetadata(projectRoot) {
t.Fatal("Expected the importer to detect the gb manifest file")
}

m, l, err := g.Import(projectRoot, testGbProjectRoot)
h.Must(err)

if m == nil {
t.Fatal("Expected the manifest to be generated")
}

if l == nil {
t.Fatal("Expected the lock to be generated")
}

goldenFile := "gb/golden.txt"
got := verboseOutput.String()
want := h.GetTestFileString(goldenFile)
if want != got {
if *test.UpdateGolden {
if err := h.WriteTestFile(goldenFile, got); err != nil {
t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
}
} else {
t.Fatalf("expected %s, got %s", want, got)
}
}
}

func TestGbConfig_Convert_Project(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

ctx := newTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

pkg := "github.com/sdboyer/deptest"
repo := "https://github.com/sdboyer/deptest.git"

g := newGbImporter(ctx.Err, true, sm)
g.manifest = gbManifest{
Dependencies: []gbDependency{
{
Importpath: pkg,
Repository: repo,
Revision: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
},
},
}

manifest, lock, err := g.convert(testGbProjectRoot)
if err != nil {
t.Fatal(err)
}

d, ok := manifest.Constraints[gps.ProjectRoot(pkg)]
if !ok {
t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none")
}

wantC := "^1.0.0"
gotC := d.Constraint.String()
if gotC != wantC {
t.Fatalf("Expected manifest constraint to be %s, got %s", wantC, gotC)
}

gotS := d.Source
if gotS != repo {
t.Fatalf("Expected manifest source to be %s, got %s", repo, gotS)
}

wantP := 1
gotP := len(lock.P)
if gotP != 1 {
t.Fatalf("Expected the lock to contain %d project but got %d", wantP, gotP)
}

p := lock.P[0]
gotPr := string(p.Ident().ProjectRoot)
if gotPr != pkg {
t.Fatalf("Expected the lock to have a project for %s but got '%s'", pkg, gotPr)
}

gotS = p.Ident().Source
if gotS != repo {
t.Fatalf("Expected locked source to be %s, got '%s'", repo, gotS)
}

lv := p.Version()
lpv, ok := lv.(gps.PairedVersion)
if !ok {
t.Fatalf("Expected locked version to be a PairedVersion but got %T", lv)
}

wantRev := "ff2948a2ac8f538c4ecd55962e919d1e13e74baf"
gotRev := lpv.Revision().String()
if gotRev != wantRev {
t.Fatalf("Expected locked revision to be %s, got %s", wantRev, gotRev)
}

wantV := "v1.0.0"
gotV := lpv.String()
if gotV != wantV {
t.Fatalf("Expected locked version to be %s, got %s", wantV, gotV)
}
}

func TestGbConfig_Convert_BadInput_EmptyPackageName(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

ctx := newTestContext(h)
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

g := newGbImporter(ctx.Err, true, sm)
g.manifest = gbManifest{
Dependencies: []gbDependency{{Importpath: ""}},
}

_, _, err = g.convert(testGbProjectRoot)
if err == nil {
t.Fatal("Expected conversion to fail because the package name is empty")
}
}
7 changes: 7 additions & 0 deletions cmd/dep/testdata/gb/golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Detected gb manifest file...
Converting from gb manifest...
Using master as initial constraint for imported dep github.com/kr/fs
Trying master (2788f0d) as initial lock for imported dep github.com/kr/fs
Using f234c3c6540c0358b1802f7fd90c0879af9232eb as initial hint for imported dep github.com/pkg/sftp
Using ^1.0.0 as initial constraint for imported dep github.com/sdboyer/deptest
Trying v1.0.0 (ff2948a) as initial lock for imported dep github.com/sdboyer/deptest
25 changes: 25 additions & 0 deletions cmd/dep/testdata/gb/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": 0,
"dependencies": [
{
"importpath": "github.com/kr/fs",
"repository": "https://github.com/kr/fs",
"revision": "2788f0dbd16903de03cb8186e5c7d97b69ad387b",
"branch": "master",
"path": ""
},
{
"importpath": "github.com/pkg/sftp",
"repository": "https://github.com/pkg/sftp",
"revision": "f234c3c6540c0358b1802f7fd90c0879af9232eb",
"branch": "master",
"path": ""
},
{
"importpath": "github.com/sdboyer/deptest",
"repository": "https://github.com/sdboyer/deptest",
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
"branch": "HEAD"
}
]
}

0 comments on commit ba11bbe

Please sign in to comment.