TestDocHeadingStructure verifies the heading hierarchy of every generated markdown page: - exactly one H1 (the command name), and it is the first heading - cobra's H3 sections (Synopsis, Options, SEE ALSO) were promoted to H2 - cobra's all-caps ## SEE ALSO heading was normalized to ## See Also - no
(t *testing.T)
| 67 | // - cobra's all-caps ## SEE ALSO heading was normalized to ## See Also |
| 68 | // - no duplicate ## See Also sections |
| 69 | func TestDocHeadingStructure(t *testing.T) { |
| 70 | tmpDir := generateDocs(t) |
| 71 | |
| 72 | entries, err := os.ReadDir(tmpDir) |
| 73 | require.NoError(t, err) |
| 74 | |
| 75 | for _, e := range entries { |
| 76 | if e.IsDir() || !strings.HasSuffix(e.Name(), ".md") { |
| 77 | continue |
| 78 | } |
| 79 | data, err := os.ReadFile(filepath.Join(tmpDir, e.Name())) |
| 80 | require.NoError(t, err) |
| 81 | content := string(data) |
| 82 | |
| 83 | t.Run(e.Name(), func(t *testing.T) { |
| 84 | // Strip fenced code blocks so bash comments (# ...) inside |
| 85 | // Examples sections are not mistaken for markdown headings. |
| 86 | stripped := fencedCodeBlock.ReplaceAllString(content, "") |
| 87 | |
| 88 | // Every page must have exactly one H1 heading. |
| 89 | h1Count := strings.Count(stripped, "\n# ") |
| 90 | require.Equal(t, 1, h1Count, "expected exactly one H1 heading") |
| 91 | |
| 92 | // The first heading in the document body must be H1, not H2 or H3. |
| 93 | // (frontmatter ends with "---\n", so the next heading follows a newline) |
| 94 | afterFrontmatter := stripped[strings.Index(stripped, "---\n")+4:] |
| 95 | firstHeadingIdx := strings.Index(afterFrontmatter, "\n#") |
| 96 | require.NotEqual(t, -1, firstHeadingIdx, "no heading found after frontmatter") |
| 97 | firstHeading := afterFrontmatter[firstHeadingIdx+1:] // skip the leading \n |
| 98 | require.True(t, strings.HasPrefix(firstHeading, "# "), |
| 99 | "first heading must be H1, got: %q", strings.SplitN(firstHeading, "\n", 2)[0]) |
| 100 | |
| 101 | // cobra's H3 sections must have been promoted to H2. Verify none of |
| 102 | // the known cobra section names remain at H3 level. |
| 103 | for _, section := range []string{"Synopsis", "Options", "Examples", "SEE ALSO", "See Also"} { |
| 104 | require.NotContains(t, stripped, "\n### "+section, |
| 105 | "cobra section %q was not promoted from H3 to H2", section) |
| 106 | } |
| 107 | |
| 108 | // No all-caps SEE ALSO heading should remain anywhere. |
| 109 | require.NotContains(t, content, "## SEE ALSO", |
| 110 | "cobra's all-caps SEE ALSO should be normalized to ## See Also") |
| 111 | |
| 112 | // There must be at most one ## See Also section (no duplicate). |
| 113 | seeAlsoCount := strings.Count(stripped, "\n## See Also\n") |
| 114 | require.LessOrEqual(t, seeAlsoCount, 1, "duplicate ## See Also sections found") |
| 115 | }) |
| 116 | } |
| 117 | } |
nothing calls this directly
no test coverage detected