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

Commit

Permalink
Read other dependency mgrs cfg during init
Browse files Browse the repository at this point in the history
Extract initial root manifest generation into an analyzer
which combines evaluating the contents of the GOPATH with
importing cfg from other managers.
  • Loading branch information
carolynvs committed May 12, 2017
1 parent e7bd278 commit 4a51515
Show file tree
Hide file tree
Showing 19 changed files with 723 additions and 83 deletions.
72 changes: 72 additions & 0 deletions cmd/dep/compositeAnalyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 main

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

// compositeAnalyzer overlays configuration from multiple analyzers
type compositeAnalyzer struct {
// Analyzers is the set of analyzers to apply, last one wins any conflicts
Analyzers []rootProjectAnalyzer
}

func (a compositeAnalyzer) DeriveRootManifestAndLock(path string, n gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
var rootM *dep.Manifest
var rootL *dep.Lock

for _, a := range a.Analyzers {
m, l, err := a.DeriveRootManifestAndLock(path, n)
if err != nil {
return nil, nil, errors.Wrapf(err, "%T.DeriveRootManifestAndLock", a)
}

if rootM == nil && rootL == nil {
rootM = m
rootL = l
} else {
// Overlay the changes from the analyzer on-top of the previous analyzer's work
if m != nil {
for pkg, prj := range m.Dependencies {
rootM.Dependencies[pkg] = prj
}
for pkg, prj := range m.Ovr {
rootM.Ovr[pkg] = prj
}
for _, pkg := range m.Required {
if !contains(rootM.Required, pkg) {
rootM.Required = append(rootM.Required, pkg)
}
}
for _, pkg := range m.Ignored {
if !contains(rootM.Ignored, pkg) {
rootM.Ignored = append(rootM.Ignored, pkg)
}
}
}

if l != nil {
for _, lp := range l.P {
for i, existingLP := range rootL.P {
if lp.Ident().ProjectRoot == existingLP.Ident().ProjectRoot {
rootL.P[i] = lp
}
}
}
}
}
}

return rootM, rootL, nil
}

func (a compositeAnalyzer) PostSolveShenanigans(m *dep.Manifest, l *dep.Lock) {
for _, a := range a.Analyzers {
a.PostSolveShenanigans(m, l)
}
}
181 changes: 181 additions & 0 deletions cmd/dep/compositeAnalyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// 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 main

import (
"testing"

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
)

type testRootProjectAnalyzer struct {
*dep.Manifest
*dep.Lock
}

func (a testRootProjectAnalyzer) DeriveRootManifestAndLock(path string, n gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
return a.Manifest, a.Lock, nil
}

func (a testRootProjectAnalyzer) PostSolveShenanigans(*dep.Manifest, *dep.Lock) {
// do nothing
}

func TestCompositeAnalyzer_ManifestDependencies(t *testing.T) {
pkg := gps.ProjectRoot("github.com/sdboyer/deptest")
m1 := &dep.Manifest{
Dependencies: gps.ProjectConstraints{
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")},
},
}
m2 := &dep.Manifest{
Dependencies: gps.ProjectConstraints{
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")},
},
}
c := compositeAnalyzer{
Analyzers: []rootProjectAnalyzer{
testRootProjectAnalyzer{Manifest: m1},
testRootProjectAnalyzer{Manifest: m2},
},
}

rm, _, err := c.DeriveRootManifestAndLock("", "")
if err != nil {
t.Fatal(err)
}

if rm == nil {
t.Fatal("Expected the root manifest to not be nil")
}

dep, has := rm.Dependencies[pkg]
if !has {
t.Fatal("Expected the root manifest to contain the test project")
}

wantC := "^2.0.0"
gotC := dep.Constraint.String()
if wantC != gotC {
t.Fatalf("Expected the test project to be constrained to '%s', got '%s'", wantC, gotC)
}
}

func TestCompositeAnalyzer_ManifestOverrides(t *testing.T) {
pkg := gps.ProjectRoot("github.com/sdboyer/deptest")
m1 := &dep.Manifest{
Ovr: gps.ProjectConstraints{
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^1.0.0")},
},
}
m2 := &dep.Manifest{
Ovr: gps.ProjectConstraints{
pkg: gps.ProjectProperties{Constraint: gps.NewVersion("^2.0.0")},
},
}
c := compositeAnalyzer{
Analyzers: []rootProjectAnalyzer{
testRootProjectAnalyzer{Manifest: m1},
testRootProjectAnalyzer{Manifest: m2},
},
}

rm, _, err := c.DeriveRootManifestAndLock("", "")
if err != nil {
t.Fatal(err)
}

if rm == nil {
t.Fatal("Expected the root manifest to not be nil")
}

dep, has := rm.Ovr[pkg]
if !has {
t.Fatal("Expected the root manifest to contain the test project override")
}

wantC := "^2.0.0"
gotC := dep.Constraint.String()
if wantC != gotC {
t.Fatalf("Expected the test project to be overridden to '%s', got '%s'", wantC, gotC)
}
}

func TestCompositeAnalyzer_ManifestRequired(t *testing.T) {
pkg1 := "github.com/sdboyer/deptest"
pkg2 := "github.com/sdboyer/deptestdos"
m1 := &dep.Manifest{
Required: []string{pkg1},
}
m2 := &dep.Manifest{
Required: []string{pkg2},
}
c := compositeAnalyzer{
Analyzers: []rootProjectAnalyzer{
testRootProjectAnalyzer{Manifest: m1},
testRootProjectAnalyzer{Manifest: m2},
},
}

rm, _, err := c.DeriveRootManifestAndLock("", "")
if err != nil {
t.Fatal(err)
}

if rm == nil {
t.Fatal("Expected the root manifest to not be nil")
}

if len(rm.Required) != 2 {
t.Fatalf("Expected the root manifest to contain 2 required packages, got %d", len(rm.Required))
}

if rm.Required[0] != pkg1 {
t.Fatalf("Expected the first required package to be '%s', got '%s'", pkg1, rm.Required[0])
}

if rm.Required[1] != pkg2 {
t.Fatalf("Expected the second required package to be '%s', got '%s'", pkg2, rm.Required[1])
}
}

func TestCompositeAnalyzer_ManifestIgnored(t *testing.T) {
pkg1 := "github.com/sdboyer/deptest"
pkg2 := "github.com/sdboyer/deptestdos"
m1 := &dep.Manifest{
Ignored: []string{pkg1},
}
m2 := &dep.Manifest{
Ignored: []string{pkg2},
}
c := compositeAnalyzer{
Analyzers: []rootProjectAnalyzer{
testRootProjectAnalyzer{Manifest: m1},
testRootProjectAnalyzer{Manifest: m2},
},
}

rm, _, err := c.DeriveRootManifestAndLock("", "")
if err != nil {
t.Fatal(err)
}

if rm == nil {
t.Fatal("Expected the root manifest to not be nil")
}

if len(rm.Ignored) != 2 {
t.Fatalf("Expected the root manifest to contain 2 ignored packages, got %d", len(rm.Ignored))
}

if rm.Ignored[0] != pkg1 {
t.Fatalf("Expected the first ignored package to be '%s', got '%s'", pkg1, rm.Ignored[0])
}

if rm.Ignored[1] != pkg2 {
t.Fatalf("Expected the second ignored package to be '%s', got '%s'", pkg2, rm.Ignored[1])
}
}
68 changes: 37 additions & 31 deletions cmd/dep/glideConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

type glideFiles struct {
yaml glideYaml
lock glideLock
loggers *Loggers
lock *glideLock
loggers *dep.Loggers
}

func newGlideFiles(loggers *Loggers) glideFiles {
return glideFiles{loggers: loggers}
func newGlideFiles(loggers *dep.Loggers) *glideFiles {
return &glideFiles{loggers: loggers}
}

type glideYaml struct {
Expand All @@ -44,7 +44,7 @@ type glidePackage struct {
Repository string `yaml:"repo"`
}

func (files glideFiles) load(projectDir string) error {
func (files *glideFiles) load(projectDir string) error {
y := filepath.Join(projectDir, glideYamlName)
if files.loggers.Verbose {
files.loggers.Err.Printf("dep: Loading %s", y)
Expand All @@ -59,22 +59,24 @@ func (files glideFiles) load(projectDir string) error {
}

l := filepath.Join(projectDir, glideLockName)
if files.loggers.Verbose {
files.loggers.Err.Printf("dep: Loading %s", l)
}
lb, err := ioutil.ReadFile(l)
if err != nil {
return errors.Wrapf(err, "Unable to read %s", l)
}
err = yaml.Unmarshal(lb, &files.lock)
if err != nil {
return errors.Wrapf(err, "Unable to parse %s", l)
if exists, _ := dep.IsRegular(l); exists {
if files.loggers.Verbose {
files.loggers.Err.Printf("dep: Loading %s", l)
}
lb, err := ioutil.ReadFile(l)
if err != nil {
return errors.Wrapf(err, "Unable to read %s", l)
}
err = yaml.Unmarshal(lb, &files.lock)
if err != nil {
return errors.Wrapf(err, "Unable to parse %s", l)
}
}

return nil
}

func (files glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, error) {
func (files *glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, error) {
manifest := &dep.Manifest{
Dependencies: make(gps.ProjectConstraints),
}
Expand Down Expand Up @@ -105,24 +107,28 @@ func (files glideFiles) convert(projectName string) (*dep.Manifest, *dep.Lock, e
}
}

lock := &dep.Lock{}
var lock *dep.Lock
if files.lock != nil {
lock = &dep.Lock{}

lockDep := func(pkg glidePackage) {
id := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name)}
c, has := manifest.Dependencies[id.ProjectRoot]
if has {
id.Source = c.Source
}
version := gps.Revision(pkg.Reference)

lockDep := func(pkg glidePackage) {
id := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name)}
c, has := manifest.Dependencies[id.ProjectRoot]
if has {
id.Source = c.Source
lp := gps.NewLockedProject(id, version, nil)
lock.P = append(lock.P, lp)
}
version := gps.Revision(pkg.Reference)

lp := gps.NewLockedProject(id, version, nil)
lock.P = append(lock.P, lp)
}
for _, pkg := range files.lock.Imports {
lockDep(pkg)
}
for _, pkg := range files.lock.TestImports {
lockDep(pkg)
for _, pkg := range files.lock.Imports {
lockDep(pkg)
}
for _, pkg := range files.lock.TestImports {
lockDep(pkg)
}
}

return manifest, lock, nil
Expand Down
Loading

0 comments on commit 4a51515

Please sign in to comment.