chore(docs): convert catalog_test + generate_test + integration_test
Drops `fmt` + `os` + `path/filepath` + `encoding/json` imports from all 3 test files. ZERO banned-imports remaining in docs. Conversions in catalog_test.go: - os.MkdirAll → MkdirAll (dot-imported Result-shape; bare statement, no error check, matches original Go behaviour) - os.WriteFile → WriteFile (1 site) - filepath.Join → PathJoin (~6 sites) - fmt.Sprintf → Sprintf (5 sites in benchmark) Conversions in generate_test.go: - Adds 2 test helpers: statExists(path) error + readFileBytes(t, path) → []byte. They wrap Stat + ReadFile Result-shape into the conventional test idiom (assertion-on-error / fail-on-error). - os.Stat + AssertNoError → AssertNoError(t, statExists(...)) (3 sites) - os.ReadFile + RequireNoError → readFileBytes(t, ...) (6 sites; collapses 4 lines per site into 1) - json.Unmarshal → JSONUnmarshal Result-shape with t.Fatal on !OK (2 sites) - filepath.Join → PathJoin Conversions in integration_test.go: - os.MkdirAll/WriteFile → MkdirAll/WriteFile Result-shape with t.Fatal on !OK (4 sites) - os.Stat + AssertNoError → statExists() helper (6 sites) - os.ReadFile + RequireNoError → readFileBytes() helper (3 sites) - json.Unmarshal → JSONUnmarshal Result-shape (1 site) - filepath.Join → PathJoin All docs tests pass. Banned-imports: 9 → 0 (-9, FULLY CLEARED) for docs. Refs Mantis #1316 Filed-by: hephaestus Co-authored-by: Hephaestus <hephaestus@lthn.ai>
This commit is contained in:
parent
4e41093582
commit
983ca4b0f6
3 changed files with 72 additions and 75 deletions
|
|
@ -2,9 +2,6 @@ package help
|
|||
|
||||
import (
|
||||
. "dappco.re/go"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func TestDefaultCatalog_Good(t *T) {
|
||||
|
|
@ -177,9 +174,9 @@ func TestCatalog_Search_Good_ScoreTiebreaking(t *T) {
|
|||
|
||||
func TestCatalog_LoadContentDir_Good(t *T) {
|
||||
dir := t.TempDir()
|
||||
os.MkdirAll(filepath.Join(dir, "cli"), 0o755)
|
||||
os.WriteFile(filepath.Join(dir, "cli", "dev-work.md"), []byte("---\ntitle: Dev Work\ntags: [cli, dev]\n---\n\n## Usage\n\ncore dev work syncs your workspace.\n"), 0o644)
|
||||
os.WriteFile(filepath.Join(dir, "cli", "setup.md"), []byte("---\ntitle: Setup\ntags: [cli]\n---\n\n## Installation\n\nRun core setup to get started.\n"), 0o644)
|
||||
MkdirAll(PathJoin(dir, "cli"), 0o755)
|
||||
WriteFile(PathJoin(dir, "cli", "dev-work.md"), []byte("---\ntitle: Dev Work\ntags: [cli, dev]\n---\n\n## Usage\n\ncore dev work syncs your workspace.\n"), 0o644)
|
||||
WriteFile(PathJoin(dir, "cli", "setup.md"), []byte("---\ntitle: Setup\ntags: [cli]\n---\n\n## Installation\n\nRun core setup to get started.\n"), 0o644)
|
||||
|
||||
catalog, err := LoadContentDir(dir)
|
||||
RequireNoError(t, err)
|
||||
|
|
@ -203,8 +200,8 @@ func TestCatalog_LoadContentDir_Good_Empty(t *T) {
|
|||
|
||||
func TestCatalog_LoadContentDir_Good_SkipsNonMd(t *T) {
|
||||
dir := t.TempDir()
|
||||
os.WriteFile(filepath.Join(dir, "readme.txt"), []byte("not markdown"), 0o644)
|
||||
os.WriteFile(filepath.Join(dir, "topic.md"), []byte("---\ntitle: Topic\n---\n\nContent here.\n"), 0o644)
|
||||
WriteFile(PathJoin(dir, "readme.txt"), []byte("not markdown"), 0o644)
|
||||
WriteFile(PathJoin(dir, "topic.md"), []byte("---\ntitle: Topic\n---\n\nContent here.\n"), 0o644)
|
||||
|
||||
catalog, err := LoadContentDir(dir)
|
||||
RequireNoError(t, err)
|
||||
|
|
@ -220,18 +217,18 @@ func BenchmarkSearch(b *B) {
|
|||
|
||||
for i := range 150 {
|
||||
c.Add(&Topic{
|
||||
ID: fmt.Sprintf("topic-%d", i),
|
||||
Title: fmt.Sprintf("Topic Number %d About Various Subjects", i),
|
||||
Content: fmt.Sprintf("This is the content of topic %d. It covers installation, configuration, deployment, and testing of the system.", i),
|
||||
Tags: []string{"generated", fmt.Sprintf("tag%d", i%10)},
|
||||
ID: Sprintf("topic-%d", i),
|
||||
Title: Sprintf("Topic Number %d About Various Subjects", i),
|
||||
Content: Sprintf("This is the content of topic %d. It covers installation, configuration, deployment, and testing of the system.", i),
|
||||
Tags: []string{"generated", Sprintf("tag%d", i%10)},
|
||||
Sections: []Section{
|
||||
{
|
||||
ID: fmt.Sprintf("section-%d-a", i),
|
||||
ID: Sprintf("section-%d-a", i),
|
||||
Title: "Overview",
|
||||
Content: "An overview of the topic and its purpose.",
|
||||
},
|
||||
{
|
||||
ID: fmt.Sprintf("section-%d-b", i),
|
||||
ID: Sprintf("section-%d-b", i),
|
||||
Title: "Details",
|
||||
Content: "Detailed information about the topic including examples and usage.",
|
||||
},
|
||||
|
|
|
|||
|
|
@ -3,11 +3,27 @@ package help
|
|||
|
||||
import (
|
||||
. "dappco.re/go"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// statExists is a small test helper that reports OK iff the path exists.
|
||||
func statExists(path string) error {
|
||||
r := Stat(path)
|
||||
if !r.OK {
|
||||
return r.Value.(error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readFileBytes returns the bytes at path or fails the test.
|
||||
func readFileBytes(t *T, path string) []byte {
|
||||
t.Helper()
|
||||
r := ReadFile(path)
|
||||
if !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
return r.Value.([]byte)
|
||||
}
|
||||
|
||||
// testCatalog builds a small catalog for generator tests.
|
||||
func testCatalog() *Catalog {
|
||||
c := &Catalog{
|
||||
|
|
@ -51,9 +67,8 @@ func TestGenerate_Good_FileStructure(t *T) {
|
|||
}
|
||||
|
||||
for _, f := range expectedFiles {
|
||||
path := filepath.Join(dir, f)
|
||||
_, err := os.Stat(path)
|
||||
AssertNoError(t, err, "expected generated file to exist: "+f)
|
||||
path := PathJoin(dir, f)
|
||||
AssertNoError(t, statExists(path), "expected generated file to exist: "+f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -64,10 +79,7 @@ func TestGenerate_Good_IndexContainsTopics(t *T) {
|
|||
err := Generate(catalog, dir)
|
||||
RequireNoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(dir, "index.html"))
|
||||
RequireNoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
html := string(readFileBytes(t, PathJoin(dir, "index.html")))
|
||||
AssertContains(t, html, "Getting Started")
|
||||
AssertContains(t, html, "Configuration")
|
||||
}
|
||||
|
|
@ -79,10 +91,7 @@ func TestGenerate_Good_TopicContainsRenderedMarkdown(t *T) {
|
|||
err := Generate(catalog, dir)
|
||||
RequireNoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(dir, "topics", "getting-started.html"))
|
||||
RequireNoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
html := string(readFileBytes(t, PathJoin(dir, "topics", "getting-started.html")))
|
||||
AssertContains(t, html, "Getting Started")
|
||||
AssertContains(t, html, "<strong>guide</strong>")
|
||||
}
|
||||
|
|
@ -94,11 +103,12 @@ func TestGenerate_Good_SearchIndexJSON(t *T) {
|
|||
err := Generate(catalog, dir)
|
||||
RequireNoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(dir, "search-index.json"))
|
||||
RequireNoError(t, err)
|
||||
content := readFileBytes(t, PathJoin(dir, "search-index.json"))
|
||||
|
||||
var entries []searchIndexEntry
|
||||
RequireNoError(t, json.Unmarshal(content, &entries))
|
||||
if r := JSONUnmarshal(content, &entries); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
AssertLen(t, entries, 2, "search index should contain all topics")
|
||||
|
||||
// Verify fields are populated
|
||||
|
|
@ -119,10 +129,7 @@ func TestGenerate_Good_404Exists(t *T) {
|
|||
err := Generate(catalog, dir)
|
||||
RequireNoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(dir, "404.html"))
|
||||
RequireNoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
html := string(readFileBytes(t, PathJoin(dir, "404.html")))
|
||||
AssertContains(t, html, "404")
|
||||
AssertContains(t, html, "Not Found")
|
||||
}
|
||||
|
|
@ -149,9 +156,7 @@ func TestGenerate_Good_OverwriteExisting(t *T) {
|
|||
AssertNoError(t, err)
|
||||
|
||||
// Verify files still exist and are valid
|
||||
content, err := os.ReadFile(filepath.Join(dir, "index.html"))
|
||||
RequireNoError(t, err)
|
||||
AssertContains(t, string(content), "Getting Started")
|
||||
AssertContains(t, string(readFileBytes(t, PathJoin(dir, "index.html"))), "Getting Started")
|
||||
}
|
||||
|
||||
func TestGenerate_Good_SearchPageHasScript(t *T) {
|
||||
|
|
@ -161,10 +166,7 @@ func TestGenerate_Good_SearchPageHasScript(t *T) {
|
|||
err := Generate(catalog, dir)
|
||||
RequireNoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(dir, "search.html"))
|
||||
RequireNoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
html := string(readFileBytes(t, PathJoin(dir, "search.html")))
|
||||
AssertContains(t, html, "<script>")
|
||||
AssertContains(t, html, "search-index.json")
|
||||
}
|
||||
|
|
@ -180,13 +182,13 @@ func TestGenerate_Good_EmptyCatalog(t *T) {
|
|||
RequireNoError(t, err)
|
||||
|
||||
// index.html should still exist
|
||||
_, err = os.Stat(filepath.Join(dir, "index.html"))
|
||||
AssertNoError(t, err)
|
||||
AssertNoError(t, statExists(PathJoin(dir, "index.html")))
|
||||
|
||||
// search-index.json should be valid empty array
|
||||
content, err := os.ReadFile(filepath.Join(dir, "search-index.json"))
|
||||
RequireNoError(t, err)
|
||||
content := readFileBytes(t, PathJoin(dir, "search-index.json"))
|
||||
var entries []searchIndexEntry
|
||||
RequireNoError(t, json.Unmarshal(content, &entries))
|
||||
if r := JSONUnmarshal(content, &entries); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
AssertEmpty(t, entries)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,19 @@ package help
|
|||
|
||||
import (
|
||||
. "dappco.re/go"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func TestIntegration_Good_FullPipeline(t *T) {
|
||||
// 1. Create content directory
|
||||
contentDir := t.TempDir()
|
||||
RequireNoError(t, os.MkdirAll(filepath.Join(contentDir, "cli"), 0o755))
|
||||
RequireNoError(t, os.MkdirAll(filepath.Join(contentDir, "go"), 0o755))
|
||||
if r := MkdirAll(PathJoin(contentDir, "cli"), 0o755); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
if r := MkdirAll(PathJoin(contentDir, "go"), 0o755); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
|
||||
RequireNoError(t, os.WriteFile(filepath.Join(contentDir, "cli", "dev-work.md"), []byte(`---
|
||||
if r := WriteFile(PathJoin(contentDir, "cli", "dev-work.md"), []byte(`---
|
||||
title: Dev Work
|
||||
tags: [cli]
|
||||
order: 1
|
||||
|
|
@ -27,9 +28,11 @@ core dev work syncs your workspace.
|
|||
## Flags
|
||||
|
||||
--status Show status only
|
||||
`), 0o644))
|
||||
`), 0o644); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
|
||||
RequireNoError(t, os.WriteFile(filepath.Join(contentDir, "go", "go-scm.md"), []byte(`---
|
||||
if r := WriteFile(PathJoin(contentDir, "go", "go-scm.md"), []byte(`---
|
||||
title: Go SCM
|
||||
tags: [go, library]
|
||||
order: 2
|
||||
|
|
@ -38,7 +41,9 @@ order: 2
|
|||
## Overview
|
||||
|
||||
Registry and git operations for the workspace.
|
||||
`), 0o644))
|
||||
`), 0o644); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
|
||||
// 2. Load into catalog
|
||||
catalog, err := LoadContentDir(contentDir)
|
||||
|
|
@ -51,37 +56,30 @@ Registry and git operations for the workspace.
|
|||
RequireNoError(t, err)
|
||||
|
||||
// 4. Verify file structure
|
||||
_, err = os.Stat(filepath.Join(outputDir, "index.html"))
|
||||
AssertNoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(outputDir, "search.html"))
|
||||
AssertNoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(outputDir, "search-index.json"))
|
||||
AssertNoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(outputDir, "404.html"))
|
||||
AssertNoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(outputDir, "topics", "dev-work.html"))
|
||||
AssertNoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(outputDir, "topics", "go-scm.html"))
|
||||
AssertNoError(t, err)
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "index.html")))
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "search.html")))
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "search-index.json")))
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "404.html")))
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "topics", "dev-work.html")))
|
||||
AssertNoError(t, statExists(PathJoin(outputDir, "topics", "go-scm.html")))
|
||||
|
||||
// 5. Verify search index
|
||||
indexData, err := os.ReadFile(filepath.Join(outputDir, "search-index.json"))
|
||||
RequireNoError(t, err)
|
||||
indexData := readFileBytes(t, PathJoin(outputDir, "search-index.json"))
|
||||
var entries []searchIndexEntry
|
||||
RequireNoError(t, json.Unmarshal(indexData, &entries))
|
||||
if r := JSONUnmarshal(indexData, &entries); !r.OK {
|
||||
t.Fatal(r.Error())
|
||||
}
|
||||
AssertLen(t, entries, 2)
|
||||
|
||||
// 6. Verify HTML content has HLCRF structure
|
||||
indexHTML, err := os.ReadFile(filepath.Join(outputDir, "index.html"))
|
||||
RequireNoError(t, err)
|
||||
indexHTML := readFileBytes(t, PathJoin(outputDir, "index.html"))
|
||||
AssertContains(t, string(indexHTML), "Dev Work")
|
||||
AssertContains(t, string(indexHTML), "Go SCM")
|
||||
AssertContains(t, string(indexHTML), `role="banner"`)
|
||||
AssertContains(t, string(indexHTML), `role="main"`)
|
||||
|
||||
// 7. Verify topic page has section anchors
|
||||
topicHTML, err := os.ReadFile(filepath.Join(outputDir, "topics", "dev-work.html"))
|
||||
RequireNoError(t, err)
|
||||
topicHTML := readFileBytes(t, PathJoin(outputDir, "topics", "dev-work.html"))
|
||||
AssertContains(t, string(topicHTML), `href="#usage"`)
|
||||
AssertContains(t, string(topicHTML), `href="#flags"`)
|
||||
AssertContains(t, string(topicHTML), `role="complementary"`)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue