manual: the shared "<program> manual" command (ADR-0012)
FromFS turns embedded README.md + docs/**/*.md into topics (README is "readme", titles from the first heading); Run serves "manual", "manual <topic>" and "manual --json [topic]" -- the JSON shape yet reads for an installed program's manual (yet manual --package, TUI, yet-gui), same as "yet manual --json". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e8bf23a064
commit
e757fdd025
3 files changed
+294
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package manual
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func testFS() fstest.MapFS {
|
||||
return fstest.MapFS{
|
||||
"README.md": {Data: []byte("# Tool\n\nIntro.\n")},
|
||||
"docs/configuration.md": {Data: []byte("# Configuration\n\nKeys.")},
|
||||
"docs/adr/0001-decision.md": {Data: []byte("no heading here")},
|
||||
"docs/image.png": {Data: []byte("x")},
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromFSAndJSON(t *testing.T) {
|
||||
m, err := FromFS("tool", testFS())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Topic{
|
||||
{Slug: "readme", Title: "Tool", Path: "README.md"},
|
||||
{Slug: "adr/0001-decision", Title: "adr/0001-decision", Path: "docs/adr/0001-decision.md"},
|
||||
{Slug: "configuration", Title: "Configuration", Path: "docs/configuration.md"},
|
||||
}
|
||||
if len(m.Topics) != len(want) {
|
||||
t.Fatalf("topics = %+v", m.Topics)
|
||||
}
|
||||
for i := range want {
|
||||
if m.Topics[i] != want[i] {
|
||||
t.Errorf("topic %d = %+v, want %+v", i, m.Topics[i], want[i])
|
||||
}
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
if err := m.Run([]string{"--json"}, &b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var list []Topic
|
||||
if err := json.Unmarshal([]byte(b.String()), &list); err != nil || len(list) != 3 {
|
||||
t.Fatalf("list json: %v %s", err, b.String())
|
||||
}
|
||||
|
||||
b.Reset()
|
||||
if err := m.Run([]string{"--json", "configuration"}, &b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var page struct {
|
||||
Topic
|
||||
Markdown string `json:"markdown"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(b.String()), &page); err != nil || page.Markdown != "# Configuration\n\nKeys." || page.Path != "docs/configuration.md" {
|
||||
t.Fatalf("page json: %v %+v", err, page)
|
||||
}
|
||||
|
||||
b.Reset()
|
||||
if err := m.Run([]string{"readme"}, &b); err != nil || b.String() != "# Tool\n\nIntro.\n" {
|
||||
t.Fatalf("plain page: %v %q", err, b.String())
|
||||
}
|
||||
b.Reset()
|
||||
if err := m.Run(nil, &b); err != nil || !strings.Contains(b.String(), "configuration") {
|
||||
t.Fatalf("plain list: %v %q", err, b.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunErrors(t *testing.T) {
|
||||
m := MustFromFS("tool", testFS())
|
||||
var b strings.Builder
|
||||
if err := m.Run([]string{"nope"}, &b); err == nil {
|
||||
t.Error("unknown topic should fail")
|
||||
}
|
||||
if err := m.Run([]string{"--bogus"}, &b); !errors.Is(err, ErrUsage) {
|
||||
t.Errorf("unknown flag: %v", err)
|
||||
}
|
||||
if err := m.Run([]string{"a", "b"}, &b); !errors.Is(err, ErrUsage) {
|
||||
t.Errorf("two topics: %v", err)
|
||||
}
|
||||
if _, err := FromFS("empty", fstestEmpty()); err == nil {
|
||||
t.Error("an empty manual should be an error")
|
||||
}
|
||||
}
|
||||
|
||||
func fstestEmpty() fstest.MapFS { return fstest.MapFS{"x.txt": {Data: []byte("x")}} }
|
||||
Reference in new issue
Block a user