This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at a gb manifest importer
- Loading branch information
Showing
4 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |