help_uniform.sh (6783B)
1 #!/bin/sh 2 # Focused CLI help-shape and no-argument behavior checks. 3 4 set -u 5 6 script_dir=$(cd "$(dirname "$0")" && pwd) 7 repo_root=$(cd "$script_dir/../.." && pwd) 8 9 KIT="${KIT:-$repo_root/build/kit}" 10 11 if [ ! -x "$KIT" ]; then 12 echo "driver-help: kit binary not found at $KIT" >&2 13 exit 2 14 fi 15 16 work=$(mktemp -d "${TMPDIR:-/tmp}/kit-driver-help-test.XXXXXX") 17 trap 'rm -rf "$work"' EXIT 18 19 KIT_KIT_DIR="$repo_root/test/lib" 20 . "$repo_root/test/lib/kit_sh_kit.sh" 21 kit_report_init 22 23 # The top-level inventory stays complete, but is split into task-oriented 24 # groups so a user can scan it without already knowing a tool's name. 25 "$KIT" --help > "$work/top.help" 2> "$work/top.help.err" 26 awk ' 27 $0 == "TOOLS" { in_tools = 1; next } 28 $0 == "GETTING HELP" { exit } 29 in_tools && /^ [^ ]/ { 30 group = $0 31 sub(/^ /, "", group) 32 next 33 } 34 in_tools && /^ [^ ]/ { print group "|" $1 } 35 ' "$work/top.help" > "$work/top.groups" 36 cat > "$work/top.groups.expected" <<'EOF' 37 Compilation and build|cc 38 Compilation and build|check 39 Compilation and build|build-exe 40 Compilation and build|build-lib 41 Compilation and build|build-obj 42 Compilation and build|build 43 Compilation and build|make 44 Compilation and build|cpp 45 Compilation and build|as 46 Compilation and build|ld 47 Compilation and build|gram 48 Binary and object tools|ar 49 Binary and object tools|cpio 50 Binary and object tools|ranlib 51 Binary and object tools|strip 52 Binary and object tools|objcopy 53 Binary and object tools|image 54 Binary and object tools|objdump 55 Binary and object tools|nm 56 Binary and object tools|size 57 Binary and object tools|addr2line 58 Binary and object tools|symbolize 59 Binary and object tools|strings 60 Binary and object tools|disas 61 Binary and object tools|mc 62 Execution and debugging|run 63 Execution and debugging|dbg 64 Distribution|cas 65 Distribution|pkg 66 Data utilities|xxd 67 Data utilities|cmp 68 Data utilities|hash 69 Data utilities|sha256sum 70 Data utilities|b2sum 71 Data utilities|crc32 72 Data utilities|compress 73 Data utilities|gzip 74 Data utilities|gunzip 75 Data utilities|lz4 76 Data utilities|lz4c 77 Kit management|install 78 Kit management|targets 79 Kit management|version 80 Kit management|update 81 EOF 82 if cmp -s "$work/top.groups.expected" "$work/top.groups" && 83 [ ! -s "$work/top.help.err" ]; then 84 ok "top-help-groups" 85 else 86 { 87 printf '%s\n' 'expected groups:' 88 sed 's/^/ /' "$work/top.groups.expected" 89 printf '%s\n' 'actual groups:' 90 sed 's/^/ /' "$work/top.groups" 91 sed 's/^/stderr: /' "$work/top.help.err" 92 } > "$work/top.groups.diag" 93 not_ok "top-help-groups" "$work/top.groups.diag" 94 fi 95 96 # Examples assume `kit` is already on PATH. Keep extraction-layout variables, 97 # direct bin/kit paths, and explicit sibling support paths out of this section. 98 : > "$work/example-path-noise.diag" 99 example_tools=$(awk -F '|' '{ print $2 }' "$work/top.groups") 100 for tool in $example_tools emu; do 101 example_help="$work/$tool.examples.help" 102 example_err="$work/$tool.examples.err" 103 example_body="$work/$tool.examples" 104 if ! "$KIT" "$tool" --help > "$example_help" 2> "$example_err"; then 105 printf '%s: help failed\n' "$tool" >> "$work/example-path-noise.diag" 106 sed 's/^/ /' "$example_err" >> "$work/example-path-noise.diag" 107 continue 108 fi 109 awk ' 110 $0 == "EXAMPLES" { in_examples = 1; next } 111 in_examples && /^[A-Z][A-Z ]*$/ { exit } 112 in_examples { print } 113 ' "$example_help" > "$example_body" 114 example_noise="$work/$tool.examples.noise" 115 if grep -En 'DIST|/bin/kit|/path/to/kit|--support-dir|(^|[[:space:]])K=' \ 116 "$example_body" > "$example_noise"; then 117 sed "s/^/$tool:/" "$example_noise" >> "$work/example-path-noise.diag" 118 fi 119 done 120 if [ ! -s "$work/example-path-noise.diag" ]; then 121 ok "examples-use-kit-from-path" 122 else 123 not_ok "examples-use-kit-from-path" "$work/example-path-noise.diag" 124 fi 125 126 help_shape() { 127 tool=$1 128 out="$work/$tool.help" 129 err="$work/$tool.help.err" 130 if ! "$KIT" "$tool" --help > "$out" 2> "$err"; then 131 not_ok "help-shape-$tool" "$err" 132 return 133 fi 134 usage=$(awk '$0 == "USAGE" { print NR; exit }' "$out") 135 desc=$(awk '$0 == "DESCRIPTION" { print NR; exit }' "$out") 136 opts=$(awk '$0 == "OPTIONS" { print NR; exit }' "$out") 137 exits=$(awk '$0 == "EXIT CODES" { print NR; exit }' "$out") 138 if [ -n "$usage" ] && [ -n "$desc" ] && [ -n "$opts" ] && 139 [ -n "$exits" ] && [ "$usage" -lt "$desc" ] && 140 [ "$desc" -lt "$opts" ] && [ "$opts" -lt "$exits" ] && 141 [ ! -s "$err" ]; then 142 ok "help-shape-$tool" 143 else 144 { 145 printf 'USAGE=%s DESCRIPTION=%s OPTIONS=%s EXIT_CODES=%s\n' \ 146 "$usage" "$desc" "$opts" "$exits" 147 sed 's/^/stdout: /' "$out" 148 sed 's/^/stderr: /' "$err" 149 } > "$work/$tool.help.diag" 150 not_ok "help-shape-$tool" "$work/$tool.help.diag" 151 fi 152 } 153 154 for tool in cc check cas pkg build-exe build-lib nm size addr2line symbolize \ 155 strip objcopy; do 156 help_shape "$tool" 157 done 158 159 for tool in build install cpio pkg cmp; do 160 noargs_out="$work/$tool.noargs.out" 161 noargs_err="$work/$tool.noargs.err" 162 help_out="$work/$tool.explicit-help.out" 163 help_err="$work/$tool.explicit-help.err" 164 if "$KIT" "$tool" > "$noargs_out" 2> "$noargs_err"; then 165 noargs_rc=0 166 else 167 noargs_rc=$? 168 fi 169 if "$KIT" "$tool" --help > "$help_out" 2> "$help_err"; then 170 help_rc=0 171 else 172 help_rc=$? 173 fi 174 if [ "$noargs_rc" -eq 0 ] && [ "$help_rc" -eq 0 ] && 175 cmp -s "$noargs_out" "$help_out" && [ ! -s "$noargs_err" ] && 176 [ ! -s "$help_err" ]; then 177 ok "noargs-help-$tool" 178 else 179 { 180 sed 's/^/noargs stdout: /' "$noargs_out" 181 sed 's/^/noargs stderr: /' "$noargs_err" 182 sed 's/^/help stdout: /' "$help_out" 183 sed 's/^/help stderr: /' "$help_err" 184 } > "$work/$tool.noargs.diag" 185 not_ok "noargs-help-$tool" "$work/$tool.noargs.diag" 186 fi 187 done 188 189 # A minimal synthetic hosted SDK proves that dbg accepts the same -lc/sysroot 190 # workflow as run and actually adds the sysroot's system include directory. 191 mkdir -p "$work/sysroot/usr/include" "$work/sysroot/usr/lib" 192 printf '%s\n' '#define KIT_DBG_SYSROOT_VALUE 42' \ 193 > "$work/sysroot/usr/include/kit_dbg_sysroot_test.h" 194 : > "$work/sysroot/usr/lib/libc.so" 195 cat > "$work/dbg-sysroot.c" <<'SRC' 196 #include <kit_dbg_sysroot_test.h> 197 int main(void) { return KIT_DBG_SYSROOT_VALUE == 42 ? 0 : 1; } 198 SRC 199 if "$KIT" dbg -lc --sysroot "$work/sysroot" --batch --command q \ 200 "$work/dbg-sysroot.c" > "$work/dbg-sysroot.out" \ 201 2> "$work/dbg-sysroot.err"; then 202 ok "dbg-sysroot-hosted-headers" 203 else 204 not_ok "dbg-sysroot-hosted-headers" "$work/dbg-sysroot.err" 205 fi 206 207 kit_summary driver-help 208 kit_exit