Skip to content
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
11 changes: 11 additions & 0 deletions integration-tests/tests/doctor_fix_shows_remediation.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test that cog doctor --fix prints the remediation text for unfixed findings.

! cog doctor --fix
stderr 'predict.py not found'
stderr 'Remediation: Create predict.py or update the predict field in cog.yaml'
! stderr '(no auto-fix available)'

-- cog.yaml --
build:
python_version: "3.12"
predict: "predict.py:Predictor"
8 changes: 6 additions & 2 deletions pkg/cli/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ func printDoctorResults(result *doctor.Result, fix bool, hasFixableErrors bool)
}
console.Infof(" %s%s", location, f.Message)

if fix && !cr.Fixed && f.Remediation != "" {
console.Infof(" (no auto-fix available)")
if fix && !cr.Fixed {
if f.Remediation != "" {
console.Infof(" Remediation: %s", f.Remediation)
} else {
console.Infof(" (no auto-fix available)")
}
}
}
}
Expand Down
98 changes: 98 additions & 0 deletions pkg/cli/doctor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cli

import (
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/replicate/cog/pkg/doctor"
"github.com/replicate/cog/pkg/util/console"
)

func TestPrintDoctorResultsFixShowsRemediationForUnfixedFinding(t *testing.T) {
result := &doctor.Result{
Results: []doctor.CheckResult{
{
Check: unfixedDoctorCheck{},
Findings: []doctor.Finding{
{
Severity: doctor.SeverityError,
Message: "predict.py not found",
Remediation: `Create predict.py or update the predict field in cog.yaml`,
File: "cog.yaml",
Line: 3,
},
},
},
},
}

output := captureDoctorStderr(t, func() {
console.SetColor(false)
printDoctorResults(result, true, false)
})

require.Contains(t, output, "predict.py not found")
require.Contains(t, output, "Remediation: Create predict.py or update the predict field in cog.yaml")
require.NotContains(t, output, "(no auto-fix available)")
}

func TestPrintDoctorResultsFixShowsNoAutoFixWhenRemediationMissing(t *testing.T) {
result := &doctor.Result{
Results: []doctor.CheckResult{
{
Check: unfixedDoctorCheck{},
Findings: []doctor.Finding{
{
Severity: doctor.SeverityWarning,
Message: "something could not be fixed",
},
},
},
},
}

output := captureDoctorStderr(t, func() {
console.SetColor(false)
printDoctorResults(result, true, false)
})

require.Contains(t, output, "(no auto-fix available)")
}

type unfixedDoctorCheck struct{}

func (unfixedDoctorCheck) Name() string { return "unfixed-check" }
func (unfixedDoctorCheck) Group() doctor.Group { return doctor.GroupConfig }
func (unfixedDoctorCheck) Description() string { return "Unfixed check" }
func (unfixedDoctorCheck) Check(*doctor.CheckContext) ([]doctor.Finding, error) {
return nil, nil
}
func (unfixedDoctorCheck) Fix(*doctor.CheckContext, []doctor.Finding) error {
return doctor.ErrNoAutoFix
}

func captureDoctorStderr(t *testing.T, fn func()) string {
t.Helper()

originalStderr := os.Stderr
r, w, err := os.Pipe()
require.NoError(t, err)

os.Stderr = w
defer func() {
os.Stderr = originalStderr
}()

fn()

require.NoError(t, w.Close())
out, err := io.ReadAll(r)
require.NoError(t, err)
require.NoError(t, r.Close())

return strings.ReplaceAll(string(out), "\r\n", "\n")
}