Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for pip resolution #150

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/resolution/job/base_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func TestErrors(t *testing.T) {
j := BaseJob{}
j.file = testFile
j.errs = NewErrors(j.file)
j.errs.Critical(jobErr)
jobError := NewBaseJobError(jobErr.Error())
j.errs.Critical(jobError)

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), jobErr)
assert.Contains(t, j.Errors().GetAll(), jobError)
}

func TestSendStatus(t *testing.T) {
Expand Down
52 changes: 51 additions & 1 deletion internal/resolution/job/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
package job

type IError interface {
error
Error() string
Command() string
Documentation() string
Status() string
SetStatus(string)
SetDocumentation(string)
SetCommand(string)
}

type BaseJobError struct {
err string
command string
documentation string
status string
}

func (e BaseJobError) Error() string {
return e.err
}

func (e BaseJobError) Command() string {
return e.command
}

func (e BaseJobError) Documentation() string {
return e.documentation + "\n"
}

func (e BaseJobError) Status() string {
return e.status
}

func (e *BaseJobError) SetStatus(status string) {
e.status = status
}

func (e *BaseJobError) SetDocumentation(doc string) {
e.documentation = doc
}

func (e *BaseJobError) SetCommand(command string) {
e.command = command
}

func NewBaseJobError(err string) *BaseJobError {
return &BaseJobError{
err: err,
command: "",
documentation: "",
status: "",
}
}
75 changes: 75 additions & 0 deletions internal/resolution/job/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package job

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewBaseJobError(t *testing.T) {
error_message := "error"
jobError := NewBaseJobError(error_message)
assert.Equal(t, error_message, jobError.err)
assert.Equal(t, string(""), jobError.documentation)
assert.NotNil(t, jobError)
}

func TestBaseJobErrorError(t *testing.T) {
jobError := BaseJobError{
err: "error",
command: "",
documentation: "",
status: "",
}
assert.Equal(t, "error", jobError.Error())
}

func TestBaseJobErrorCommand(t *testing.T) {
jobError := BaseJobError{
err: "",
command: "command",
documentation: "",
status: "",
}
assert.Equal(t, "command", jobError.Command())
}

func TestBaseJobErrorSetCommand(t *testing.T) {
jobError := NewBaseJobError("")
assert.Equal(t, "", jobError.Command())
jobError.SetCommand("command")
assert.Equal(t, "command", jobError.Command())
}

func TestBaseJobErrorDocumentation(t *testing.T) {
jobError := BaseJobError{
err: "",
command: "",
documentation: "documentation",
status: "",
}
assert.Equal(t, "documentation\n", jobError.Documentation())
}

func TestBaseJobErrorSetDocumentation(t *testing.T) {
jobError := NewBaseJobError("")
assert.Equal(t, "\n", jobError.Documentation())
jobError.SetDocumentation("documentation")
assert.Equal(t, "documentation\n", jobError.Documentation())
}
func TestBaseJobErrorStatus(t *testing.T) {
jobError := BaseJobError{
err: "",
command: "",
documentation: "",
status: "status",
}
assert.Equal(t, "status", jobError.Status())
}

func TestBaseJobErrorSetStatus(t *testing.T) {
jobError := NewBaseJobError("")
assert.Equal(t, "", jobError.Status())
jobError.SetStatus("status")
assert.Equal(t, "status", jobError.Status())
}
18 changes: 8 additions & 10 deletions internal/resolution/job/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package job

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +17,7 @@ func TestNewErrors(t *testing.T) {

func TestWarning(t *testing.T) {
errors := NewErrors("")
warning := fmt.Errorf("error")
warning := NewBaseJobError("error")
errors.Warning(warning)
assert.Empty(t, errors.criticalErrs)
assert.Len(t, errors.warningErrs, 1)
Expand All @@ -27,7 +26,7 @@ func TestWarning(t *testing.T) {

func TestCritical(t *testing.T) {
errors := NewErrors("")
critical := fmt.Errorf("error")
critical := NewBaseJobError("error")
errors.Critical(critical)
assert.Empty(t, errors.warningErrs)
assert.Len(t, errors.criticalErrs, 1)
Expand All @@ -36,7 +35,7 @@ func TestCritical(t *testing.T) {

func TestGetWarningErrors(t *testing.T) {
errors := NewErrors("")
warning := fmt.Errorf("error")
warning := NewBaseJobError("error")
errors.Warning(warning)
assert.Empty(t, errors.GetCriticalErrors())
assert.Len(t, errors.GetWarningErrors(), 1)
Expand All @@ -45,7 +44,7 @@ func TestGetWarningErrors(t *testing.T) {

func TestGetCriticalErrors(t *testing.T) {
errors := NewErrors("")
critical := fmt.Errorf("error")
critical := NewBaseJobError("critical")
errors.Critical(critical)
assert.Empty(t, errors.GetWarningErrors())
assert.Len(t, errors.GetCriticalErrors(), 1)
Expand All @@ -54,8 +53,8 @@ func TestGetCriticalErrors(t *testing.T) {

func TestGetAll(t *testing.T) {
errors := NewErrors("")
warning := fmt.Errorf("warning")
critical := fmt.Errorf("critical")
warning := NewBaseJobError("warning")
critical := NewBaseJobError("critical")
errors.Warning(warning)
errors.Critical(critical)
assert.Len(t, errors.GetAll(), 2)
Expand All @@ -67,11 +66,10 @@ func TestHasError(t *testing.T) {
errors := NewErrors("")
assert.False(t, errors.HasError())

warning := fmt.Errorf("warning")
warning := NewBaseJobError("warning")
errors.Warning(warning)
assert.True(t, errors.HasError())

critical := fmt.Errorf("critical")
critical := NewBaseJobError("critical")
errors.Warning(critical)
assert.True(t, errors.HasError())
}
4 changes: 3 additions & 1 deletion internal/resolution/pm/composer/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package composer

import (
"github.com/debricked/cli/internal/resolution/job"
"github.com/debricked/cli/internal/resolution/pm/util"
)

const (
Expand Down Expand Up @@ -37,7 +38,8 @@ func (j *Job) Run() {
j.SendStatus("installing dependencies")
_, err := j.runInstallCmd()
if err != nil {
j.Errors().Critical(err)
cmdErr := util.NewPMJobError(err.Error())
j.Errors().Critical(cmdErr)

return
}
Expand Down
1 change: 0 additions & 1 deletion internal/resolution/pm/composer/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestRunInstallCmdErr(t *testing.T) {
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), cmdErr)
}

func TestRunInstallCmdOutputErr(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions internal/resolution/pm/gomod/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ func (j *Job) Run() {

graphCmdOutput, err := j.runGraphCmd(workingDirectory)
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(util.NewPMJobError(err.Error()))

return
}

j.SendStatus("creating dependency version list")
listCmdOutput, err := j.runListCmd(workingDirectory)
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(util.NewPMJobError(err.Error()))

return
}

j.SendStatus("creating lock file")
lockFile, err := j.fileWriter.Create(util.MakePathFromManifestFile(j.GetFile(), fileName))
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(util.NewPMJobError(err.Error()))

return
}
Expand All @@ -66,7 +66,7 @@ func (j *Job) Run() {

err = j.fileWriter.Write(lockFile, fileContents)
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(util.NewPMJobError(err.Error()))
}
}

Expand Down
11 changes: 6 additions & 5 deletions internal/resolution/pm/gomod/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

jobTestdata "github.com/debricked/cli/internal/resolution/job/testdata"
"github.com/debricked/cli/internal/resolution/pm/gomod/testdata"
"github.com/debricked/cli/internal/resolution/pm/util"
"github.com/debricked/cli/internal/resolution/pm/writer"
writerTestdata "github.com/debricked/cli/internal/resolution/pm/writer/testdata"
"github.com/stretchr/testify/assert"
Expand All @@ -27,7 +28,7 @@ func TestRunGraphCmdErr(t *testing.T) {

j.Run()

assert.Contains(t, j.Errors().GetCriticalErrors(), cmdErr)
assert.Contains(t, j.Errors().GetCriticalErrors(), util.NewPMJobError(cmdErr.Error()))
}

func TestRunCmdOutputErr(t *testing.T) {
Expand All @@ -53,7 +54,7 @@ func TestRunListCmdErr(t *testing.T) {
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), cmdErr)
assert.Contains(t, j.Errors().GetAll(), util.NewPMJobError(cmdErr.Error()))
}

func TestRunListCmdOutputErr(t *testing.T) {
Expand All @@ -79,7 +80,7 @@ func TestRunCreateErr(t *testing.T) {
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), createErr)
assert.Contains(t, j.Errors().GetAll(), util.NewPMJobError(createErr.Error()))
}

func TestRunWriteErr(t *testing.T) {
Expand All @@ -93,7 +94,7 @@ func TestRunWriteErr(t *testing.T) {
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), writeErr)
assert.Contains(t, j.Errors().GetAll(), util.NewPMJobError(writeErr.Error()))
}

func TestRunCloseErr(t *testing.T) {
Expand All @@ -107,7 +108,7 @@ func TestRunCloseErr(t *testing.T) {
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Contains(t, j.Errors().GetAll(), closeErr)
assert.Contains(t, j.Errors().GetAll(), util.NewPMJobError(closeErr.Error()))
}

func TestRun(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions internal/resolution/pm/gradle/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/debricked/cli/internal/resolution/job"
"github.com/debricked/cli/internal/resolution/pm/util"
"github.com/debricked/cli/internal/resolution/pm/writer"
)

Expand Down Expand Up @@ -52,9 +53,9 @@ func (j *Job) Run() {

if err != nil {
if permissionErr != nil {
j.Errors().Critical(permissionErr)
j.Errors().Critical(util.NewPMJobError(permissionErr.Error()))
}
j.Errors().Critical(err)
j.Errors().Critical(util.NewPMJobError(err.Error()))

return
}
Expand All @@ -63,11 +64,11 @@ func (j *Job) Run() {
_, err = dependenciesCmd.Output()

if permissionErr != nil {
j.Errors().Warning(permissionErr)
j.Errors().Warning(util.NewPMJobError(permissionErr.Error()))
}

if err != nil {
j.Errors().Critical(j.GetExitError(err))
j.Errors().Critical(util.NewPMJobError(j.GetExitError(err).Error()))

return
}
Expand Down
Loading
Loading