|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestCLIFlagsDocumented verifies that every registered CLI flag appears in |
| 17 | +// docs/cli-reference.md, and every documented flag corresponds to a registered flag. |
| 18 | +func TestCLIFlagsDocumented(t *testing.T) { |
| 19 | + // Resolve docs path from this test file's location. |
| 20 | + _, thisFile, _, ok := runtime.Caller(0) |
| 21 | + require.True(t, ok, "runtime.Caller(0) failed to retrieve file path") |
| 22 | + docPath := filepath.Join(filepath.Dir(thisFile), "..", "..", "..", "docs", "cli-reference.md") |
| 23 | + |
| 24 | + docBytes, err := os.ReadFile(docPath) |
| 25 | + require.NoError(t, err, "reading cli-reference.md") |
| 26 | + docContent := string(docBytes) |
| 27 | + |
| 28 | + // Build the map of command -> FlagSet by calling every Setup*Flags function. |
| 29 | + commands := map[string]*flag.FlagSet{ |
| 30 | + "validate": mustFS(SetupValidateFlags()), |
| 31 | + "parse": mustFS(SetupParseFlags()), |
| 32 | + "fix": mustFS(SetupFixFlags()), |
| 33 | + "convert": mustFS(SetupConvertFlags()), |
| 34 | + "diff": mustFS(SetupDiffFlags()), |
| 35 | + "join": mustFS(SetupJoinFlags()), |
| 36 | + "generate": mustFS(SetupGenerateFlags()), |
| 37 | + "overlay apply": mustFS(SetupOverlayApplyFlags()), |
| 38 | + "overlay validate": mustFS(SetupOverlayValidateFlags()), |
| 39 | + "walk operations": mustFS(SetupWalkOperationsFlags()), |
| 40 | + "walk schemas": mustFS(SetupWalkSchemasFlags()), |
| 41 | + "walk parameters": mustFS(SetupWalkParametersFlags()), |
| 42 | + "walk responses": mustFS(SetupWalkResponsesFlags()), |
| 43 | + "walk security": mustFS(SetupWalkSecurityFlags()), |
| 44 | + "walk paths": mustFS(SetupWalkPathsFlags()), |
| 45 | + } |
| 46 | + |
| 47 | + // Parse documented flags from markdown tables per command section. |
| 48 | + knownCmds := make(map[string]bool, len(commands)) |
| 49 | + for name := range commands { |
| 50 | + knownCmds[name] = true |
| 51 | + } |
| 52 | + docFlags := parseDocFlags(docContent, knownCmds) |
| 53 | + |
| 54 | + for cmdName, fs := range commands { |
| 55 | + t.Run(cmdName, func(t *testing.T) { |
| 56 | + documented := docFlags[cmdName] |
| 57 | + require.NotNil(t, documented, "no flag table found in cli-reference.md for command %q", cmdName) |
| 58 | + |
| 59 | + // Collect registered long-form flag names (skip single-char aliases and help). |
| 60 | + registeredSet := make(map[string]bool) |
| 61 | + fs.VisitAll(func(f *flag.Flag) { |
| 62 | + if len(f.Name) == 1 || f.Name == "help" { |
| 63 | + return |
| 64 | + } |
| 65 | + registeredSet[f.Name] = true |
| 66 | + }) |
| 67 | + |
| 68 | + // Check: every registered flag should be documented. |
| 69 | + for name := range registeredSet { |
| 70 | + assert.True(t, documented[name], "flag --%s is registered for %q but not documented in cli-reference.md", name, cmdName) |
| 71 | + } |
| 72 | + |
| 73 | + // Check: every documented flag should be registered. |
| 74 | + for name := range documented { |
| 75 | + if name == "help" { |
| 76 | + continue |
| 77 | + } |
| 78 | + assert.True(t, registeredSet[name], "flag --%s is documented for %q in cli-reference.md but not registered", name, cmdName) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// mustFS extracts the *flag.FlagSet from a Setup*Flags return pair. |
| 85 | +func mustFS[T any](fs *flag.FlagSet, _ T) *flag.FlagSet { |
| 86 | + return fs |
| 87 | +} |
| 88 | + |
| 89 | +// parseDocFlags parses cli-reference.md and returns a map of command name -> set of documented flag names. |
| 90 | +// It looks for markdown sections (## command / ### subcommand) and flag table rows (| `--flagname` |). |
| 91 | +// knownCmds is the authoritative set of command names to match against section headers. |
| 92 | +func parseDocFlags(content string, knownCmds map[string]bool) map[string]map[string]bool { |
| 93 | + result := make(map[string]map[string]bool) |
| 94 | + |
| 95 | + // Split into lines for section tracking. |
| 96 | + lines := strings.Split(content, "\n") |
| 97 | + |
| 98 | + // allFlagsRe matches all --flag-name occurrences in a table row. |
| 99 | + // This handles rows like: |
| 100 | + // | `--flag-name` | description | |
| 101 | + // | `-s, --flag-name` | description | |
| 102 | + // | `--prune-all, --prune` | description | |
| 103 | + // | `-o, --flag-name string` | description | |
| 104 | + allFlagsRe := regexp.MustCompile(`--([a-zA-Z][a-zA-Z0-9-]*)`) |
| 105 | + |
| 106 | + var currentCmd string |
| 107 | + |
| 108 | + for _, line := range lines { |
| 109 | + trimmed := strings.TrimSpace(line) |
| 110 | + |
| 111 | + // Track current section from ## and ### headers. |
| 112 | + // - ## headers are top-level commands: set currentCmd if recognized, reset otherwise |
| 113 | + // - ### headers are subcommands (overlay apply, walk operations, etc.): set if recognized, keep otherwise |
| 114 | + // - #### headers are sub-subsections (Flags, Examples): always ignore |
| 115 | + if strings.HasPrefix(trimmed, "## ") || strings.HasPrefix(trimmed, "### ") { |
| 116 | + var headerText string |
| 117 | + switch { |
| 118 | + case strings.HasPrefix(trimmed, "### "): |
| 119 | + headerText = strings.TrimPrefix(trimmed, "### ") |
| 120 | + case strings.HasPrefix(trimmed, "## "): |
| 121 | + headerText = strings.TrimPrefix(trimmed, "## ") |
| 122 | + } |
| 123 | + headerText = strings.TrimSpace(headerText) |
| 124 | + headerLower := strings.ToLower(headerText) |
| 125 | + |
| 126 | + if knownCmds[headerLower] { |
| 127 | + currentCmd = headerLower |
| 128 | + if result[currentCmd] == nil { |
| 129 | + result[currentCmd] = make(map[string]bool) |
| 130 | + } |
| 131 | + } else if strings.HasPrefix(trimmed, "## ") { |
| 132 | + // Reset currentCmd on unrecognized ## headers to avoid |
| 133 | + // attributing flags from unrelated sections to the previous command. |
| 134 | + currentCmd = "" |
| 135 | + } |
| 136 | + // For unrecognized ### headers (e.g., "### Flags", "### Examples"), |
| 137 | + // keep currentCmd as-is — they are subsections of the current command. |
| 138 | + } |
| 139 | + |
| 140 | + // Extract flags from the first cell of table rows. |
| 141 | + if currentCmd != "" && strings.HasPrefix(trimmed, "|") { |
| 142 | + // Extract the first table cell (between the first and second |). |
| 143 | + parts := strings.SplitN(trimmed, "|", 3) |
| 144 | + if len(parts) >= 3 { |
| 145 | + firstCell := parts[1] |
| 146 | + for _, matches := range allFlagsRe.FindAllStringSubmatch(firstCell, -1) { |
| 147 | + if len(matches) > 1 { |
| 148 | + result[currentCmd][matches[1]] = true |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return result |
| 156 | +} |
0 commit comments