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

Commit

Permalink
internal/gps: extract ValidateParams
Browse files Browse the repository at this point in the history
Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Jun 3, 2017
1 parent 3a28237 commit 2058950
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
9 changes: 7 additions & 2 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
35 changes: 0 additions & 35 deletions internal/gps/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/gps/solve_failures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions internal/gps/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 2058950

Please sign in to comment.