diff --git a/Gopkg.lock b/Gopkg.lock index 3541734bfa..194a42832f 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,4 +1,4 @@ -memo = "" +memo = "6f3369fa60f8d2ee63f8bd5e5f8feb8d5801977940cc65b5c14a3dc411f8a9e1" [[projects]] branch = "2.x" diff --git a/cmd/dep/ensure.go b/cmd/dep/ensure.go index 00ea17aab1..b9f03bad79 100644 --- a/cmd/dep/ensure.go +++ b/cmd/dep/ensure.go @@ -6,14 +6,12 @@ package main import ( "bytes" - "encoding/hex" "flag" "fmt" "go/build" "log" "os" "path/filepath" - "strconv" "strings" "github.com/golang/dep" @@ -275,7 +273,7 @@ func getProjectConstraint(arg string, sm *gps.SourceMgr) (gps.ProjectConstraint, parts := strings.SplitN(arg, "@", 2) arg = parts[0] versionStr = parts[1] - constraint.Constraint = deduceConstraint(parts[1]) + constraint.Constraint = gps.DeduceConstraint(parts[1]) } // TODO: What if there is no @, assume default branch (which may not be master) ? // TODO: if we decide to keep equals..... @@ -327,48 +325,6 @@ func getProjectConstraint(arg string, sm *gps.SourceMgr) (gps.ProjectConstraint, return constraint, nil } -// deduceConstraint tries to puzzle out what kind of version is given in a string - -// semver, a revision, or as a fallback, a plain tag -func deduceConstraint(s string) gps.Constraint { - // always semver if we can - c, err := gps.NewSemverConstraint(s) - if err == nil { - return c - } - - slen := len(s) - if slen == 40 { - if _, err = hex.DecodeString(s); err == nil { - // Whether or not it's intended to be a SHA1 digest, this is a - // valid byte sequence for that, so go with Revision. This - // covers git and hg - return gps.Revision(s) - } - } - // Next, try for bzr, which has a three-component GUID separated by - // dashes. There should be two, but the email part could contain - // internal dashes - if strings.Count(s, "-") >= 2 { - // Work from the back to avoid potential confusion from the email - i3 := strings.LastIndex(s, "-") - // Skip if - is last char, otherwise this would panic on bounds err - if slen == i3+1 { - return gps.NewVersion(s) - } - - i2 := strings.LastIndex(s[:i3], "-") - if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil { - // Getting this far means it'd pretty much be nuts if it's not a - // bzr rev, so don't bother parsing the email. - return gps.Revision(s) - } - } - - // If not a plain SHA1 or bzr custom GUID, assume a plain version. - // TODO: if there is amgibuity here, then prompt the user? - return gps.NewVersion(s) -} - func checkErrors(m map[string]pkgtree.PackageOrErr) error { noGoErrors, pkgErrors := 0, 0 for _, poe := range m { diff --git a/cmd/dep/ensure_test.go b/cmd/dep/ensure_test.go deleted file mode 100644 index ae7b611461..0000000000 --- a/cmd/dep/ensure_test.go +++ /dev/null @@ -1,38 +0,0 @@ -// 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 ( - "testing" - - "github.com/golang/dep/gps" -) - -func TestDeduceConstraint(t *testing.T) { - sv, err := gps.NewSemverConstraint("v1.2.3") - if err != nil { - t.Fatal(err) - } - - constraints := map[string]gps.Constraint{ - "v1.2.3": sv, - "5b3352dc16517996fb951394bcbbe913a2a616e3": gps.Revision("5b3352dc16517996fb951394bcbbe913a2a616e3"), - - // valid bzr revs - "jess@linux.com-20161116211307-wiuilyamo9ian0m7": gps.Revision("jess@linux.com-20161116211307-wiuilyamo9ian0m7"), - - // invalid bzr revs - "go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn": gps.NewVersion("go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn"), - "go4@golang.org-sadfasdf-": gps.NewVersion("go4@golang.org-sadfasdf-"), - "20120425195858-psty8c35ve2oej8t": gps.NewVersion("20120425195858-psty8c35ve2oej8t"), - } - - for str, expected := range constraints { - c := deduceConstraint(str) - if c != expected { - t.Fatalf("expected: %#v, got %#v for %s", expected, c, str) - } - } -} diff --git a/cmd/dep/init.go b/cmd/dep/init.go index f3526a9564..95194689df 100644 --- a/cmd/dep/init.go +++ b/cmd/dep/init.go @@ -16,6 +16,7 @@ import ( "github.com/golang/dep/gps" "github.com/golang/dep/gps/pkgtree" "github.com/golang/dep/internal" + "github.com/golang/dep/internal/importer" "github.com/pkg/errors" ) @@ -33,6 +34,10 @@ versions available from the upstream source per the following algorithm: - Default branch(es) (sorted lexicographically) - Non-semver tags (sorted lexicographically) +If configuration files for other dependency management tools are found, they +are used to pre-populate the manifest and lock. Specify -skip-tools to disable +this behavior. + A Gopkg.toml file will be written with inferred version constraints for all direct dependencies. Gopkg.lock will be written with precise versions, and vendor/ will be populated with the precise versions written to Gopkg.lock. @@ -46,10 +51,12 @@ func (cmd *initCommand) Hidden() bool { return false } func (cmd *initCommand) Register(fs *flag.FlagSet) { fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml") + fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency manager tools") } type initCommand struct { noExamples bool + skipTools bool } func trimPathPrefix(p1, p2 string) string { @@ -116,10 +123,26 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { if err != nil { return err } + m := &dep.Manifest{ Dependencies: pd.constraints, } + if !cmd.skipTools { + ipd, err := importer.Import(root, cpr) + if err != nil { + return errors.Wrap(err, "Error importing dependency management configuration") + } + + if ipd != nil { + m.Ignored = ipd.Ignored + for pr, c := range ipd.Dependencies { + internal.Vlogf("Importing dependency on %s: %s", pr, c) + m.Dependencies[pr] = c + } + } + } + // Make an initial lock from what knowledge we've collected about the // versions on disk l := &dep.Lock{ diff --git a/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.lock b/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.lock new file mode 100644 index 0000000000..5bcce13e52 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.lock @@ -0,0 +1,20 @@ +memo = "" + +[[projects]] + branch = "master" + name = "github.com/golang/lint" + packages = ["."] + revision = "cb00e5669539f047b2f4c53a421a01b0c8e172c6" + +[[projects]] + branch = "master" + name = "github.com/sdboyer/deptest" + packages = ["."] + revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" + version = "v1.0.0" + +[[projects]] + name = "github.com/sdboyer/deptestdos" + packages = ["."] + revision = "5c607206be5decd28e6263ffffdcee067266015e" + version = "v2.0.0" diff --git a/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.toml new file mode 100644 index 0000000000..d67848bc4a --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.toml @@ -0,0 +1,14 @@ +ignored = ["github.com/sdboyer/dep-test","github.com/golang/notexist/samples"] + +[[dependencies]] + branch = "master" + name = "github.com/golang/lint" + +[[dependencies]] + branch = "master" + name = "github.com/sdboyer/deptest" + source = "https://github.com/carolynvs/deptest.git" + +[[dependencies]] + name = "github.com/sdboyer/deptestdos" + version = "v2.0.0" diff --git a/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.lock b/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.lock new file mode 100644 index 0000000000..8a114cb544 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.lock @@ -0,0 +1,14 @@ +hash: 16053c82a71f9bd509b05a4523df6bc418aed2083e4b8bd97a870bbc003256f8 +updated: 2017-03-07T17:02:32.214383898-06:00 +imports: +- name: github.com/sdboyer/deptest + repo: https://github.com/carolynvs/deptest.git + vcs: git + version: ff2948a2ac8f538c4ecd55962e919d1e13e74baf +- name: github.com/sdboyer/deptestdos + version: 5c607206be5decd28e6263ffffdcee067266015e +- name: github.com/sdboyer/deptest + version: 4a1e882c79dcf4ec00d2e29fac74b9c8938d5052 +testImports: +- name: github.com/golang/lint + version: cb00e5669539f047b2f4c53a421a01b0c8e172c6 \ No newline at end of file diff --git a/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.yaml b/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.yaml new file mode 100644 index 0000000000..60ac8f5a2d --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/initial/glide.yaml @@ -0,0 +1,20 @@ +package: github.com/golang/notexist +homepage: http://example.com +license: MIT +owners: +- name: Sam Boyer + email: sdboyer@example.com + homepage: http://sdboyer.io +ignore: +- github.com/sdboyer/dep-test +excludeDirs: +- samples +import: +- package: github.com/sdboyer/deptest + repo: https://github.com/carolynvs/deptest.git + vcs: git + version: master +- package: github.com/sdboyer/deptestdos + version: v2.0.0 +testImport: +- package: github.com/golang/lint \ No newline at end of file diff --git a/cmd/dep/testdata/harness_tests/init/glide1/initial/main.go b/cmd/dep/testdata/harness_tests/init/glide1/initial/main.go new file mode 100644 index 0000000000..3389cd06b4 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/initial/main.go @@ -0,0 +1,17 @@ +// 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 ( + "fmt" + + _ "github.com/sdboyer/dep-test" + "github.com/sdboyer/deptestdos" +) + +func main() { + var x deptestdos.Bar + fmt.Println(x) +} diff --git a/cmd/dep/testdata/harness_tests/init/glide1/initial/samples/samples.go b/cmd/dep/testdata/harness_tests/init/glide1/initial/samples/samples.go new file mode 100644 index 0000000000..3e160f22fe --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/initial/samples/samples.go @@ -0,0 +1,12 @@ +// Copyright 2016 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 samples + +import dt "github.com/carolynvs/go-dep-test" + +func Sample1() int { + var x = dt.Thing + return x +} diff --git a/cmd/dep/testdata/harness_tests/init/glide1/testcase.json b/cmd/dep/testdata/harness_tests/init/glide1/testcase.json new file mode 100644 index 0000000000..4a30d9cfd3 --- /dev/null +++ b/cmd/dep/testdata/harness_tests/init/glide1/testcase.json @@ -0,0 +1,13 @@ +{ + "commands": [ + ["init", "-no-examples"] + ], + "gopath-initial": { + "github.com/golang/lint": "cb00e5669539f047b2f4c53a421a01b0c8e172c6", + "github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f" + }, + "vendor-final": [ + "github.com/sdboyer/deptest", + "github.com/sdboyer/deptestdos" + ] +} diff --git a/gps/constraint_test.go b/gps/constraint_test.go index ab99063919..583b4bd79e 100644 --- a/gps/constraint_test.go +++ b/gps/constraint_test.go @@ -905,3 +905,30 @@ func TestTypedConstraintString(t *testing.T) { } } } + +func TestDeduceConstraint(t *testing.T) { + sv, err := NewSemverConstraint("v1.2.3") + if err != nil { + t.Fatal(err) + } + + constraints := map[string]Constraint{ + "v1.2.3": sv, + "5b3352dc16517996fb951394bcbbe913a2a616e3": Revision("5b3352dc16517996fb951394bcbbe913a2a616e3"), + + // valid bzr revs + "jess@linux.com-20161116211307-wiuilyamo9ian0m7": Revision("jess@linux.com-20161116211307-wiuilyamo9ian0m7"), + + // invalid bzr revs + "go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn": NewVersion("go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn"), + "go4@golang.org-sadfasdf-": NewVersion("go4@golang.org-sadfasdf-"), + "20120425195858-psty8c35ve2oej8t": NewVersion("20120425195858-psty8c35ve2oej8t"), + } + + for str, expected := range constraints { + c := DeduceConstraint(str) + if c != expected { + t.Fatalf("expected: %#v, got %#v for %s", expected, c, str) + } + } +} diff --git a/gps/constraints.go b/gps/constraints.go index cb9d4f5ae1..261a4db619 100644 --- a/gps/constraints.go +++ b/gps/constraints.go @@ -8,6 +8,10 @@ import ( "fmt" "sort" + "encoding/hex" + "strconv" + "strings" + "github.com/Masterminds/semver" ) @@ -361,3 +365,45 @@ type sortedWC []workingConstraint func (s sortedWC) Len() int { return len(s) } func (s sortedWC) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s sortedWC) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) } + +// DeduceConstraint tries to puzzle out what kind of version is given in a string - +// semver, a revision, or as a fallback, a plain tag +func DeduceConstraint(s string) Constraint { + // always semver if we can + c, err := NewSemverConstraint(s) + if err == nil { + return c + } + + slen := len(s) + if slen == 40 { + if _, err = hex.DecodeString(s); err == nil { + // Whether or not it's intended to be a SHA1 digest, this is a + // valid byte sequence for that, so go with Revision. This + // covers git and hg + return Revision(s) + } + } + // Next, try for bzr, which has a three-component GUID separated by + // dashes. There should be two, but the email part could contain + // internal dashes + if strings.Count(s, "-") >= 2 { + // Work from the back to avoid potential confusion from the email + i3 := strings.LastIndex(s, "-") + // Skip if - is last char, otherwise this would panic on bounds err + if slen == i3+1 { + return NewVersion(s) + } + + i2 := strings.LastIndex(s[:i3], "-") + if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil { + // Getting this far means it'd pretty much be nuts if it's not a + // bzr rev, so don't bother parsing the email. + return Revision(s) + } + } + + // If not a plain SHA1 or bzr custom GUID, assume a plain version. + // TODO: if there is amgibuity here, then prompt the user? + return NewVersion(s) +} diff --git a/internal/importer/glide.go b/internal/importer/glide.go new file mode 100644 index 0000000000..ba68ad696a --- /dev/null +++ b/internal/importer/glide.go @@ -0,0 +1,142 @@ +// Copyright 2016 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 importer + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + + "github.com/go-yaml/yaml" + "github.com/golang/dep/gps" + "github.com/golang/dep/internal" + "github.com/pkg/errors" +) + +const glideConfigName = "glide.yaml" +const glideLockName = "glide.lock" + +type glideImporter struct { + rootDir string + projectRoot string +} + +func newGlideImporter(rootDir, projectRoot string) glideImporter { + return glideImporter{ + rootDir: rootDir, + projectRoot: projectRoot, + } +} + +func (i glideImporter) Name() string { + return "Glide" +} + +func (i glideImporter) getConfigPaths() (config, lock string) { + return filepath.Join(i.rootDir, glideConfigName), filepath.Join(i.rootDir, glideLockName) +} + +func (i glideImporter) HasConfig() (bool, error) { + c, l := i.getConfigPaths() + if _, err := os.Stat(c); err != nil { + if os.IsNotExist(err) { + return false, nil + } + + return false, errors.Wrapf(err, "Unable to check if Glide configuration file exists at %s", c) + } + if _, err := os.Stat(l); err != nil { + if os.IsNotExist(err) { + return false, nil + } + + return false, errors.Wrapf(err, "Unable to check if Glide lock file exists at %s", l) + } + return true, nil +} + +func (i glideImporter) LoadConfig() (*ImportedProjectData, error) { + c, l := i.getConfigPaths() + + internal.Vlogf("Loading %s", c) + cb, err := ioutil.ReadFile(c) + if err != nil { + return nil, errors.Wrapf(err, "Unable to read %s", c) + } + config := &glideConfig{} + err = yaml.Unmarshal(cb, config) + if err != nil { + return nil, errors.Wrapf(err, "Unable to parse %s", c) + } + + internal.Vlogf("Loading %s", l) + lb, err := ioutil.ReadFile(c) + if err != nil { + return nil, errors.Wrapf(err, "Unable to read %s", l) + } + lock := &glideLock{} + err = yaml.Unmarshal(lb, lock) + if err != nil { + return nil, errors.Wrapf(err, "Unable to parse %s", l) + } + + return i.convert(i.projectRoot, config, lock), nil +} + +type glideConfig struct { + Name string `yaml:"package"` + Ignores []string `yaml:"ignore"` + ExcludeDirs []string `yaml:"excludeDirs"` + Imports []glidePackage `yaml:"import"` + TestImports []glidePackage `yaml:"testImport"` +} + +type glideLock struct { + Imports []glidePackage `yaml:"import"` + TestImports []glidePackage `yaml:"testImport"` +} + +type glidePackage struct { + Name string `yaml:"package"` + Reference string `yaml:"version"` + Repository string `yaml:"repo"` +} + +func (i glideImporter) convert(projectRoot string, config *glideConfig, lock *glideLock) *ImportedProjectData { + pd := &ImportedProjectData{ + Dependencies: make(gps.ProjectConstraints), + } + + for _, pkg := range config.Imports { + pd.Dependencies[gps.ProjectRoot(pkg.Name)] = gps.ProjectProperties{ + Source: pkg.Repository, + Constraint: gps.DeduceConstraint(pkg.Reference), + } + } + + for _, pkg := range config.TestImports { + pd.Dependencies[gps.ProjectRoot(pkg.Name)] = gps.ProjectProperties{ + Source: pkg.Repository, + Constraint: gps.DeduceConstraint(pkg.Reference), + } + } + + pd.Ignored = append(pd.Ignored, config.Ignores...) + + if len(config.ExcludeDirs) > 0 { + if config.Name != "" && config.Name != projectRoot { + fmt.Fprintf(os.Stderr, "dep: Glide thinks the package is '%s' but dep thinks it is '%s', using dep's value.\n", config.Name, projectRoot) + } + + for _, dir := range config.ExcludeDirs { + pkg := path.Join(projectRoot, dir) + pd.Ignored = append(pd.Ignored, pkg) + } + } + + return pd +} diff --git a/internal/importer/glide_test.go b/internal/importer/glide_test.go new file mode 100644 index 0000000000..47d72b05b8 --- /dev/null +++ b/internal/importer/glide_test.go @@ -0,0 +1,176 @@ +// Copyright 2016 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 importer + +import ( + "path" + "path/filepath" + "testing" + + "github.com/golang/dep/test" +) + +const projectDir = "glidetest" +const projectName = "github.com/golang/notexist" + +func TestImportGlideProject(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + h.TempDir(filepath.Join("src", projectDir)) + h.TempCopy(filepath.Join(projectDir, glideConfigName), "glide.yaml") + h.TempCopy(filepath.Join(projectDir, glideLockName), "glide.lock") + + i := newGlideImporter(h.Path(projectDir), projectName) + + exists, err := i.HasConfig() + h.Must(err) + if !exists { + t.Fatal("Expected the importer to detect the glide configuration file") + } + + pd, err := i.LoadConfig() + h.Must(err) + + c, ok := pd.Dependencies["github.com/sdboyer/deptest"] + if !ok { + t.Fatal("Expected glide constraints for 'github.com/sdboyer/deptest' but got none") + } + + v := c.Constraint.String() + if v != "master" { + t.Fatalf("Expected constraint to be master, got %s", v) + } + + if c.Source != "https://github.com/carolynvs/deptest.git" { + t.Fatalf("Expected source to be https://github.com/carolynvs/deptest.git, got %s", c.Source) + } +} + +func TestImportGlideTestProject(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + h.TempDir(filepath.Join("src", projectDir)) + h.TempCopy(filepath.Join(projectDir, glideConfigName), "glide.yaml") + h.TempCopy(filepath.Join(projectDir, glideLockName), "glide.lock") + + i := newGlideImporter(h.Path(projectDir), projectName) + + exists, err := i.HasConfig() + h.Must(err) + if !exists { + t.Fatal("Expected the importer to detect the glide configuration file") + } + + pd, err := i.LoadConfig() + h.Must(err) + + _, ok := pd.Dependencies["github.com/golang/lint"] + if !ok { + t.Fatal("Expected glide constraints for 'github.com/golang/lint' but got none") + } +} + +func TestImportGlideIgnore(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + h.TempDir(filepath.Join("src", projectDir)) + h.TempCopy(filepath.Join(projectDir, glideConfigName), "glide.yaml") + h.TempCopy(filepath.Join(projectDir, glideLockName), "glide.lock") + + i := newGlideImporter(h.Path(projectDir), projectName) + + exists, err := i.HasConfig() + h.Must(err) + if !exists { + t.Fatal("Expected the importer to detect the glide configuration file") + } + + pd, err := i.LoadConfig() + h.Must(err) + + want := "github.com/sdboyer/dep-test" + var found bool + for i := range pd.Ignored { + if pd.Ignored[i] == want { + found = true + break + } + } + if !found { + t.Fatalf("Expected ignores to contain %s, got '%s'", want, pd.Ignored) + } +} + +func TestImportGlideExcludeDir(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + h.TempDir(filepath.Join("src", projectDir)) + h.TempCopy(filepath.Join(projectDir, glideConfigName), "glide.yaml") + h.TempCopy(filepath.Join(projectDir, glideLockName), "glide.lock") + + i := newGlideImporter(h.Path(projectDir), projectName) + + exists, err := i.HasConfig() + h.Must(err) + if !exists { + t.Fatal("Expected the importer to detect the glide configuration file") + } + + pd, err := i.LoadConfig() + h.Must(err) + + want := path.Join(projectName, "samples") + var found bool + for i := range pd.Ignored { + if pd.Ignored[i] == want { + found = true + break + } + } + if !found { + t.Fatalf("Expected ignores to contain %s, got '%s'", want, pd.Ignored) + } +} + +func TestImportGlideExcludeDir_IgnoresMisconfiguredPackageName(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + h.TempDir(filepath.Join("src", projectDir)) + h.TempCopy(filepath.Join(projectDir, glideConfigName), "glide-bad-package-name.yaml") + h.TempCopy(filepath.Join(projectDir, glideLockName), "glide.lock") + + i := newGlideImporter(h.Path(projectDir), projectName) + + exists, err := i.HasConfig() + h.Must(err) + if !exists { + t.Fatal("Expected the importer to detect the glide configuration file") + } + + pd, err := i.LoadConfig() + h.Must(err) + + want := path.Join(projectName, "samples") + var found bool + for i := range pd.Ignored { + if pd.Ignored[i] == want { + found = true + break + } + } + if !found { + t.Fatalf("Expected ignores to contain %s, got '%s'", want, pd.Ignored) + } +} diff --git a/internal/importer/importer.go b/internal/importer/importer.go new file mode 100644 index 0000000000..2db3a2fca6 --- /dev/null +++ b/internal/importer/importer.go @@ -0,0 +1,48 @@ +// Copyright 2016 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 importer + +import ( + "github.com/golang/dep/gps" + "github.com/golang/dep/internal" + "github.com/pkg/errors" +) + +// ImportedProjectData provides hints about existing dependencies +// imported from another dependency manager. +type ImportedProjectData struct { + Dependencies gps.ProjectConstraints + Ignored []string +} + +type configImporter interface { + Name() string + HasConfig() (bool, error) + LoadConfig() (*ImportedProjectData, error) +} + +// Import scans a directory for configuration from other dependency managers +// and loads usable config values. +// dir - the root project directory, e.g. ~/bar +// projectRoot - the project's name, e.g. github.com/foo/bar +func Import(dir string, project string) (*ImportedProjectData, error) { + importers := []configImporter{ + newGlideImporter(dir, project), + } + + for _, i := range importers { + has, err := i.HasConfig() + if err != nil { + return nil, errors.Wrapf(err, "Unable to read") + } + + if has { + internal.Vlogf("Importing %s configuration. Run with -skip-tools to skip.", i.Name()) + return i.LoadConfig() + } + } + + return nil, nil +} diff --git a/internal/importer/importer_test.go b/internal/importer/importer_test.go new file mode 100644 index 0000000000..53d4e36c66 --- /dev/null +++ b/internal/importer/importer_test.go @@ -0,0 +1,5 @@ +// Copyright 2016 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 importer diff --git a/internal/importer/testdata/glide-bad-package-name.yaml b/internal/importer/testdata/glide-bad-package-name.yaml new file mode 100644 index 0000000000..7a74b43f3f --- /dev/null +++ b/internal/importer/testdata/glide-bad-package-name.yaml @@ -0,0 +1,3 @@ +package: github.com/golang/notexist1 +excludeDirs: +- samples \ No newline at end of file diff --git a/internal/importer/testdata/glide.lock b/internal/importer/testdata/glide.lock new file mode 100644 index 0000000000..c1eb026de9 --- /dev/null +++ b/internal/importer/testdata/glide.lock @@ -0,0 +1,21 @@ +package: github.com/golang/notexist +homepage: http://example.com +license: MIT +owners: +- name: Sam Boyer + email: sdboyer@example.com + homepage: http://sdboyer.io +ignore: +- github.com/sdboyer/dep-test +excludeDir: +- samples +import: +- package: github.com/sdboyer/deptest + repo: https://github.com/carolynvs/deptest.git + vcs: git + version: master +- package: github.com/sdboyer/deptestdos + version: v2.0.0 +testImport: +- package: github.com/golang/lint + version: cb00e5669539f047b2f4c53a421a01b0c8e172c6 \ No newline at end of file diff --git a/internal/importer/testdata/glide.yaml b/internal/importer/testdata/glide.yaml new file mode 100644 index 0000000000..60ac8f5a2d --- /dev/null +++ b/internal/importer/testdata/glide.yaml @@ -0,0 +1,20 @@ +package: github.com/golang/notexist +homepage: http://example.com +license: MIT +owners: +- name: Sam Boyer + email: sdboyer@example.com + homepage: http://sdboyer.io +ignore: +- github.com/sdboyer/dep-test +excludeDirs: +- samples +import: +- package: github.com/sdboyer/deptest + repo: https://github.com/carolynvs/deptest.git + vcs: git + version: master +- package: github.com/sdboyer/deptestdos + version: v2.0.0 +testImport: +- package: github.com/golang/lint \ No newline at end of file