package docsite import "testing" // root is the real corpus, relative to this package. Every gate below // runs against it. const root = "../../docs/site" // TestNavAndFilesAgreeBothWays keeps the table of contents honest in // both directions. A page nav.json names must exist — Load fails on // that one — and a page on disk that no nav entry reaches must not. An // unreachable page is the worse failure: it looks written, and nobody // ever reads it. func TestNavAndFilesAgreeBothWays(t *testing.T) { site, err := Load(root) if err != nil { t.Fatalf("load: %v", err) } seen := map[string]bool{} for _, p := range site.Pages() { if seen[p.Slug] { t.Errorf("%s: appears in nav.json more than once", p.Slug) } seen[p.Slug] = true if p.Title == "" { t.Errorf("%s: no `# ` title", p.Slug) } if p != site.Index && p.Blurb == "" { t.Errorf("%s: no blurb in nav.json (it renders on the /docs index)", p.Slug) } if p != site.Index && p.Label == "" { t.Errorf("%s: no label in nav.json", p.Slug) } } files, err := MarkdownFiles(root) if err != nil { t.Fatalf("walk %s: %v", root, err) } for _, slug := range files { if !seen[slug] { t.Errorf("docs/site/%s.md exists but no nav.json entry reaches it", slug) } } } // TestOrphanPagesAreCaught proves the gate above can actually fail. The // check that never fires is the check nobody notices is broken. func TestOrphanPagesAreCaught(t *testing.T) { site, err := Load("testdata/orphan") if err != nil { t.Fatalf("load: %v", err) } seen := map[string]bool{} for _, p := range site.Pages() { seen[p.Slug] = true } files, err := MarkdownFiles("testdata/orphan") if err != nil { t.Fatalf("walk: %v", err) } var orphans []string for _, slug := range files { if !seen[slug] { orphans = append(orphans, slug) } } if len(orphans) != 1 || orphans[0] != "stranded" { t.Errorf("orphans = %v, want [stranded]", orphans) } } // TestNavNamingAMissingPageFails is the other direction. func TestNavNamingAMissingPageFails(t *testing.T) { if _, err := Load("testdata/missing"); err == nil { t.Fatal("Load succeeded on a nav.json naming a page that does not exist") } }