| 1 | package docsite |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // root is the real corpus, relative to this package. Every gate below |
| 6 | // runs against it. |
| 7 | const root = "../../docs/site" |
| 8 | |
| 9 | // TestNavAndFilesAgreeBothWays keeps the table of contents honest in |
| 10 | // both directions. A page nav.json names must exist — Load fails on |
| 11 | // that one — and a page on disk that no nav entry reaches must not. An |
| 12 | // unreachable page is the worse failure: it looks written, and nobody |
| 13 | // ever reads it. |
| 14 | func TestNavAndFilesAgreeBothWays(t *testing.T) { |
| 15 | site, err := Load(root) |
| 16 | if err != nil { |
| 17 | t.Fatalf("load: %v", err) |
| 18 | } |
| 19 | |
| 20 | seen := map[string]bool{} |
| 21 | for _, p := range site.Pages() { |
| 22 | if seen[p.Slug] { |
| 23 | t.Errorf("%s: appears in nav.json more than once", p.Slug) |
| 24 | } |
| 25 | seen[p.Slug] = true |
| 26 | |
| 27 | if p.Title == "" { |
| 28 | t.Errorf("%s: no `# ` title", p.Slug) |
| 29 | } |
| 30 | if p != site.Index && p.Blurb == "" { |
| 31 | t.Errorf("%s: no blurb in nav.json (it renders on the /docs index)", p.Slug) |
| 32 | } |
| 33 | if p != site.Index && p.Label == "" { |
| 34 | t.Errorf("%s: no label in nav.json", p.Slug) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | files, err := MarkdownFiles(root) |
| 39 | if err != nil { |
| 40 | t.Fatalf("walk %s: %v", root, err) |
| 41 | } |
| 42 | for _, slug := range files { |
| 43 | if !seen[slug] { |
| 44 | t.Errorf("docs/site/%s.md exists but no nav.json entry reaches it", slug) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // TestOrphanPagesAreCaught proves the gate above can actually fail. The |
| 50 | // check that never fires is the check nobody notices is broken. |
| 51 | func TestOrphanPagesAreCaught(t *testing.T) { |
| 52 | site, err := Load("testdata/orphan") |
| 53 | if err != nil { |
| 54 | t.Fatalf("load: %v", err) |
| 55 | } |
| 56 | seen := map[string]bool{} |
| 57 | for _, p := range site.Pages() { |
| 58 | seen[p.Slug] = true |
| 59 | } |
| 60 | files, err := MarkdownFiles("testdata/orphan") |
| 61 | if err != nil { |
| 62 | t.Fatalf("walk: %v", err) |
| 63 | } |
| 64 | var orphans []string |
| 65 | for _, slug := range files { |
| 66 | if !seen[slug] { |
| 67 | orphans = append(orphans, slug) |
| 68 | } |
| 69 | } |
| 70 | if len(orphans) != 1 || orphans[0] != "stranded" { |
| 71 | t.Errorf("orphans = %v, want [stranded]", orphans) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // TestNavNamingAMissingPageFails is the other direction. |
| 76 | func TestNavNamingAMissingPageFails(t *testing.T) { |
| 77 | if _, err := Load("testdata/missing"); err == nil { |
| 78 | t.Fatal("Load succeeded on a nav.json naming a page that does not exist") |
| 79 | } |
| 80 | } |
| 81 | |