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

Commit 8a978e5

Browse files
committed
return slice of errors
1 parent bd9c433 commit 8a978e5

File tree

2 files changed

+76
-57
lines changed

2 files changed

+76
-57
lines changed

cmd/dep/status.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,11 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
417417
return false, 0, errors.Wrapf(err, "could not set up solver for input hashing")
418418
}
419419

420-
cm, err := collectConstraints(ctx, p, sm)
421-
if err != nil {
422-
return false, 0, err
420+
// Errors while collecting constraints should not fail the whole status run.
421+
// It should count the error and tell the user about incomplete results.
422+
cm, ccerrs := collectConstraints(ctx, p, sm)
423+
if len(ccerrs) > 0 {
424+
errCount += len(ccerrs)
423425
}
424426

425427
// Get the project list and sort it so that the printed output users see is
@@ -569,7 +571,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
569571

570572
// Count ListVersions error because we get partial results when
571573
// this happens.
572-
errCount = len(errListVerCh)
574+
errCount += len(errListVerCh)
573575
if ctx.Verbose {
574576
for err := range errListVerCh {
575577
ctx.Err.Println(err.Error())
@@ -687,28 +689,31 @@ type projectConstraint struct {
687689
type constraintsCollection map[string][]projectConstraint
688690

689691
// collectConstraints collects constraints declared by all the dependencies.
690-
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (constraintsCollection, error) {
692+
// It returns constraintsCollection and a slice of errors encountered while
693+
// collecting the constraints, if any.
694+
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (constraintsCollection, []error) {
691695
logger := ctx.Err
692696
if !ctx.Verbose {
693697
logger = log.New(ioutil.Discard, "", 0)
694698
}
695699

696700
logger.Println("Collecting project constraints:")
697701

702+
var mutex sync.Mutex
703+
constraintCollection := make(constraintsCollection)
704+
698705
// Get direct deps of the root project.
699706
_, directDeps, err := getDirectDependencies(sm, p)
700707
if err != nil {
701-
return nil, err
708+
// Return empty collection, not nil, if we fail here.
709+
return constraintCollection, []error{errors.Wrap(err, "failed to get direct dependencies")}
702710
}
703711

704712
// Create a root analyzer.
705713
rootAnalyzer := newRootAnalyzer(true, ctx, directDeps, sm)
706714

707715
lp := p.Lock.Projects()
708716

709-
var mutex sync.Mutex
710-
constraintCollection := make(constraintsCollection)
711-
712717
// Channel for receiving all the errors.
713718
errCh := make(chan error, len(lp))
714719

@@ -753,14 +758,15 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
753758
wg.Wait()
754759
close(errCh)
755760

761+
var errs []error
756762
if len(errCh) > 0 {
757-
err = errors.New("failed to collect constraints")
758763
for e := range errCh {
764+
errs = append(errs, e)
759765
logger.Println(e.Error())
760766
}
761767
}
762768

763-
return constraintCollection, err
769+
return constraintCollection, errs
764770
}
765771

766772
type byProject []projectConstraint

cmd/dep/status_test.go

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"io/ioutil"
1010
"log"
11+
"path/filepath"
1112
"reflect"
1213
"testing"
1314
"text/tabwriter"
@@ -298,48 +299,50 @@ func TestCollectConstraints(t *testing.T) {
298299
ver2, _ := gps.NewSemverConstraintIC("v2.0.0")
299300

300301
cases := []struct {
301-
name string
302-
project dep.Project
302+
name string
303+
// project dep.Project
304+
lock dep.Lock
303305
wantConstraints constraintsCollection
306+
wantErr bool
304307
}{
305308
{
306309
name: "without any constraints",
307-
project: dep.Project{
308-
Lock: &dep.Lock{
309-
P: []gps.LockedProject{
310-
gps.NewLockedProject(
311-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
312-
gps.NewVersion("v1.0.0"),
313-
[]string{"."},
314-
),
315-
},
310+
// project: dep.Project{
311+
lock: dep.Lock{
312+
P: []gps.LockedProject{
313+
gps.NewLockedProject(
314+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
315+
gps.NewVersion("v1.0.0"),
316+
[]string{"."},
317+
),
316318
},
317319
},
320+
// },
318321
wantConstraints: constraintsCollection{},
319322
},
320323
{
321324
name: "with multiple constraints",
322-
project: dep.Project{
323-
Lock: &dep.Lock{
324-
P: []gps.LockedProject{
325-
gps.NewLockedProject(
326-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
327-
gps.NewVersion("v1.0.0"),
328-
[]string{"."},
329-
),
330-
gps.NewLockedProject(
331-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
332-
gps.NewVersion("v0.1.0"),
333-
[]string{"."},
334-
),
335-
gps.NewLockedProject(
336-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
337-
gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")),
338-
[]string{"."},
339-
),
340-
},
325+
// project: dep.Project{
326+
lock: dep.Lock{
327+
P: []gps.LockedProject{
328+
gps.NewLockedProject(
329+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
330+
gps.NewVersion("v1.0.0"),
331+
[]string{"."},
332+
),
333+
gps.NewLockedProject(
334+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
335+
gps.NewVersion("v0.1.0"),
336+
[]string{"."},
337+
),
338+
gps.NewLockedProject(
339+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
340+
gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")),
341+
[]string{"."},
342+
),
341343
},
342344
},
345+
// },
343346
wantConstraints: constraintsCollection{
344347
"github.com/sdboyer/deptestdos": []projectConstraint{
345348
{"github.com/darkowlzz/deptest-project-2", ver2},
@@ -355,27 +358,28 @@ func TestCollectConstraints(t *testing.T) {
355358
},
356359
{
357360
name: "skip projects with invalid versions",
358-
project: dep.Project{
359-
Lock: &dep.Lock{
360-
P: []gps.LockedProject{
361-
gps.NewLockedProject(
362-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
363-
gps.NewVersion("v0.1.0"),
364-
[]string{"."},
365-
),
366-
gps.NewLockedProject(
367-
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
368-
gps.NewVersion("v1.0.0"),
369-
[]string{"."},
370-
),
371-
},
361+
// project: dep.Project{
362+
lock: dep.Lock{
363+
P: []gps.LockedProject{
364+
gps.NewLockedProject(
365+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
366+
gps.NewVersion("v0.1.0"),
367+
[]string{"."},
368+
),
369+
gps.NewLockedProject(
370+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
371+
gps.NewVersion("v1.0.0"),
372+
[]string{"."},
373+
),
372374
},
373375
},
376+
// },
374377
wantConstraints: constraintsCollection{
375378
"github.com/sdboyer/deptest": []projectConstraint{
376379
{"github.com/darkowlzz/deptest-project-1", ver1},
377380
},
378381
},
382+
wantErr: true,
379383
},
380384
}
381385

@@ -396,12 +400,21 @@ func TestCollectConstraints(t *testing.T) {
396400
h.Must(err)
397401
defer sm.Release()
398402

403+
p := new(dep.Project)
404+
p.SetRoot(filepath.Join(pwd, "src"))
405+
399406
for _, c := range cases {
400407
t.Run(c.name, func(t *testing.T) {
401-
gotConstraints, _ := collectConstraints(ctx, &c.project, sm)
408+
p.Lock = &c.lock
409+
gotConstraints, err := collectConstraints(ctx, p, sm)
410+
if len(err) > 0 && !c.wantErr {
411+
t.Fatalf("unexpected errors while collecting constraints: %v", err)
412+
} else if len(err) == 0 && c.wantErr {
413+
t.Fatalf("expected errors while collecting constraints, but got none")
414+
}
402415

403416
if !reflect.DeepEqual(gotConstraints, c.wantConstraints) {
404-
t.Fatalf("Unexpected collected constraints: \n\t(GOT): %v\n\t(WNT): %v", gotConstraints, c.wantConstraints)
417+
t.Fatalf("unexpected collected constraints: \n\t(GOT): %v\n\t(WNT): %v", gotConstraints, c.wantConstraints)
405418
}
406419
})
407420
}

0 commit comments

Comments
 (0)