From 983ca4b0f6f77bb46cc77800122a072a2c473252 Mon Sep 17 00:00:00 2001 From: "user.email" Date: Fri, 1 May 2026 12:57:56 +0100 Subject: [PATCH] chore(docs): convert catalog_test + generate_test + integration_test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- go/pkg/help/catalog_test.go | 25 ++++++------ go/pkg/help/generate_test.go | 68 +++++++++++++++++---------------- go/pkg/help/integration_test.go | 54 +++++++++++++------------- 3 files changed, 72 insertions(+), 75 deletions(-) diff --git a/go/pkg/help/catalog_test.go b/go/pkg/help/catalog_test.go index 2fa51d2..667c81c 100644 --- a/go/pkg/help/catalog_test.go +++ b/go/pkg/help/catalog_test.go @@ -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.", }, diff --git a/go/pkg/help/generate_test.go b/go/pkg/help/generate_test.go index b6ce1bc..30eaaa5 100644 --- a/go/pkg/help/generate_test.go +++ b/go/pkg/help/generate_test.go @@ -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, "guide") } @@ -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, "