Skip to content

Commit 8828999

Browse files
committed
Add in a help-check test pipeline similar to ver-check
1 parent 7087db1 commit 8828999

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed

help-check/help-check

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#!/bin/sh
2+
# shellcheck disable=SC2317,SC2166,SC3043,SC2162,SC2086
3+
set +x
4+
set -f
5+
info() { echo "INFO[help-check]:" "$@"; }
6+
error() { echo "ERROR[help-check]:" "$@"; exit 1; }
7+
fail() { echo "FAIL[help-check]:" "$@"; fails=$((fails+1)); }
8+
pass() { echo "PASS[help-check]:" "$@"; passes=$((passes+1)); }
9+
10+
show_help() {
11+
cat << EOF
12+
Usage: help-check [OPTIONS]
13+
14+
Tool to check help information for binaries.
15+
16+
Options:
17+
-h, --help Show this help message and exit
18+
--bins=BINARY, --bins BINARY Space-separated list of binaries to check
19+
--help-flag=FLAG, --help-flag FLAG
20+
Help flag to use (default: auto)
21+
--expect-contains=STR, --expect-contains STR
22+
Optional strings to verify in help output (space-separated)
23+
--verbose=BOOL, --verbose BOOL
24+
Enable verbose output (true or false, default: false)
25+
26+
Examples:
27+
help-check --bins="nginx"
28+
help-check --bins="gcc g++ cpp"
29+
help-check --bins="node" --help-flag="--help"
30+
help-check --bins="nginx" --expect-contains="Usage Options"
31+
EOF
32+
exit 0
33+
}
34+
35+
bins=""
36+
help_flag="auto"
37+
expect_contains=""
38+
VERBOSE=false
39+
40+
while [ $# -ne 0 ]; do
41+
case "$1" in
42+
-h|--help) show_help;;
43+
--bins=*) bins="${bins} ${1#*=}";;
44+
--bins) bins="${bins} $2"; shift;;
45+
--help-flag=*) help_flag="${1#*=}";;
46+
--help-flag) help_flag="$2"; shift;;
47+
--expect-contains=*) expect_contains="${1#*=}";;
48+
--expect-contains) expect_contains="$2"; shift;;
49+
--verbose=*) VERBOSE="${1#*=}";;
50+
--verbose) VERBOSE="$2"; shift;;
51+
--*) error "Unknown argument '$1'";;
52+
esac
53+
shift
54+
done
55+
56+
bins=${bins# }
57+
58+
case "$VERBOSE" in
59+
true|false) :;;
60+
*) error "--verbose must be 'true' or 'false'. found '$VERBOSE'";;
61+
esac
62+
63+
[ -n "$bins" ] || error "Must specify --bins"
64+
65+
export LANG=C
66+
67+
vmsg() {
68+
[ "$VERBOSE" = "false" ] || echo "$@"
69+
}
70+
71+
check_binary() {
72+
local binary="$1"
73+
local help_flag="$2"
74+
local expect_contains="$3"
75+
local output exit_code
76+
local help_candidates="--help -h -help help"
77+
78+
vmsg "Checking help for binary: $binary"
79+
80+
# Check if binary exists in PATH
81+
local fbinary=""
82+
if ! fbinary=$(command -v "$binary" 2>/dev/null); then
83+
fail "Binary $binary not found in PATH ($PATH)"
84+
return 1
85+
fi
86+
vmsg "Binary '$fbinary' found in PATH"
87+
88+
local binarymsg="'$binary'"
89+
if [ "$binary" != "$fbinary" ]; then
90+
binarymsg="'$binary [$fbinary]'"
91+
fi
92+
93+
# Auto-detect help flag if not specified
94+
local detected_flag="$help_flag"
95+
if [ "$help_flag" = "auto" ]; then
96+
vmsg "Auto-detecting help flag for $fbinary..."
97+
for flag in $help_candidates; do
98+
vmsg "Trying: $fbinary $flag"
99+
# Help flags may exit with 0 or 1, both are acceptable
100+
if output=$($binary $flag 2>&1); then
101+
exit_code=$?
102+
else
103+
exit_code=$?
104+
fi
105+
106+
# Accept exit code 0 or 1 for help output
107+
if [ $exit_code -eq 0 ] || [ $exit_code -eq 1 ]; then
108+
if [ -n "$output" ]; then
109+
detected_flag="$flag"
110+
vmsg "Success with: $flag (exit code: $exit_code)"
111+
break
112+
fi
113+
fi
114+
done
115+
116+
if [ "$detected_flag" = "auto" ]; then
117+
fail "Could not auto-detect help flag for '$binarymsg' (tried: $help_candidates)"
118+
return 1
119+
fi
120+
else
121+
vmsg "Using specified help flag: $help_flag"
122+
output=$($binary $detected_flag 2>&1) || true
123+
exit_code=$?
124+
125+
# Help flags commonly exit with 0 or 1
126+
if [ $exit_code -ne 0 ] && [ $exit_code -ne 1 ]; then
127+
fail "'$binarymsg $detected_flag' failed with unexpected exit code $exit_code"
128+
[ "$VERBOSE" = "true" ] && echo "$output" | sed 's/^/ /'
129+
return 1
130+
fi
131+
fi
132+
133+
if [ -z "$output" ]; then
134+
fail "'$binarymsg $detected_flag' produced no output"
135+
return 1
136+
fi
137+
138+
if [ "$VERBOSE" = "true" ]; then
139+
echo "> $ $binary $detected_flag"
140+
echo "$output" | sed 's/^/> /'
141+
fi
142+
143+
# Check for expected content if specified
144+
if [ -n "$expect_contains" ]; then
145+
local all_found=true
146+
for expected_str in $expect_contains; do
147+
if ! echo "$output" | grep -F "$expected_str" >/dev/null; then
148+
fail "Help output for $binarymsg missing expected string: '$expected_str'"
149+
[ "$VERBOSE" = "true" ] && echo "$output" | sed 's/^/ /'
150+
all_found=false
151+
fi
152+
done
153+
154+
if [ "$all_found" = "false" ]; then
155+
return 1
156+
fi
157+
fi
158+
159+
pass "$binarymsg help check"
160+
return 0
161+
}
162+
163+
fails=0
164+
passes=0
165+
166+
info "Starting help checks for: $bins"
167+
info "Help flag: $help_flag"
168+
[ -n "$expect_contains" ] && info "Expecting output to contain: $expect_contains"
169+
170+
for binary in $bins; do
171+
check_binary "$binary" "$help_flag" "$expect_contains"
172+
done
173+
174+
info "tested $((passes+fails)) binaries. $passes passes. $fails fails."
175+
176+
[ $fails -eq 0 ] || exit 1

pipelines/test/tw/help-check.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: help-check
2+
3+
needs:
4+
packages:
5+
- help-check
6+
7+
inputs:
8+
bins:
9+
description: |
10+
Space-separated list of binary names to check for help output.
11+
Can be a single binary or multiple binaries.
12+
Examples: "nginx" or "gcc g++ cpp" or "node npm npx"
13+
required: true
14+
help-flag:
15+
description: |
16+
Command line flag used to get help information.
17+
Use 'auto' to try common flags automatically: --help, -h, -help, help
18+
Or specify exact flag like: --help, -h, etc.
19+
required: false
20+
default: "auto"
21+
expect-contains:
22+
description: |
23+
Optional string(s) to verify are present in the help output.
24+
Can be used to ensure help text contains usage information, options, etc.
25+
Multiple strings can be space-separated (all must be found).
26+
Example: "Usage Options" or "Examples --version"
27+
required: false
28+
default: ""
29+
verbose:
30+
description: |
31+
Whether or not the output should be verbose
32+
required: false
33+
default: false
34+
35+
# USAGE EXAMPLES:
36+
#
37+
# Basic check (just ensure help flag works):
38+
# - uses: test/tw/help-check
39+
# with:
40+
# bins: ${{package.name}}
41+
#
42+
# Multiple binaries:
43+
# - uses: test/tw/help-check
44+
# with:
45+
# bins: "gcc g++ cpp"
46+
#
47+
# Verify help content contains specific strings:
48+
# - uses: test/tw/help-check
49+
# with:
50+
# bins: "nginx"
51+
# expect-contains: "Usage Options"
52+
pipeline:
53+
- name: "check help information for binaries"
54+
runs: |
55+
help-check \
56+
--bins="${{inputs.bins}}" \
57+
--help-flag="${{inputs.help-flag}}" \
58+
--expect-contains="${{inputs.expect-contains}}" \
59+
--verbose="${{inputs.verbose}}"

0 commit comments

Comments
 (0)