From f7cf4810c69677847ef372a7f4082ee44ac99776 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 10:41:45 +0900 Subject: [PATCH 01/10] add Gomfile importer --- cmd/dep/gom_importer.go | 264 ++++++++++++++++++ cmd/dep/gom_importer_test.go | 215 ++++++++++++++ cmd/dep/root_analyzer.go | 1 + cmd/dep/testdata/gom/Gomfile | 2 + .../testdata/gom/expected_import_output.txt | 6 + 5 files changed, 488 insertions(+) create mode 100644 cmd/dep/gom_importer.go create mode 100644 cmd/dep/gom_importer_test.go create mode 100644 cmd/dep/testdata/gom/Gomfile create mode 100644 cmd/dep/testdata/gom/expected_import_output.txt diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go new file mode 100644 index 0000000000..1df973430b --- /dev/null +++ b/cmd/dep/gom_importer.go @@ -0,0 +1,264 @@ +// 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 ( + "bufio" + "fmt" + "io" + "log" + "os" + "path/filepath" + "regexp" + "strings" + + "github.com/golang/dep" + fb "github.com/golang/dep/internal/feedback" + "github.com/golang/dep/internal/gps" + "github.com/pkg/errors" +) + +var ( + gomQx = `'[^']*'|"[^"]*"` + gomKx = `:[a-z][a-z0-9_]*` + gomAx = `(?:\s*` + gomKx + `\s*|,\s*` + gomKx + `\s*)` + gomReGroup = regexp.MustCompile(`\s*group\s+((?:` + gomKx + `\s*|,\s*` + gomKx + `\s*)*)\s*do\s*$`) + gomReEnd = regexp.MustCompile(`\s*end\s*$`) + gomReGom = regexp.MustCompile(`^\s*gom\s+(` + gomQx + `)\s*((?:,\s*` + gomKx + `\s*=>\s*(?:` + gomQx + `|\s*\[\s*` + gomAx + `*\s*\]\s*))*)$`) + gomReOptions = regexp.MustCompile(`(,\s*` + gomKx + `\s*=>\s*(?:` + gomQx + `|\s*\[\s*` + gomAx + `*\s*\]\s*)\s*)`) +) + +const gomfileName = "Gomfile" + +type gomImporter struct { + goms []gomPackage + + logger *log.Logger + verbose bool + sm gps.SourceManager +} + +func newGomImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *gomImporter { + return &gomImporter{ + logger: logger, + verbose: verbose, + sm: sm, + } +} + +type gomPackage struct { + name string + options map[string]interface{} +} + +func (p *gomPackage) hasOption(name string) bool { + _, ok := p.options[name] + return ok +} + +func (g *gomImporter) Name() string { + return "gom" +} + +func (g *gomImporter) HasDepMetadata(dir string) bool { + y := filepath.Join(dir, gomfileName) + if _, err := os.Stat(y); err != nil { + return false + } + + return true +} + +func (g *gomImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { + err := g.load(dir) + if err != nil { + return nil, nil, err + } + + return g.convert(pr) +} + +func unquote(name string) string { + name = strings.TrimSpace(name) + if len(name) > 2 { + if (name[0] == '\'' && name[len(name)-1] == '\'') || (name[0] == '"' && name[len(name)-1] == '"') { + return name[1 : len(name)-1] + } + } + return name +} + +func (g *gomImporter) has(c interface{}, key string) bool { + if m, ok := c.(map[string]interface{}); ok { + _, ok := m[key] + return ok + } else if a, ok := c.([]string); ok { + for _, s := range a { + if ok && s == key { + return true + } + } + } + return false +} + +func (g *gomImporter) parseOptions(line string, options map[string]interface{}) { + ss := gomReOptions.FindAllStringSubmatch(line, -1) + re_a := regexp.MustCompile(gomAx) + for _, s := range ss { + kvs := strings.SplitN(strings.TrimSpace(s[0])[1:], "=>", 2) + kvs[0], kvs[1] = strings.TrimSpace(kvs[0]), strings.TrimSpace(kvs[1]) + if kvs[1][0] == '[' { + as := re_a.FindAllStringSubmatch(kvs[1][1:len(kvs[1])-1], -1) + a := []string{} + for i := range as { + it := strings.TrimSpace(as[i][0]) + if strings.HasPrefix(it, ",") { + it = strings.TrimSpace(it[1:]) + } + if strings.HasPrefix(it, ":") { + it = strings.TrimSpace(it[1:]) + } + a = append(a, it) + } + options[kvs[0][1:]] = a + } else { + options[kvs[0][1:]] = unquote(kvs[1]) + } + } +} + +// load the gomfile. +func (g *gomImporter) load(projectDir string) error { + g.logger.Println("Detected Gomfile...") + filename := filepath.Join(projectDir, gomfileName) + f, err := os.Open(filename + ".lock") + if err != nil { + f, err = os.Open(filename) + if err != nil { + return err + } + } + defer f.Close() + br := bufio.NewReader(f) + + g.goms = make([]gomPackage, 0) + + n := 0 + skip := 0 + valid := true + var envs []string + for { + n++ + lb, _, err := br.ReadLine() + if err != nil { + if err == io.EOF { + return nil + } + return err + } + line := strings.TrimSpace(string(lb)) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + + name := "" + options := make(map[string]interface{}) + var items []string + if gomReGroup.MatchString(line) { + envs = strings.Split(gomReGroup.FindStringSubmatch(line)[1], ",") + for i := range envs { + envs[i] = strings.TrimSpace(envs[i])[1:] + } + valid = true + continue + } else if gomReEnd.MatchString(line) { + if !valid { + skip-- + if skip < 0 { + return fmt.Errorf("Syntax Error at line %d", n) + } + } + valid = false + envs = nil + continue + } else if skip > 0 { + continue + } else if gomReGom.MatchString(line) { + items = gomReGom.FindStringSubmatch(line)[1:] + name = unquote(items[0]) + g.parseOptions(items[1], options) + } else { + return fmt.Errorf("Syntax Error at line %d", n) + } + if envs != nil { + options["group"] = envs + } + g.goms = append(g.goms, gomPackage{name, options}) + } + return nil +} + +// convert the gomfile into dep configuration files. +func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { + g.logger.Println("Converting from Gomfile ...") + + manifest := &dep.Manifest{ + Constraints: make(gps.ProjectConstraints), + } + lock := &dep.Lock{} + + for _, pkg := range g.goms { + // Obtain ProjectRoot. Required for avoiding sub-package imports. + ip, err := g.sm.DeduceProjectRoot(pkg.name) + if err != nil { + return nil, nil, err + } + pkg.name = string(ip) + + // Check if it already existing in locked projects + if projectExistsInLock(lock, pkg.name) { + continue + } + + rev := "" + + if pkg.hasOption("branch") { + rev, _ = pkg.options["branch"].(string) + } + if pkg.hasOption("tag") { + rev, _ = pkg.options["tag"].(string) + } + if pkg.hasOption("commit") { + rev, _ = pkg.options["commit"].(string) + } + + if rev != "" { + pi := gps.ProjectIdentifier{ + ProjectRoot: gps.ProjectRoot(pkg.name), + } + revision := gps.Revision(rev) + version, err := lookupVersionForRevision(revision, pi, g.sm) + if err != nil { + warn := errors.Wrapf(err, "Unable to lookup the version represented by %s in %s. Falling back to locking the revision only.", rev, pi.ProjectRoot) + g.logger.Printf(warn.Error()) + version = revision + } + feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger) + lp := gps.NewLockedProject(pi, version, nil) + lock.P = append(lock.P, lp) + } + var pc gps.ProjectConstraint + pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.name)} + if err != nil { + return nil, nil, err + } + pc.Constraint, err = gps.NewSemverConstraintIC(rev) + + manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} + } + + return manifest, lock, nil +} diff --git a/cmd/dep/gom_importer_test.go b/cmd/dep/gom_importer_test.go new file mode 100644 index 0000000000..041d4af6fa --- /dev/null +++ b/cmd/dep/gom_importer_test.go @@ -0,0 +1,215 @@ +// 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" + "reflect" + "testing" + + "github.com/golang/dep" + "github.com/golang/dep/internal/gps" + "github.com/golang/dep/internal/test" + "github.com/pkg/errors" +) + +const testGomProjectRoot = "github.com/golang/notexist" + +func TestGomConfig_Import(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + cacheDir := "gps-repocache" + h.TempDir(cacheDir) + h.TempDir("src") + h.TempDir(filepath.Join("src", testGomProjectRoot)) + h.TempCopy(filepath.Join(testGomProjectRoot, gomfileName), "gom/Gomfile") + + projectRoot := h.Path(testGomProjectRoot) + sm, err := gps.NewSourceManager(h.Path(cacheDir)) + h.Must(err) + defer sm.Release() + + // Capture stderr so we can verify output + verboseOutput := &bytes.Buffer{} + logger := log.New(verboseOutput, "", 0) + + g := newGomImporter(logger, 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 Gomfile") + } + + m, l, err := g.Import(projectRoot, testGomProjectRoot) + 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 := "gom/expected_import_output.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 TestGomConfig_ConvertProject(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + ctx := newTestContext(h) + sm, err := ctx.SourceManager() + h.Must(err) + defer sm.Release() + + g := newGomImporter(discardLogger, true, sm) + g.goms = []gomPackage{ + { + name: "github.com/sdboyer/deptest", + options: map[string]interface{}{ + "commit": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf", + }, + }, + } + + manifest, lock, err := g.convert("") + if err != nil { + t.Fatal(err) + } + + _, ok := manifest.Constraints["github.com/sdboyer/deptest"] + if !ok { + t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none") + } + + p := lock.P[0] + if p.Ident().ProjectRoot != "github.com/sdboyer/deptest" { + t.Fatalf("Expected the lock to have a project for 'github.com/sdboyer/deptest' but got '%s'", p.Ident().ProjectRoot) + } + + lv := p.Version() + lpv, ok := lv.(gps.PairedVersion) + if !ok { + t.Fatalf("Expected locked version to be PairedVersion but got %T", lv) + } + + rev := lpv.Underlying() + if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" { + t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev) + } +} + +func TestGomConfig_ConvertProject_EmptyComment(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + h.TempDir("src") + + ctx := newTestContext(h) + sm, err := ctx.SourceManager() + h.Must(err) + defer sm.Release() + + g := newGomImporter(discardLogger, true, sm) + g.goms = []gomPackage{ + { + name: "github.com/sdboyer/deptest", + options: map[string]interface{}{ + "commit": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf", + }, + }, + } + + manifest, lock, err := g.convert("") + if err != nil { + t.Fatal(err) + } + + _, ok := manifest.Constraints["github.com/sdboyer/deptest"] + if !ok { + t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none") + } + + p := lock.P[0] + if p.Ident().ProjectRoot != "github.com/sdboyer/deptest" { + t.Fatalf("Expected the lock to have a project for 'github.com/sdboyer/deptest' but got '%s'", p.Ident().ProjectRoot) + } + + lv := p.Version() + lpv, ok := lv.(gps.PairedVersion) + if !ok { + t.Fatalf("Expected locked version to be PairedVersion but got %T", lv) + } + + rev := lpv.Underlying() + if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" { + t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev) + } +} + +func TestGomConfig_ProjectExistsInLock(t *testing.T) { + lock := &dep.Lock{} + pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")} + ver := gps.NewVersion("v1.0.0") + lock.P = append(lock.P, gps.NewLockedProject(pi, ver, nil)) + + cases := []struct { + importPath string + want bool + }{ + { + importPath: "github.com/sdboyer/deptest", + want: true, + }, + { + importPath: "github.com/golang/notexist", + want: false, + }, + } + + for _, c := range cases { + result := projectExistsInLock(lock, c.importPath) + + if result != c.want { + t.Fatalf("projectExistsInLock result is not as expected: \n\t(GOT) %v \n\t(WNT) %v", result, c.want) + } + } +} + +// Compares two slices of gomPackage and checks if they are equal. +func equalGomImports(a, b []gomPackage) bool { + + if a == nil && b == nil { + return true + } + + if a == nil || b == nil { + return false + } + + if len(a) != len(b) { + return false + } + + for i := range a { + if !reflect.DeepEqual(a[i], b[i]) { + return false + } + } + + return true +} diff --git a/cmd/dep/root_analyzer.go b/cmd/dep/root_analyzer.go index 9f93b7eb22..0df3602264 100644 --- a/cmd/dep/root_analyzer.go +++ b/cmd/dep/root_analyzer.go @@ -73,6 +73,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup importers := []importer{ newGlideImporter(logger, a.ctx.Verbose, a.sm), newGodepImporter(logger, a.ctx.Verbose, a.sm), + newGomImporter(logger, a.ctx.Verbose, a.sm), } for _, i := range importers { diff --git a/cmd/dep/testdata/gom/Gomfile b/cmd/dep/testdata/gom/Gomfile new file mode 100644 index 0000000000..209e72ce8e --- /dev/null +++ b/cmd/dep/testdata/gom/Gomfile @@ -0,0 +1,2 @@ +gom "github.com/sdboyer/deptest", :commit => "3f4c3bea144e112a69bbe5d8d01c1b09a544253f" +gom "github.com/sdboyer/deptestdos", :commit => "5c607206be5decd28e6263ffffdcee067266015e" diff --git a/cmd/dep/testdata/gom/expected_import_output.txt b/cmd/dep/testdata/gom/expected_import_output.txt new file mode 100644 index 0000000000..6e5e684423 --- /dev/null +++ b/cmd/dep/testdata/gom/expected_import_output.txt @@ -0,0 +1,6 @@ +Detected Gomfile... +Converting from Gomfile ... + Using ^0.8.1 as initial constraint for imported dep github.com/sdboyer/deptest + Trying v0.8.1 (3f4c3be) as initial lock for imported dep github.com/sdboyer/deptest + Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos + Trying v2.0.0 (5c60720) as initial lock for imported dep github.com/sdboyer/deptestdos From a552c03ae0557ef95657cdde7dc00bdd0984eafa Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 11:00:24 +0900 Subject: [PATCH 02/10] fix check err --- cmd/dep/gom_importer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index 1df973430b..da9c9addb0 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -252,10 +252,10 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err } var pc gps.ProjectConstraint pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.name)} + pc.Constraint, err = gps.NewSemverConstraintIC(rev) if err != nil { return nil, nil, err } - pc.Constraint, err = gps.NewSemverConstraintIC(rev) manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} } From e6045bbe69e2ad2c4abf1175f2cf4888b69423ef Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 11:01:15 +0900 Subject: [PATCH 03/10] unreachable code --- cmd/dep/gom_importer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index da9c9addb0..18217e6fe1 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -198,7 +198,6 @@ func (g *gomImporter) load(projectDir string) error { } g.goms = append(g.goms, gomPackage{name, options}) } - return nil } // convert the gomfile into dep configuration files. From 23ee21a45e8881a5a3cbb86195d47e59028dcde9 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 11:06:41 +0900 Subject: [PATCH 04/10] fix underscores --- cmd/dep/gom_importer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index 18217e6fe1..9174232284 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -106,12 +106,12 @@ func (g *gomImporter) has(c interface{}, key string) bool { func (g *gomImporter) parseOptions(line string, options map[string]interface{}) { ss := gomReOptions.FindAllStringSubmatch(line, -1) - re_a := regexp.MustCompile(gomAx) + re := regexp.MustCompile(gomAx) for _, s := range ss { kvs := strings.SplitN(strings.TrimSpace(s[0])[1:], "=>", 2) kvs[0], kvs[1] = strings.TrimSpace(kvs[0]), strings.TrimSpace(kvs[1]) if kvs[1][0] == '[' { - as := re_a.FindAllStringSubmatch(kvs[1][1:len(kvs[1])-1], -1) + as := re.FindAllStringSubmatch(kvs[1][1:len(kvs[1])-1], -1) a := []string{} for i := range as { it := strings.TrimSpace(as[i][0]) From ae02dbb9db053b996de8360852499a2755504264 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 11:18:39 +0900 Subject: [PATCH 05/10] fix constraint --- cmd/dep/gom_importer.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index 9174232284..abac41b4b5 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -234,6 +234,9 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err rev, _ = pkg.options["commit"].(string) } + var pc gps.ProjectConstraint + pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.name)} + if rev != "" { pi := gps.ProjectIdentifier{ ProjectRoot: gps.ProjectRoot(pkg.name), @@ -248,12 +251,10 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger) lp := gps.NewLockedProject(pi, version, nil) lock.P = append(lock.P, lp) - } - var pc gps.ProjectConstraint - pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.name)} - pc.Constraint, err = gps.NewSemverConstraintIC(rev) - if err != nil { - return nil, nil, err + pc.Constraint, err = gps.NewSemverConstraintIC(rev) + if err != nil { + return nil, nil, err + } } manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} From c807efe7862ba6f89aad20a0605b72593ddfdded Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 13 Jun 2017 14:00:17 +0900 Subject: [PATCH 06/10] gom doesn't support constant --- cmd/dep/gom_importer.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index abac41b4b5..c02c27e26b 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -251,13 +251,8 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger) lp := gps.NewLockedProject(pi, version, nil) lock.P = append(lock.P, lp) - pc.Constraint, err = gps.NewSemverConstraintIC(rev) - if err != nil { - return nil, nil, err - } } - - manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} + manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: nil} } return manifest, lock, nil From cb2d63db367b587bf415c86cab5181e4bbd665a5 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 14 Jun 2017 12:56:02 +0900 Subject: [PATCH 07/10] enable constraint --- cmd/dep/gom_importer.go | 7 ++++++- cmd/dep/gom_importer_test.go | 26 +++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index c02c27e26b..45c671f147 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -251,8 +251,13 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger) lp := gps.NewLockedProject(pi, version, nil) lock.P = append(lock.P, lp) + + pc.Constraint, err = deduceConstraint(rev, pc.Ident, g.sm) + if err != nil { + return nil, nil, err + } } - manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: nil} + manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} } return manifest, lock, nil diff --git a/cmd/dep/gom_importer_test.go b/cmd/dep/gom_importer_test.go index 041d4af6fa..87709e81a7 100644 --- a/cmd/dep/gom_importer_test.go +++ b/cmd/dep/gom_importer_test.go @@ -92,11 +92,16 @@ func TestGomConfig_ConvertProject(t *testing.T) { t.Fatal(err) } - _, ok := manifest.Constraints["github.com/sdboyer/deptest"] + d, ok := manifest.Constraints["github.com/sdboyer/deptest"] if !ok { t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none") } + v := d.Constraint.String() + if v != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" { + t.Fatalf("Expected manifest constraint to be %q got %q", "ff2948a2ac8f538c4ecd55962e919d1e13e74baf", v) + } + p := lock.P[0] if p.Ident().ProjectRoot != "github.com/sdboyer/deptest" { t.Fatalf("Expected the lock to have a project for 'github.com/sdboyer/deptest' but got '%s'", p.Ident().ProjectRoot) @@ -129,7 +134,7 @@ func TestGomConfig_ConvertProject_EmptyComment(t *testing.T) { { name: "github.com/sdboyer/deptest", options: map[string]interface{}{ - "commit": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf", + "branch": "0.8.0", }, }, } @@ -139,24 +144,23 @@ func TestGomConfig_ConvertProject_EmptyComment(t *testing.T) { t.Fatal(err) } - _, ok := manifest.Constraints["github.com/sdboyer/deptest"] + d, ok := manifest.Constraints["github.com/sdboyer/deptest"] if !ok { t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none") } + v := d.Constraint.String() + if v != "^0.8.0" { + t.Fatalf("Expected manifest constraint to be %q got %q", "^0.8.0", v) + } + p := lock.P[0] if p.Ident().ProjectRoot != "github.com/sdboyer/deptest" { t.Fatalf("Expected the lock to have a project for 'github.com/sdboyer/deptest' but got '%s'", p.Ident().ProjectRoot) } - lv := p.Version() - lpv, ok := lv.(gps.PairedVersion) - if !ok { - t.Fatalf("Expected locked version to be PairedVersion but got %T", lv) - } - - rev := lpv.Underlying() - if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" { + rev := p.Version().String() + if rev != "0.8.0" { t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev) } } From 66a8d6ee4718220b79244717f2f80c506bf18f53 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 22 Jul 2017 15:21:40 +0900 Subject: [PATCH 08/10] rebase master --- cmd/dep/gom_importer.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index 45c671f147..beab82eaa8 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -248,14 +248,17 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err g.logger.Printf(warn.Error()) version = revision } - feedback(version, pi.ProjectRoot, fb.DepTypeImported, g.logger) - lp := gps.NewLockedProject(pi, version, nil) - lock.P = append(lock.P, lp) pc.Constraint, err = deduceConstraint(rev, pc.Ident, g.sm) if err != nil { return nil, nil, err } + + lp := gps.NewLockedProject(pi, version, nil) + + f := fb.NewLockedProjectFeedback(lp, fb.DepTypeImported) + f.LogFeedback(g.logger) + lock.P = append(lock.P, lp) } manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} } From 92dca1a078254659a7e87f0e2175785042fad5ab Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 22 Jul 2017 15:23:05 +0900 Subject: [PATCH 09/10] remove test and staff --- cmd/dep/gom_importer_test.go | 55 ------------------------------------ 1 file changed, 55 deletions(-) diff --git a/cmd/dep/gom_importer_test.go b/cmd/dep/gom_importer_test.go index 87709e81a7..bea8136eb7 100644 --- a/cmd/dep/gom_importer_test.go +++ b/cmd/dep/gom_importer_test.go @@ -8,10 +8,8 @@ import ( "bytes" "log" "path/filepath" - "reflect" "testing" - "github.com/golang/dep" "github.com/golang/dep/internal/gps" "github.com/golang/dep/internal/test" "github.com/pkg/errors" @@ -164,56 +162,3 @@ func TestGomConfig_ConvertProject_EmptyComment(t *testing.T) { t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev) } } - -func TestGomConfig_ProjectExistsInLock(t *testing.T) { - lock := &dep.Lock{} - pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")} - ver := gps.NewVersion("v1.0.0") - lock.P = append(lock.P, gps.NewLockedProject(pi, ver, nil)) - - cases := []struct { - importPath string - want bool - }{ - { - importPath: "github.com/sdboyer/deptest", - want: true, - }, - { - importPath: "github.com/golang/notexist", - want: false, - }, - } - - for _, c := range cases { - result := projectExistsInLock(lock, c.importPath) - - if result != c.want { - t.Fatalf("projectExistsInLock result is not as expected: \n\t(GOT) %v \n\t(WNT) %v", result, c.want) - } - } -} - -// Compares two slices of gomPackage and checks if they are equal. -func equalGomImports(a, b []gomPackage) bool { - - if a == nil && b == nil { - return true - } - - if a == nil || b == nil { - return false - } - - if len(a) != len(b) { - return false - } - - for i := range a { - if !reflect.DeepEqual(a[i], b[i]) { - return false - } - } - - return true -} From 06d20554285ca55fa8984c0967c5a3ff1de8187b Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 22 Jul 2017 22:00:21 +0900 Subject: [PATCH 10/10] fix build --- cmd/dep/gom_importer.go | 19 ++++++++++--------- cmd/dep/gom_importer_test.go | 2 +- .../testdata/gom/expected_import_output.txt | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cmd/dep/gom_importer.go b/cmd/dep/gom_importer.go index beab82eaa8..a088220171 100644 --- a/cmd/dep/gom_importer.go +++ b/cmd/dep/gom_importer.go @@ -235,25 +235,26 @@ func (g *gomImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, err } var pc gps.ProjectConstraint - pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.name)} + pi := gps.ProjectIdentifier{ + ProjectRoot: gps.ProjectRoot(pkg.name), + } + + pc.Ident = pi if rev != "" { - pi := gps.ProjectIdentifier{ - ProjectRoot: gps.ProjectRoot(pkg.name), + pc.Constraint, err = g.sm.InferConstraint(rev, pc.Ident) + if err != nil { + return nil, nil, err } + revision := gps.Revision(rev) - version, err := lookupVersionForRevision(revision, pi, g.sm) + version, err := lookupVersionForLockedProject(pi, pc.Constraint, revision, g.sm) if err != nil { warn := errors.Wrapf(err, "Unable to lookup the version represented by %s in %s. Falling back to locking the revision only.", rev, pi.ProjectRoot) g.logger.Printf(warn.Error()) version = revision } - pc.Constraint, err = deduceConstraint(rev, pc.Ident, g.sm) - if err != nil { - return nil, nil, err - } - lp := gps.NewLockedProject(pi, version, nil) f := fb.NewLockedProjectFeedback(lp, fb.DepTypeImported) diff --git a/cmd/dep/gom_importer_test.go b/cmd/dep/gom_importer_test.go index bea8136eb7..7f5e3c5a6e 100644 --- a/cmd/dep/gom_importer_test.go +++ b/cmd/dep/gom_importer_test.go @@ -111,7 +111,7 @@ func TestGomConfig_ConvertProject(t *testing.T) { t.Fatalf("Expected locked version to be PairedVersion but got %T", lv) } - rev := lpv.Underlying() + rev := lpv.Revision().String() if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" { t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev) } diff --git a/cmd/dep/testdata/gom/expected_import_output.txt b/cmd/dep/testdata/gom/expected_import_output.txt index 6e5e684423..e14cd03bd6 100644 --- a/cmd/dep/testdata/gom/expected_import_output.txt +++ b/cmd/dep/testdata/gom/expected_import_output.txt @@ -1,6 +1,4 @@ Detected Gomfile... Converting from Gomfile ... - Using ^0.8.1 as initial constraint for imported dep github.com/sdboyer/deptest Trying v0.8.1 (3f4c3be) as initial lock for imported dep github.com/sdboyer/deptest - Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos Trying v2.0.0 (5c60720) as initial lock for imported dep github.com/sdboyer/deptestdos