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

Commit 34e1de4

Browse files
committed
fix(manifest): validate project roots in manifest
Validating ProjectRoot(s) require source manager, which is created after loading the project. Hence, ProjectRoot validation can't be done in existing validateManifest. This change adds `ValidateProjectRoots()` which validates only the Constraint and Override names to be valid ProjectRoot. Warnings are issued at stderr when invalid ProjectRoot is found in manifest. `ensure` and `status` call `ValidateProjectRoots()` expecitly.
1 parent 3b01418 commit 34e1de4

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

cmd/dep/ensure.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
160160
sm.UseDefaultSignalHandling()
161161
defer sm.Release()
162162

163+
if err := dep.ValidateProjectRoots(ctx, p.Manifest, sm); err != nil {
164+
return err
165+
}
166+
163167
params := p.MakeParams()
164168
if ctx.Verbose {
165169
params.TraceLogger = ctx.Err

cmd/dep/status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
190190
sm.UseDefaultSignalHandling()
191191
defer sm.Release()
192192

193+
if err := dep.ValidateProjectRoots(ctx, p.Manifest, sm); err != nil {
194+
return err
195+
}
196+
193197
var buf bytes.Buffer
194198
var out outputter
195199
switch {

manifest.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ func validateManifest(s string) ([]error, error) {
156156
return warns, nil
157157
}
158158

159+
// ValidateProjectRoots validates the project roots present in manifest.
160+
func ValidateProjectRoots(c *Ctx, m *Manifest, sm gps.SourceManager) error {
161+
projectRoots := make([]gps.ProjectRoot, 0, len(m.Constraints)+len(m.Ovr))
162+
for pr := range m.Constraints {
163+
projectRoots = append(projectRoots, pr)
164+
}
165+
for pr := range m.Ovr {
166+
projectRoots = append(projectRoots, pr)
167+
}
168+
169+
for _, pr := range projectRoots {
170+
origPR, err := sm.DeduceProjectRoot(string(pr))
171+
if err != nil {
172+
return errors.Wrapf(err, "could not deduce project root for %s", pr)
173+
}
174+
if origPR != pr {
175+
c.Err.Printf("dep: WARNING: name %q in Gopkg.toml should be project root", pr)
176+
}
177+
}
178+
179+
return nil
180+
}
181+
159182
// readManifest returns a Manifest read from r and a slice of validation warnings.
160183
func readManifest(r io.Reader) (*Manifest, []error, error) {
161184
buf := &bytes.Buffer{}

manifest_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package dep
66

77
import (
8+
"bytes"
89
"errors"
10+
"io/ioutil"
11+
"log"
912
"reflect"
1013
"strings"
1114
"testing"
@@ -372,3 +375,110 @@ func TestValidateManifest(t *testing.T) {
372375
}
373376
}
374377
}
378+
379+
func TestValidateProjectRoots(t *testing.T) {
380+
cases := []struct {
381+
name string
382+
manifest Manifest
383+
wantError bool
384+
wantWarn []string
385+
}{
386+
{
387+
name: "empty Manifest",
388+
manifest: Manifest{},
389+
wantError: false,
390+
wantWarn: []string{},
391+
},
392+
{
393+
name: "valid project root",
394+
manifest: Manifest{
395+
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
396+
gps.ProjectRoot("github.com/goland/dep"): {
397+
Constraint: gps.Any(),
398+
},
399+
},
400+
},
401+
wantError: false,
402+
wantWarn: []string{},
403+
},
404+
{
405+
name: "invalid project roots in Constraints and Overrides",
406+
manifest: Manifest{
407+
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
408+
gps.ProjectRoot("github.com/golang/dep/foo"): {
409+
Constraint: gps.Any(),
410+
},
411+
gps.ProjectRoot("github.com/golang/go/xyz"): {
412+
Constraint: gps.Any(),
413+
},
414+
gps.ProjectRoot("github.com/golang/fmt"): {
415+
Constraint: gps.Any(),
416+
},
417+
},
418+
Ovr: map[gps.ProjectRoot]gps.ProjectProperties{
419+
gps.ProjectRoot("github.com/golang/mock/bar"): {
420+
Constraint: gps.Any(),
421+
},
422+
gps.ProjectRoot("github.com/golang/mock"): {
423+
Constraint: gps.Any(),
424+
},
425+
},
426+
},
427+
wantError: false,
428+
wantWarn: []string{
429+
"name \"github.com/golang/dep/foo\" in Gopkg.toml should be project root",
430+
"name \"github.com/golang/mock/bar\" in Gopkg.toml should be project root",
431+
"name \"github.com/golang/go/xyz\" in Gopkg.toml should be project root",
432+
},
433+
},
434+
{
435+
name: "invalid source path",
436+
manifest: Manifest{
437+
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
438+
gps.ProjectRoot("github.com/golang"): {
439+
Constraint: gps.Any(),
440+
},
441+
},
442+
},
443+
wantError: true,
444+
wantWarn: []string{},
445+
},
446+
}
447+
448+
h := test.NewHelper(t)
449+
defer h.Cleanup()
450+
451+
h.TempDir("src")
452+
pwd := h.Path(".")
453+
454+
// Capture the stderr to verify the warnings
455+
stderrOutput := &bytes.Buffer{}
456+
errLogger := log.New(stderrOutput, "", 0)
457+
ctx := &Ctx{
458+
GOPATH: pwd,
459+
Out: log.New(ioutil.Discard, "", 0),
460+
Err: errLogger,
461+
}
462+
463+
sm, err := ctx.SourceManager()
464+
h.Must(err)
465+
defer sm.Release()
466+
467+
for _, c := range cases {
468+
t.Run(c.name, func(t *testing.T) {
469+
// Empty the buffer for every case
470+
stderrOutput.Reset()
471+
err := ValidateProjectRoots(ctx, &c.manifest, sm)
472+
if err != nil && !c.wantError {
473+
t.Fatalf("Unexpected error while validating project roots: %q", err)
474+
}
475+
476+
warnings := stderrOutput.String()
477+
for _, warn := range c.wantWarn {
478+
if !strings.Contains(warnings, warn) {
479+
t.Fatalf("Expected ValidateProjectRoot warnings to contain: %q", warn)
480+
}
481+
}
482+
})
483+
}
484+
}

0 commit comments

Comments
 (0)