From 20589504451c0e70b7c08348e139fe0e6bb4e87c Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Sat, 3 Jun 2017 14:50:56 +0300 Subject: [PATCH] internal/gps: extract ValidateParams Signed-off-by: Ibrahim AshShohail --- cmd/dep/ensure.go | 9 +++++++-- internal/gps/hash_test.go | 35 ---------------------------------- internal/gps/solve_failures.go | 8 ++++---- internal/gps/solver.go | 18 ++++++++--------- 4 files changed, 20 insertions(+), 50 deletions(-) diff --git a/cmd/dep/ensure.go b/cmd/dep/ensure.go index 554c464eff..2f279a15e1 100644 --- a/cmd/dep/ensure.go +++ b/cmd/dep/ensure.go @@ -142,15 +142,20 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error { } } - solver, err := gps.Prepare(params, sm) + err = gps.ValidateParams(params, sm) if err != nil { - if deduceErrs, ok := err.(gps.DeductionFailureErrs); ok { + if deduceErrs, ok := err.(gps.DeductionErrs); ok { ctx.Loggers.Err.Println("The following errors occurred while deducing packages:") for ip, dErr := range deduceErrs { ctx.Loggers.Err.Printf(" * \"%s\": %s", ip, dErr) } ctx.Loggers.Err.Println() } + return errors.Wrap(err, "validateParams") + } + + solver, err := gps.Prepare(params, sm) + if err != nil { return errors.Wrap(err, "ensure Prepare") } diff --git a/internal/gps/hash_test.go b/internal/gps/hash_test.go index 5a7dc28269..ac2300a6df 100644 --- a/internal/gps/hash_test.go +++ b/internal/gps/hash_test.go @@ -27,13 +27,6 @@ func TestHashInputs(t *testing.T) { s, err := Prepare(params, newdepspecSM(fix.ds, nil)) if err != nil { - if deduceErrs, ok := err.(DeductionFailureErrs); ok { - t.Error("The following errors occurred while deducing packages:") - for ip, dErr := range deduceErrs { - t.Errorf(" * \"%s\": %s", ip, dErr) - } - t.Error() - } t.Fatalf("Unexpected error while prepping solver: %s", err) } @@ -87,13 +80,6 @@ func TestHashInputsReqsIgs(t *testing.T) { s, err := Prepare(params, newdepspecSM(fix.ds, nil)) if err != nil { - if deduceErrs, ok := err.(DeductionFailureErrs); ok { - t.Error("The following errors occurred while deducing packages:") - for ip, dErr := range deduceErrs { - t.Errorf(" * \"%s\": %s", ip, dErr) - } - t.Error() - } t.Fatalf("Unexpected error while prepping solver: %s", err) } @@ -136,13 +122,6 @@ func TestHashInputsReqsIgs(t *testing.T) { s, err = Prepare(params, newdepspecSM(fix.ds, nil)) if err != nil { - if deduceErrs, ok := err.(DeductionFailureErrs); ok { - t.Error("The following errors occurred while deducing packages:") - for ip, dErr := range deduceErrs { - t.Errorf(" * \"%s\": %s", ip, dErr) - } - t.Error() - } t.Fatalf("Unexpected error while prepping solver: %s", err) } @@ -183,13 +162,6 @@ func TestHashInputsReqsIgs(t *testing.T) { s, err = Prepare(params, newdepspecSM(fix.ds, nil)) if err != nil { - if deduceErrs, ok := err.(DeductionFailureErrs); ok { - t.Error("The following errors occurred while deducing packages:") - for ip, dErr := range deduceErrs { - t.Errorf(" * \"%s\": %s", ip, dErr) - } - t.Error() - } t.Fatalf("Unexpected error while prepping solver: %s", err) } @@ -556,13 +528,6 @@ func TestHashInputsOverrides(t *testing.T) { s, err := Prepare(params, newdepspecSM(basefix.ds, nil)) if err != nil { - if deduceErrs, ok := err.(DeductionFailureErrs); ok { - t.Error("The following errors occurred while deducing packages:") - for ip, dErr := range deduceErrs { - t.Errorf(" * \"%s\": %s", ip, dErr) - } - t.Error() - } t.Errorf("(fix: %q) Unexpected error while prepping solver: %s", fix.name, err) continue } diff --git a/internal/gps/solve_failures.go b/internal/gps/solve_failures.go index b46bf98e73..aea0eed268 100644 --- a/internal/gps/solve_failures.go +++ b/internal/gps/solve_failures.go @@ -239,11 +239,11 @@ func (e badOptsFailure) Error() string { return string(e) } -// DeductionFailureErrs maps package import path to errors occurring during deduction. -type DeductionFailureErrs map[string]error +// DeductionErrs maps package import path to errors occurring during deduction. +type DeductionErrs map[string]error -func (e DeductionFailureErrs) Error() string { - return "could not deduce packages to ensure constraints are solvable" +func (e DeductionErrs) Error() string { + return "could not deduce external imports' project roots" } type sourceMismatchFailure struct { diff --git a/internal/gps/solver.go b/internal/gps/solver.go index 78b5abbcfb..151c3e6d36 100644 --- a/internal/gps/solver.go +++ b/internal/gps/solver.go @@ -284,11 +284,6 @@ func Prepare(params SolveParameters, sm SourceManager) (Solver, error) { params.stdLibFn = paths.IsStandardImportPath } - // Validate the solver parameters - if err := validateParams(sm, rd, params.stdLibFn); err != nil { - return nil, err - } - s := &solver{ tl: params.TraceLogger, stdLibFn: params.stdLibFn, @@ -538,13 +533,18 @@ func (s *solver) solve() (map[atom]map[string]struct{}, error) { return projs, nil } -// validateParams validates the solver parameters to ensure solving can be completed. -func validateParams(sm SourceManager, rd rootdata, stdLibFn func(string) bool) error { +// ValidateParams validates the solver parameters to ensure solving can be completed. +func ValidateParams(params SolveParameters, sm SourceManager) error { // Ensure that all packages are deducible without issues. var deducePkgsGroup sync.WaitGroup - deductionErrs := make(DeductionFailureErrs) + deductionErrs := make(DeductionErrs) var errsMut sync.Mutex + rd, err := params.toRootdata() + if err != nil { + return err + } + deducePkg := func(ip string, sm SourceManager) { _, err := sm.DeduceProjectRoot(ip) if err != nil { @@ -555,7 +555,7 @@ func validateParams(sm SourceManager, rd rootdata, stdLibFn func(string) bool) e deducePkgsGroup.Done() } - for _, ip := range rd.externalImportList(stdLibFn) { + for _, ip := range rd.externalImportList(paths.IsStandardImportPath) { deducePkgsGroup.Add(1) go deducePkg(ip, sm) }