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

Commit bdd2f6b

Browse files
committed
import gvt manifest on init
1 parent 16990a1 commit bdd2f6b

File tree

5 files changed

+352
-0
lines changed

5 files changed

+352
-0
lines changed

cmd/dep/gvt_importer.go

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
"github.com/golang/dep/internal/gps"
16+
"github.com/pkg/errors"
17+
)
18+
19+
const gvtPath = "vendor" + string(os.PathSeparator) + "manifest"
20+
21+
type gvtImporter struct {
22+
*baseImporter
23+
gvtConfig gvtManifest
24+
}
25+
26+
func newGvtImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *gvtImporter {
27+
return &gvtImporter{baseImporter: newBaseImporter(logger, verbose, sm)}
28+
}
29+
30+
type gvtManifest struct {
31+
Deps []gvtPkg `json:"dependencies"`
32+
}
33+
34+
type gvtPkg struct {
35+
ImportPath string
36+
Repository string
37+
Revision string
38+
Branch string
39+
}
40+
41+
func (g *gvtImporter) Name() string {
42+
return "gvt"
43+
}
44+
45+
func (g *gvtImporter) HasDepMetadata(dir string) bool {
46+
y := filepath.Join(dir, gvtPath)
47+
if _, err := os.Stat(y); err != nil {
48+
return false
49+
}
50+
51+
return true
52+
}
53+
54+
func (g *gvtImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
55+
err := g.load(dir)
56+
if err != nil {
57+
return nil, nil, err
58+
}
59+
60+
return g.convert(pr)
61+
}
62+
63+
func (g *gvtImporter) load(projectDir string) error {
64+
g.logger.Println("Detected gvt configuration files...")
65+
j := filepath.Join(projectDir, gvtPath)
66+
if g.verbose {
67+
g.logger.Printf(" Loading %s", j)
68+
}
69+
jb, err := ioutil.ReadFile(j)
70+
if err != nil {
71+
return errors.Wrapf(err, "unable to read %s", j)
72+
}
73+
err = json.Unmarshal(jb, &g.gvtConfig)
74+
if err != nil {
75+
return errors.Wrapf(err, "unable to parse %s", j)
76+
}
77+
78+
return nil
79+
}
80+
81+
func (g *gvtImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
82+
g.logger.Println("Converting from vendor/manifest ...")
83+
84+
packages := make([]importedPackage, 0, len(g.gvtConfig.Deps))
85+
for _, pkg := range g.gvtConfig.Deps {
86+
// Validate
87+
if pkg.ImportPath == "" {
88+
err := errors.New("invalid gvt configuration, ImportPath is required")
89+
return nil, nil, err
90+
}
91+
92+
if pkg.Revision == "" {
93+
err := errors.New("invalid gvt configuration, Revision is required")
94+
return nil, nil, err
95+
}
96+
97+
var contstraintHint = ""
98+
if pkg.Branch != "master" {
99+
contstraintHint = pkg.Branch
100+
}
101+
102+
ip := importedPackage{
103+
Name: pkg.ImportPath,
104+
//TODO: Source: pkg.Repository,
105+
LockHint: pkg.Revision,
106+
ConstraintHint: contstraintHint,
107+
}
108+
packages = append(packages, ip)
109+
}
110+
111+
err := g.importPackages(packages, true)
112+
if err != nil {
113+
return nil, nil, err
114+
}
115+
116+
return g.manifest, g.lock, nil
117+
}

cmd/dep/gvt_importer_test.go

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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"
14+
"github.com/golang/dep/internal/gps"
15+
"github.com/golang/dep/internal/test"
16+
"github.com/pkg/errors"
17+
)
18+
19+
func TestGvtConfig_Convert(t *testing.T) {
20+
testCases := map[string]struct {
21+
convertTestCase
22+
gvtConfig gvtManifest
23+
}{
24+
"package without comment": {
25+
convertTestCase{
26+
wantConstraint: importerTestV1Constraint,
27+
wantRevision: importerTestV1Rev,
28+
wantVersion: importerTestV1Tag,
29+
},
30+
gvtManifest{
31+
Deps: []gvtPkg{
32+
{
33+
ImportPath: importerTestProject,
34+
Revision: importerTestV1Rev,
35+
},
36+
},
37+
},
38+
},
39+
"package with non-master branch": {
40+
convertTestCase{
41+
wantConstraint: importerTestV2Branch,
42+
wantRevision: importerTestV2PatchRev,
43+
wantVersion: importerTestV2PatchTag,
44+
},
45+
gvtManifest{
46+
Deps: []gvtPkg{
47+
{
48+
ImportPath: importerTestProject,
49+
Revision: importerTestV2PatchRev,
50+
Branch: importerTestV2Branch,
51+
},
52+
},
53+
},
54+
},
55+
"missing package name": {
56+
convertTestCase{
57+
wantConvertErr: true,
58+
},
59+
gvtManifest{
60+
Deps: []gvtPkg{{ImportPath: ""}},
61+
},
62+
},
63+
"missing revision": {
64+
convertTestCase{
65+
wantConvertErr: true,
66+
},
67+
gvtManifest{
68+
Deps: []gvtPkg{
69+
{
70+
ImportPath: importerTestProject,
71+
},
72+
},
73+
},
74+
},
75+
}
76+
77+
for name, testCase := range testCases {
78+
name := name
79+
testCase := testCase
80+
t.Run(name, func(t *testing.T) {
81+
t.Parallel()
82+
83+
err := testCase.Exec(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error) {
84+
g := newGvtImporter(logger, true, sm)
85+
g.gvtConfig = testCase.gvtConfig
86+
return g.convert(testProjectRoot)
87+
})
88+
if err != nil {
89+
t.Fatalf("%#v", err)
90+
}
91+
})
92+
}
93+
}
94+
95+
func TestGvtConfig_Import(t *testing.T) {
96+
h := test.NewHelper(t)
97+
defer h.Cleanup()
98+
99+
cacheDir := "gps-repocache"
100+
h.TempDir(cacheDir)
101+
h.TempDir("src")
102+
h.TempDir(filepath.Join("src", testProjectRoot))
103+
h.TempCopy(filepath.Join(testProjectRoot, gvtPath), "init/gvt/manifest")
104+
105+
projectRoot := h.Path(testProjectRoot)
106+
sm, err := gps.NewSourceManager(gps.SourceManagerConfig{
107+
Cachedir: h.Path(cacheDir),
108+
Logger: log.New(test.Writer{t}, "", 0),
109+
})
110+
h.Must(err)
111+
defer sm.Release()
112+
113+
// Capture stderr so we can verify output
114+
verboseOutput := &bytes.Buffer{}
115+
logger := log.New(verboseOutput, "", 0)
116+
117+
g := newGvtImporter(logger, false, sm) // Disable verbose so that we don't print values that change each test run
118+
if !g.HasDepMetadata(projectRoot) {
119+
t.Fatal("Expected the importer to detect gvt configuration file")
120+
}
121+
122+
m, l, err := g.Import(projectRoot, testProjectRoot)
123+
h.Must(err)
124+
125+
if m == nil {
126+
t.Fatal("Expected the manifest to be generated")
127+
}
128+
129+
if l == nil {
130+
t.Fatal("Expected the lock to be generated")
131+
}
132+
133+
goldenFile := "init/gvt/golden.txt"
134+
got := verboseOutput.String()
135+
want := h.GetTestFileString(goldenFile)
136+
if want != got {
137+
if *test.UpdateGolden {
138+
if err := h.WriteTestFile(goldenFile, got); err != nil {
139+
t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
140+
}
141+
} else {
142+
t.Fatalf("want %s, got %s", want, got)
143+
}
144+
}
145+
}
146+
147+
func TestGvtConfig_JsonLoad(t *testing.T) {
148+
// This is same as cmd/dep/testdata/init/gvt/manifest
149+
wantConfig := gvtManifest{
150+
Deps: []gvtPkg{
151+
{
152+
ImportPath: "github.com/sdboyer/deptest",
153+
Revision: "3f4c3bea144e112a69bbe5d8d01c1b09a544253f",
154+
},
155+
{
156+
ImportPath: "github.com/sdboyer/deptestdos",
157+
Revision: "5c607206be5decd28e6263ffffdcee067266015e",
158+
},
159+
{
160+
ImportPath: "github.com/carolynvs/deptest-importers",
161+
Revision: "b79bc9482da8bb7402cdc3e3fd984db250718dd7",
162+
Branch: "v2",
163+
},
164+
},
165+
}
166+
167+
h := test.NewHelper(t)
168+
defer h.Cleanup()
169+
170+
ctx := newTestContext(h)
171+
172+
h.TempCopy(filepath.Join(testProjectRoot, gvtPath), "init/gvt/manifest")
173+
174+
projectRoot := h.Path(testProjectRoot)
175+
176+
g := newGvtImporter(ctx.Err, true, nil)
177+
err := g.load(projectRoot)
178+
if err != nil {
179+
t.Fatalf("Error while loading... %v", err)
180+
}
181+
182+
if !gvtEqualImports(g.gvtConfig.Deps, wantConfig.Deps) {
183+
t.Fatalf("Expected imports to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.gvtConfig.Deps, wantConfig.Deps)
184+
}
185+
}
186+
187+
// gvtEqualImports compares two slices of gvtPkg and checks if they are
188+
// equal.
189+
func gvtEqualImports(a, b []gvtPkg) bool {
190+
if a == nil && b == nil {
191+
return true
192+
}
193+
194+
if a == nil || b == nil {
195+
return false
196+
}
197+
198+
if len(a) != len(b) {
199+
return false
200+
}
201+
202+
for i := range a {
203+
if a[i] != b[i] {
204+
return false
205+
}
206+
}
207+
208+
return true
209+
}

cmd/dep/root_analyzer.go

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup
7171
newGodepImporter(logger, a.ctx.Verbose, a.sm),
7272
newVndrImporter(logger, a.ctx.Verbose, a.sm),
7373
newGovendImporter(logger, a.ctx.Verbose, a.sm),
74+
newGvtImporter(logger, a.ctx.Verbose, a.sm),
7475
}
7576

7677
for _, i := range importers {

cmd/dep/testdata/init/gvt/golden.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Detected gvt configuration files...
2+
Converting from vendor/manifest ...
3+
Using ^0.8.1 as initial constraint 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 (5c60720) as initial lock for imported dep github.com/sdboyer/deptestdos
7+
Using v2 as initial constraint for imported dep github.com/carolynvs/deptest-importers
8+
Trying v2 (b79bc94) as initial lock for imported dep github.com/carolynvs/deptest-importers

cmd/dep/testdata/init/gvt/manifest

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

0 commit comments

Comments
 (0)