[PATCH 5/9] t/t0450: split whitespace consistency check per subcommand
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- Oct 10, 2025, 10:27 UTC
- Message-ID
- <20251010-562-add-option-to-check-if-reference-backend-needs-repacking-v1-5-c7962be584fa@gmail.com>
- In-Reply-To
- <20251010-562-add-option-to-check-if-reference-backend-needs-repacking-v1-0-c7962be584fa@gmail.com>
The test t0450 contains a test to ensure that leading whitespaces within a commands help text is consistent. This would catch issues such as:
usage: git show-ref [--head] [-d | --dereference]
[-s | --hash[=<n>]] [--abbrev[=<n>]] [--branches] [--tags]
[--] [<pattern>...]where the second line has inconsistent leading whitespaces. However this considers that all lines within a command will be aligned similarly. This works for most commands, however when dealing commands which include subcommands, this assumption doesn't hold. Consider the help text for 'git-refs(1)':
usage: git refs migrate --ref-format=<format> [--no-reflog] [--dry-run]
or: git refs verify [--strict] [--verbose]
or: git refs list [--count=<count>] [--shell|--perl|--python|--tcl]
[(--sort=<key>)...] [--format=<format>]
[--include-root-refs] [--points-at=<object>]
[--merged[=<object>]] [--no-merged[=<object>]]
[--contains[=<object>]] [--no-contains[=<object>]]
[(--exclude=<pattern>)...] [--start-after=<marker>]
[ --stdin | (<pattern>...)]
or: git refs exists <ref>
or: git refs optimize [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]With the current implementation of this test, any flags added to 'git refs optimize' in a newline would require it to be aligned with the flags of 'git refs list'. Which is incorrect, since we'd want it to be aligned with the flags already added to 'git refs optimize'.
Let's modify the test to work with subcommands. Do this by swapping out the old logic. The old logic simply counts the number of spaces for all lines with leading spaces and checks to make sure they're equal. Instead, now we create a list of (subcommand number, leading space) and then ensure that there are only unique values per subcommand.
Signed-off-by: Karthik Nayak <karthik.188@gmail.com> --- t/t0450-txt-doc-vs-help.sh | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/t/t0450-txt-doc-vs-help.sh b/t/t0450-txt-doc-vs-help.sh index e12e18f97f..150655f9f0 100755 --- a/t/t0450-txt-doc-vs-help.sh +++ b/t/t0450-txt-doc-vs-help.sh @@ -94,19 +94,23 @@ do check_dashed_labels "$(help_to_synopsis "$builtin")" ' - test_expect_success "$builtin -h output has consistent spacing" ' + test_expect_success "$builtin -h output has consistent spacing for each subcommand" ' h2s="$(help_to_synopsis "$builtin")" && - sed -n \ - -e "/^ / { - s/[^ ].*//; - p; - }" \ - <"$h2s" >help && - sort -u help >help.ws && - if test -s help.ws - then - test_line_count = 1 help.ws - fi + + # For each subcommand, capture the number of whitespaces + # specific to that subcommand. + awk " + /^[^ ]/ { subcommand++ } # Count the number of subcommands + /^ / { + match(\$0, /^ */); + print subcommand, RLENGTH; + } + " <"$h2s" \ + | sort -u \ + | cut -d" " -f1 \ + | uniq -d > help.inconsistent && + + test_must_be_empty help.inconsistent ' adoc="$(builtin_to_adoc "$builtin")" &&
-- 2.51.0