Check gets Area and Hint, omitted from the JSON when empty; the table only grows an AREA or HINT column when some check uses one, so existing reports are unchanged. Report.Add records a check with all fields. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package doctor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"yetano.dev/yetano/core"
|
|
)
|
|
|
|
func TestRun(t *testing.T) {
|
|
dir := t.TempDir()
|
|
r := Report{Program: "tool"}
|
|
r.AddPath("data dir", dir, nil)
|
|
r.AddPath("missing", filepath.Join(dir, "nope"), nil)
|
|
r.AddPath("broken", "", errors.New("no home"))
|
|
r.Check("config valid", true, "")
|
|
|
|
var out, errOut strings.Builder
|
|
if code := r.Run([]string{"--json"}, &out, &errOut); code != ExitOK {
|
|
t.Fatalf("exit %d: %s", code, errOut.String())
|
|
}
|
|
var got Report
|
|
if err := json.Unmarshal([]byte(out.String()), &got); err != nil || len(got.Paths) != 3 || !got.Paths[0].Exists || got.Paths[1].Exists || got.Paths[2].Error != "no home" {
|
|
t.Fatalf("json: %v %+v", err, got)
|
|
}
|
|
|
|
r.Check("listener port free", false, "port 514 in use")
|
|
out.Reset()
|
|
if code := r.Run(nil, &out, &errOut); code != ExitProblems || !strings.Contains(out.String(), "port 514 in use") || !strings.Contains(errOut.String(), "1 problem") {
|
|
t.Fatalf("problems: %d %q %q", code, out.String(), errOut.String())
|
|
}
|
|
if code := r.Run([]string{"--bogus"}, &out, &errOut); code != ExitUsage {
|
|
t.Fatalf("usage: %d", code)
|
|
}
|
|
}
|
|
|
|
func TestAddPaths(t *testing.T) {
|
|
var r Report
|
|
r.AddPaths(core.Application{Name: "doctor-test", Kind: core.CLI})
|
|
if r.Scope != "user" || len(r.Paths) < 5 || r.Paths[0].Name != "config dir" {
|
|
t.Fatalf("paths: %+v", r)
|
|
}
|
|
for _, p := range r.Paths {
|
|
if p.Error == "" && p.Path == "" {
|
|
t.Errorf("%s: no path", p.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAreaAndHint(t *testing.T) {
|
|
// Without area/hint neither the JSON nor the table mentions them.
|
|
plain := Report{Program: "tool"}
|
|
plain.Check("config valid", true, "")
|
|
var out strings.Builder
|
|
if err := plain.Write(&out, true); err != nil || strings.Contains(out.String(), "area") || strings.Contains(out.String(), "hint") {
|
|
t.Fatalf("plain json: %v %s", err, out.String())
|
|
}
|
|
out.Reset()
|
|
if err := plain.Write(&out, false); err != nil || strings.Contains(out.String(), "AREA") || strings.Contains(out.String(), "HINT") {
|
|
t.Fatalf("plain table: %v %s", err, out.String())
|
|
}
|
|
|
|
r := Report{Program: "tool"}
|
|
r.Add(Check{Area: "config", Name: "config loads", OK: false, Note: "line 3", Hint: "run tool config validate"})
|
|
r.Check("other", true, "")
|
|
out.Reset()
|
|
if err := r.Write(&out, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got Report
|
|
if err := json.Unmarshal([]byte(out.String()), &got); err != nil || got.Checks[0].Area != "config" || got.Checks[0].Hint != "run tool config validate" || got.Checks[1].Area != "" {
|
|
t.Fatalf("json: %v %+v", err, got)
|
|
}
|
|
out.Reset()
|
|
if err := r.Write(&out, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lines := strings.Split(out.String(), "\n")
|
|
if !strings.HasPrefix(lines[0], "AREA") || !strings.Contains(lines[0], "HINT") || !strings.HasPrefix(lines[1], "config") || !strings.Contains(lines[1], "run tool config validate") {
|
|
t.Fatalf("table:\n%s", out.String())
|
|
}
|
|
}
|