| 1 |
#!/bin/sh |
| 2 |
# clitest - Tester for Unix command lines |
| 3 |
# |
| 4 |
# Author: Aurelio Jargas (http://aurelio.net) |
| 5 |
# Created: 2013-07-24 |
| 6 |
# License: MIT |
| 7 |
# GitHub: https://github.com/aureliojargas/clitest |
| 8 |
# |
| 9 |
# POSIX shell script: |
| 10 |
# This script is carefully coded to be compatible with POSIX shells. |
| 11 |
# It is currently tested in bash, dash, ksh, zsh and busybox's sh. |
| 12 |
# |
| 13 |
# Exit codes: |
| 14 |
# 0 All tests passed, or normal operation (--help, --list, ...) |
| 15 |
# 1 One or more tests have failed |
| 16 |
# 2 An error occurred (file not found, invalid range, ...) |
| 17 |
# |
| 18 |
# Test environment: |
| 19 |
# By default, the tests will run in the current working directory ($PWD). |
| 20 |
# You can change to another dir normally using 'cd' inside the test file. |
| 21 |
# All the tests are executed in the same shell session, using eval. Test |
| 22 |
# data such as variables and working directory will persist between tests. |
| 23 |
# |
| 24 |
# Namespace: |
| 25 |
# All variables and functions in this script are prefixed by 'tt_' to |
| 26 |
# avoid clashing with test's variables, functions, aliases and commands. |
| 27 |
|
| 28 |
tt_my_name="$(basename "$0")" |
| 29 |
tt_my_url='https://github.com/aureliojargas/clitest' |
| 30 |
tt_my_version='0.5.0' |
| 31 |
|
| 32 |
# Customization (if needed, edit here or use the command line options) |
| 33 |
tt_prefix='' |
| 34 |
tt_prompt='$ ' |
| 35 |
tt_inline_prefix='#=> ' |
| 36 |
tt_diff_options='-u' |
| 37 |
tt_color_mode='auto' # auto, always, never |
| 38 |
tt_progress='test' # test, number, dot, none |
| 39 |
# End of customization |
| 40 |
|
| 41 |
# --help message, keep it simple, short and informative |
| 42 |
tt_my_help="\ |
| 43 |
Usage: $tt_my_name [options] <file ...> |
| 44 |
|
| 45 |
Options: |
| 46 |
-1, --first Stop execution upon first failed test |
| 47 |
-l, --list List all the tests (no execution) |
| 48 |
-L, --list-run List all the tests with OK/FAIL status |
| 49 |
-t, --test RANGE Run specific tests, by number (1,2,4-7) |
| 50 |
-s, --skip RANGE Skip specific tests, by number (1,2,4-7) |
| 51 |
--pre-flight COMMAND Execute command before running the first test |
| 52 |
--post-flight COMMAND Execute command after running the last test |
| 53 |
-q, --quiet Quiet operation, no output shown |
| 54 |
-V, --version Show program version and exit |
| 55 |
|
| 56 |
Customization options: |
| 57 |
-P, --progress TYPE Set progress indicator: test, number, dot, none |
| 58 |
--color WHEN Set when to use colors: auto, always, never |
| 59 |
--diff-options OPTIONS Set diff command options (default: '$tt_diff_options') |
| 60 |
--inline-prefix PREFIX Set inline output prefix (default: '$tt_inline_prefix') |
| 61 |
--prefix PREFIX Set command line prefix (default: '$tt_prefix') |
| 62 |
--prompt STRING Set prompt string (default: '$tt_prompt') |
| 63 |
|
| 64 |
See also: $tt_my_url" |
| 65 |
|
| 66 |
# Flags (0=off, 1=on), most can be altered by command line options |
| 67 |
tt_debug=0 |
| 68 |
tt_use_colors=0 |
| 69 |
tt_stop_on_first_fail=0 |
| 70 |
tt_separator_line_shown=0 |
| 71 |
|
| 72 |
# The output mode values are mutually exclusive |
| 73 |
tt_output_mode='normal' # normal, quiet, list, list-run |
| 74 |
|
| 75 |
# Globals (all variables are globals, for better portability) |
| 76 |
tt_nr_files=0 |
| 77 |
tt_nr_total_tests=0 |
| 78 |
tt_nr_total_fails=0 |
| 79 |
tt_nr_total_skips=0 |
| 80 |
tt_nr_file_tests=0 |
| 81 |
tt_nr_file_fails=0 |
| 82 |
tt_nr_file_skips=0 |
| 83 |
tt_nr_file_ok=0 |
| 84 |
tt_files_stats= |
| 85 |
tt_original_dir=$(pwd) |
| 86 |
tt_pre_command= |
| 87 |
tt_post_command= |
| 88 |
tt_run_range= |
| 89 |
tt_run_range_data= |
| 90 |
tt_skip_range= |
| 91 |
tt_skip_range_data= |
| 92 |
tt_failed_range= |
| 93 |
tt_temp_dir= |
| 94 |
tt_test_file= |
| 95 |
tt_input_line= |
| 96 |
tt_line_number=0 |
| 97 |
tt_test_number=0 |
| 98 |
tt_test_line_number=0 |
| 99 |
tt_test_command= |
| 100 |
tt_test_inline= |
| 101 |
tt_test_mode= |
| 102 |
tt_test_status=2 |
| 103 |
tt_test_output= |
| 104 |
tt_test_exit_code= |
| 105 |
tt_test_diff= |
| 106 |
tt_test_ok_text= |
| 107 |
tt_missing_nl=0 |
| 108 |
|
| 109 |
# Special handy chars |
| 110 |
tt_tab=' ' |
| 111 |
tt_nl=' |
| 112 |
' |
| 113 |
|
| 114 |
### Utilities |
| 115 |
|
| 116 |
# shellcheck disable=SC2317 # Function is reachable (invoked by a trap) |
| 117 |
tt_clean_up() { |
| 118 |
test -n "$tt_temp_dir" && rm -rf "$tt_temp_dir" |
| 119 |
} |
| 120 |
tt_message() { |
| 121 |
test "$tt_output_mode" = 'quiet' && return 0 |
| 122 |
test $tt_missing_nl -eq 1 && echo |
| 123 |
printf '%s\n' "$*" |
| 124 |
tt_separator_line_shown=0 |
| 125 |
tt_missing_nl=0 |
| 126 |
} |
| 127 |
tt_message_part() { # no line break |
| 128 |
test "$tt_output_mode" = 'quiet' && return 0 |
| 129 |
printf '%s' "$*" |
| 130 |
tt_separator_line_shown=0 |
| 131 |
tt_missing_nl=1 |
| 132 |
} |
| 133 |
tt_error() { |
| 134 |
test $tt_missing_nl -eq 1 && echo |
| 135 |
printf '%s\n' "$tt_my_name: Error: $1" >&2 |
| 136 |
exit 2 |
| 137 |
} |
| 138 |
tt_debug() { # $1=id, $2=contents |
| 139 |
test $tt_debug -ne 1 && return 0 |
| 140 |
if test INPUT_LINE = "$1"; then |
| 141 |
# Original input line is all cyan and preceded by separator line |
| 142 |
printf -- "${tt_color_cyan}%s${tt_color_off}\n" "$(tt_separator_line)" |
| 143 |
printf -- "${tt_color_cyan}-- %10s[%s]${tt_color_off}\n" "$1" "$2" |
| 144 |
else |
| 145 |
# Highlight tabs and the (last) inline prefix |
| 146 |
printf -- "${tt_color_cyan}-- %10s[${tt_color_off}%s${tt_color_cyan}]${tt_color_off}\n" "$1" "$2" | |
| 147 |
sed "/LINE_CMD/ s/\(.*\)\($tt_inline_prefix\)/\1${tt_color_red}\2${tt_color_off}/" | |
| 148 |
sed "s/$tt_tab/${tt_color_green}<tab>${tt_color_off}/g" |
| 149 |
fi |
| 150 |
} |
| 151 |
tt_separator_line() { |
| 152 |
printf "%${COLUMNS}s" ' ' | tr ' ' - |
| 153 |
} |
| 154 |
tt_list_test() { # $1=normal|list|ok|fail |
| 155 |
# Show the test command in normal mode, --list and --list-run |
| 156 |
case "$1" in |
| 157 |
normal | list) |
| 158 |
# Normal line, no color, no stamp (--list) |
| 159 |
tt_message "#${tt_test_number}${tt_tab}${tt_test_command}" |
| 160 |
;; |
| 161 |
ok) |
| 162 |
# Green line or OK stamp (--list-run) |
| 163 |
if test $tt_use_colors -eq 1; then |
| 164 |
tt_message "${tt_color_green}#${tt_test_number}${tt_tab}${tt_test_command}${tt_color_off}" |
| 165 |
else |
| 166 |
tt_message "#${tt_test_number}${tt_tab}OK${tt_tab}${tt_test_command}" |
| 167 |
fi |
| 168 |
;; |
| 169 |
fail) |
| 170 |
# Red line or FAIL stamp (--list-run) |
| 171 |
if test $tt_use_colors -eq 1; then |
| 172 |
tt_message "${tt_color_red}#${tt_test_number}${tt_tab}${tt_test_command}${tt_color_off}" |
| 173 |
else |
| 174 |
tt_message "#${tt_test_number}${tt_tab}FAIL${tt_tab}${tt_test_command}" |
| 175 |
fi |
| 176 |
;; |
| 177 |
esac |
| 178 |
} |
| 179 |
tt_parse_range() { # $1=range |
| 180 |
# Parse numeric ranges and output them in an expanded format |
| 181 |
# |
| 182 |
# Supported formats Expanded |
| 183 |
# ------------------------------------------------------ |
| 184 |
# Single: 1 :1: |
| 185 |
# List: 1,3,4,7 :1:3:4:7: |
| 186 |
# Range: 1-4 :1:2:3:4: |
| 187 |
# Mixed: 1,3,4-7,11,13-15 :1:3:4:5:6:7:11:13:14:15: |
| 188 |
# |
| 189 |
# Reverse ranges and repeated/unordered numbers are ok. |
| 190 |
# Later we will just grep for :number: in each test. |
| 191 |
|
| 192 |
case "$1" in |
| 193 |
0 | '') |
| 194 |
# No range, nothing to do |
| 195 |
return 0 |
| 196 |
;; |
| 197 |
*[!0-9,-]*) |
| 198 |
# Error: strange chars, not 0123456789,- |
| 199 |
return 1 |
| 200 |
;; |
| 201 |
esac |
| 202 |
|
| 203 |
# OK, all valid chars in range, let's parse them |
| 204 |
|
| 205 |
tt_part= |
| 206 |
tt_n1= |
| 207 |
tt_n2= |
| 208 |
tt_swap= |
| 209 |
tt_range_data=':' # :1:2:4:7: |
| 210 |
|
| 211 |
# Loop each component: a number or a range |
| 212 |
for tt_part in $(echo "$1" | tr , ' '); do |
| 213 |
# If there's an hyphen, it's a range |
| 214 |
case "$tt_part" in |
| 215 |
*-*) |
| 216 |
# Error: Invalid range format, must be: number-number |
| 217 |
echo "$tt_part" | grep '^[0-9][0-9]*-[0-9][0-9]*$' > /dev/null || return 1 |
| 218 |
|
| 219 |
tt_n1=${tt_part%-*} |
| 220 |
tt_n2=${tt_part#*-} |
| 221 |
|
| 222 |
# Negative range, let's just reverse it (5-1 => 1-5) |
| 223 |
if test "$tt_n1" -gt "$tt_n2"; then |
| 224 |
tt_swap=$tt_n1 |
| 225 |
tt_n1=$tt_n2 |
| 226 |
tt_n2=$tt_swap |
| 227 |
fi |
| 228 |
|
| 229 |
# Expand the range (1-4 => 1:2:3:4) |
| 230 |
tt_part=$tt_n1: |
| 231 |
while test "$tt_n1" -ne "$tt_n2"; do |
| 232 |
tt_n1=$((tt_n1 + 1)) |
| 233 |
tt_part=$tt_part$tt_n1: |
| 234 |
done |
| 235 |
tt_part=${tt_part%:} |
| 236 |
;; |
| 237 |
esac |
| 238 |
|
| 239 |
# Append the number or expanded range to the holder |
| 240 |
test "$tt_part" != 0 && tt_range_data=$tt_range_data$tt_part: |
| 241 |
done |
| 242 |
|
| 243 |
test "$tt_range_data" != ':' && echo "$tt_range_data" |
| 244 |
return 0 |
| 245 |
} |
| 246 |
tt_reset_test_data() { |
| 247 |
tt_test_command= |
| 248 |
tt_test_inline= |
| 249 |
tt_test_mode= |
| 250 |
tt_test_status=2 |
| 251 |
tt_test_output= |
| 252 |
tt_test_diff= |
| 253 |
tt_test_ok_text= |
| 254 |
} |
| 255 |
tt_run_test() { |
| 256 |
tt_test_number=$((tt_test_number + 1)) |
| 257 |
tt_nr_total_tests=$((tt_nr_total_tests + 1)) |
| 258 |
tt_nr_file_tests=$((tt_nr_file_tests + 1)) |
| 259 |
|
| 260 |
# Run range on: skip this test if it's not listed in $tt_run_range_data |
| 261 |
if test -n "$tt_run_range_data" && test "$tt_run_range_data" = "${tt_run_range_data#*:"$tt_test_number":}"; then |
| 262 |
tt_nr_total_skips=$((tt_nr_total_skips + 1)) |
| 263 |
tt_nr_file_skips=$((tt_nr_file_skips + 1)) |
| 264 |
tt_reset_test_data |
| 265 |
return 0 |
| 266 |
fi |
| 267 |
|
| 268 |
# Skip range on: skip this test if it's listed in $tt_skip_range_data |
| 269 |
# Note: --skip always wins over --test, regardless of order |
| 270 |
if test -n "$tt_skip_range_data" && test "$tt_skip_range_data" != "${tt_skip_range_data#*:"$tt_test_number":}"; then |
| 271 |
tt_nr_total_skips=$((tt_nr_total_skips + 1)) |
| 272 |
tt_nr_file_skips=$((tt_nr_file_skips + 1)) |
| 273 |
tt_reset_test_data |
| 274 |
return 0 |
| 275 |
fi |
| 276 |
|
| 277 |
case "$tt_output_mode" in |
| 278 |
normal) |
| 279 |
# Normal mode: show progress indicator |
| 280 |
case "$tt_progress" in |
| 281 |
test) |
| 282 |
tt_list_test normal |
| 283 |
;; |
| 284 |
number) |
| 285 |
tt_message_part "$tt_test_number " |
| 286 |
;; |
| 287 |
none) |
| 288 |
: |
| 289 |
;; |
| 290 |
*) |
| 291 |
tt_message_part "$tt_progress" |
| 292 |
;; |
| 293 |
esac |
| 294 |
;; |
| 295 |
list) |
| 296 |
# List mode: just show the command and return (no execution) |
| 297 |
tt_list_test list |
| 298 |
tt_reset_test_data |
| 299 |
return 0 |
| 300 |
;; |
| 301 |
esac |
| 302 |
|
| 303 |
tt_debug EVAL "$tt_test_command" |
| 304 |
|
| 305 |
# Execute the test command, saving output (STDOUT and STDERR) |
| 306 |
eval "$tt_test_command" > "$tt_test_output_file" 2>&1 < /dev/null |
| 307 |
tt_test_exit_code=$? |
| 308 |
|
| 309 |
tt_debug OUTPUT "$(cat "$tt_test_output_file")" |
| 310 |
|
| 311 |
# The command output matches the expected output? |
| 312 |
case $tt_test_mode in |
| 313 |
output) |
| 314 |
printf %s "$tt_test_ok_text" > "$tt_test_ok_file" |
| 315 |
# shellcheck disable=SC2086 # diff options should be split |
| 316 |
tt_test_diff=$(diff $tt_diff_options "$tt_test_ok_file" "$tt_test_output_file") |
| 317 |
tt_test_status=$? |
| 318 |
;; |
| 319 |
text) |
| 320 |
# Inline OK text represents a full line, with \n |
| 321 |
printf '%s\n' "$tt_test_inline" > "$tt_test_ok_file" |
| 322 |
# shellcheck disable=SC2086 # diff options should be split |
| 323 |
tt_test_diff=$(diff $tt_diff_options "$tt_test_ok_file" "$tt_test_output_file") |
| 324 |
tt_test_status=$? |
| 325 |
;; |
| 326 |
eval) |
| 327 |
eval "$tt_test_inline" > "$tt_test_ok_file" |
| 328 |
# shellcheck disable=SC2086 # diff options should be split |
| 329 |
tt_test_diff=$(diff $tt_diff_options "$tt_test_ok_file" "$tt_test_output_file") |
| 330 |
tt_test_status=$? |
| 331 |
;; |
| 332 |
lines) |
| 333 |
tt_test_output=$(sed -n '$=' "$tt_test_output_file") |
| 334 |
test -z "$tt_test_output" && tt_test_output=0 |
| 335 |
test "$tt_test_output" -eq "$tt_test_inline" |
| 336 |
tt_test_status=$? |
| 337 |
tt_test_diff="Expected $tt_test_inline lines, got $tt_test_output." |
| 338 |
;; |
| 339 |
file) |
| 340 |
# If path is relative, make it relative to the test file path, not $PWD |
| 341 |
if test "$tt_test_inline" = "${tt_test_inline#/}"; then |
| 342 |
tt_test_inline="$(dirname "$tt_test_file")/$tt_test_inline" |
| 343 |
fi |
| 344 |
# Abort when ok file not found/readable |
| 345 |
if test ! -f "$tt_test_inline" || test ! -r "$tt_test_inline"; then |
| 346 |
tt_error "cannot read inline output file '$tt_test_inline', from line $tt_line_number of $tt_test_file" |
| 347 |
fi |
| 348 |
|
| 349 |
# shellcheck disable=SC2086 # diff options should be split |
| 350 |
tt_test_diff=$(diff $tt_diff_options "$tt_test_inline" "$tt_test_output_file") |
| 351 |
tt_test_status=$? |
| 352 |
;; |
| 353 |
egrep) |
| 354 |
grep -E "$tt_test_inline" "$tt_test_output_file" > /dev/null |
| 355 |
tt_test_status=$? |
| 356 |
|
| 357 |
# Test failed: the regex not matched |
| 358 |
if test $tt_test_status -eq 1; then |
| 359 |
tt_test_diff="egrep '$tt_test_inline' failed in:$tt_nl$(cat "$tt_test_output_file")" |
| 360 |
|
| 361 |
# Regex errors are common and user must take action to fix them |
| 362 |
elif test $tt_test_status -gt 1; then |
| 363 |
tt_error "check your inline egrep regex at line $tt_line_number of $tt_test_file" |
| 364 |
fi |
| 365 |
;; |
| 366 |
perl | regex) |
| 367 |
# Escape regex delimiter (if any) inside the regex: ' => \' |
| 368 |
if test "$tt_test_inline" != "${tt_test_inline#*\'}"; then |
| 369 |
tt_test_inline=$(printf %s "$tt_test_inline" | sed "s/'/\\\\'/g") |
| 370 |
fi |
| 371 |
|
| 372 |
# Note: -0777 to get the full file contents as a single string |
| 373 |
perl -0777 -ne "exit(!m'$tt_test_inline')" "$tt_test_output_file" |
| 374 |
tt_test_status=$? |
| 375 |
|
| 376 |
case $tt_test_status in |
| 377 |
0) # Test matched, nothing to do |
| 378 |
: |
| 379 |
;; |
| 380 |
1) # Test failed: the regex not matched |
| 381 |
tt_test_diff="Perl regex '$tt_test_inline' not matched in:$tt_nl$(cat "$tt_test_output_file")" |
| 382 |
;; |
| 383 |
127) # Perl not found :( |
| 384 |
tt_error "Perl not found. It's needed by --$tt_test_mode at line $tt_line_number of $tt_test_file" |
| 385 |
;; |
| 386 |
255) # Regex syntax errors are common and user must take action to fix them |
| 387 |
tt_error "check your inline Perl regex at line $tt_line_number of $tt_test_file" |
| 388 |
;; |
| 389 |
*) |
| 390 |
tt_error "unknown error when running Perl for --$tt_test_mode at line $tt_line_number of $tt_test_file" |
| 391 |
;; |
| 392 |
esac |
| 393 |
;; |
| 394 |
exit) |
| 395 |
test "$tt_test_exit_code" -eq "$tt_test_inline" |
| 396 |
tt_test_status=$? |
| 397 |
tt_test_diff="Expected exit code $tt_test_inline, got $tt_test_exit_code" |
| 398 |
;; |
| 399 |
*) |
| 400 |
tt_error "unknown test mode '$tt_test_mode'" |
| 401 |
;; |
| 402 |
esac |
| 403 |
|
| 404 |
# Test failed :( |
| 405 |
if test $tt_test_status -ne 0; then |
| 406 |
tt_nr_file_fails=$((tt_nr_file_fails + 1)) |
| 407 |
tt_nr_total_fails=$((tt_nr_total_fails + 1)) |
| 408 |
tt_failed_range="$tt_failed_range$tt_test_number," |
| 409 |
|
| 410 |
# Decide the message format |
| 411 |
if test "$tt_output_mode" = 'list-run'; then |
| 412 |
# List mode |
| 413 |
tt_list_test fail |
| 414 |
else |
| 415 |
# Normal mode: show FAILED message and the diff |
| 416 |
if test $tt_separator_line_shown -eq 0; then # avoid dups |
| 417 |
tt_message "${tt_color_red}$(tt_separator_line)${tt_color_off}" |
| 418 |
fi |
| 419 |
tt_message "${tt_color_red}[FAILED #$tt_test_number, line $tt_test_line_number] $tt_test_command${tt_color_off}" |
| 420 |
tt_message "$tt_test_diff" | sed '1 { /^--- / { N; /\n+++ /d; }; }' # no ---/+++ headers |
| 421 |
tt_message "${tt_color_red}$(tt_separator_line)${tt_color_off}" |
| 422 |
tt_separator_line_shown=1 |
| 423 |
fi |
| 424 |
|
| 425 |
# Should I abort now? |
| 426 |
if test $tt_stop_on_first_fail -eq 1; then |
| 427 |
exit 1 |
| 428 |
fi |
| 429 |
|
| 430 |
# Test OK |
| 431 |
else |
| 432 |
test "$tt_output_mode" = 'list-run' && tt_list_test ok |
| 433 |
fi |
| 434 |
|
| 435 |
tt_reset_test_data |
| 436 |
} |
| 437 |
tt_process_test_file() { |
| 438 |
# Reset counters |
| 439 |
tt_nr_file_tests=0 |
| 440 |
tt_nr_file_fails=0 |
| 441 |
tt_nr_file_skips=0 |
| 442 |
tt_line_number=0 |
| 443 |
tt_test_line_number=0 |
| 444 |
|
| 445 |
# Loop for each line of input file |
| 446 |
# Note: changing IFS to avoid right-trimming of spaces/tabs |
| 447 |
# Note: read -r to preserve the backslashes |
| 448 |
while IFS='' read -r tt_input_line || test -n "$tt_input_line"; do |
| 449 |
tt_line_number=$((tt_line_number + 1)) |
| 450 |
tt_debug INPUT_LINE "$tt_input_line" |
| 451 |
|
| 452 |
case "$tt_input_line" in |
| 453 |
|
| 454 |
"$tt_prefix$tt_prompt" | "$tt_prefix${tt_prompt% }" | "$tt_prefix$tt_prompt ") |
| 455 |
# Prompt alone: closes previous command line (if any) |
| 456 |
|
| 457 |
tt_debug 'LINE_$' "$tt_input_line" |
| 458 |
|
| 459 |
# Run pending tests |
| 460 |
test -n "$tt_test_command" && tt_run_test |
| 461 |
;; |
| 462 |
|
| 463 |
"$tt_prefix$tt_prompt"*) |
| 464 |
# This line is a command line to be tested |
| 465 |
|
| 466 |
tt_debug LINE_CMD "$tt_input_line" |
| 467 |
|
| 468 |
# Run pending tests |
| 469 |
test -n "$tt_test_command" && tt_run_test |
| 470 |
|
| 471 |
# Remove the prompt |
| 472 |
tt_test_command="${tt_input_line#"$tt_prefix$tt_prompt"}" |
| 473 |
|
| 474 |
# Save the test's line number for future messages |
| 475 |
tt_test_line_number=$tt_line_number |
| 476 |
|
| 477 |
# This is a special test with inline output? |
| 478 |
if printf '%s\n' "$tt_test_command" | grep "$tt_inline_prefix" > /dev/null; then |
| 479 |
# Separate command from inline output |
| 480 |
tt_test_command="${tt_test_command%"$tt_inline_prefix"*}" |
| 481 |
tt_test_inline="${tt_input_line##*"$tt_inline_prefix"}" |
| 482 |
|
| 483 |
tt_debug NEW_CMD "$tt_test_command" |
| 484 |
tt_debug OK_INLINE "$tt_test_inline" |
| 485 |
|
| 486 |
# Maybe the OK text has options? |
| 487 |
case "$tt_test_inline" in |
| 488 |
'--egrep '*) |
| 489 |
tt_test_inline=${tt_test_inline#--egrep } |
| 490 |
tt_test_mode='egrep' |
| 491 |
;; |
| 492 |
'--regex '*) # alias to --perl |
| 493 |
tt_test_inline=${tt_test_inline#--regex } |
| 494 |
tt_test_mode='regex' |
| 495 |
;; |
| 496 |
'--perl '*) |
| 497 |
tt_test_inline=${tt_test_inline#--perl } |
| 498 |
tt_test_mode='perl' |
| 499 |
;; |
| 500 |
'--file '*) |
| 501 |
tt_test_inline=${tt_test_inline#--file } |
| 502 |
tt_test_mode='file' |
| 503 |
;; |
| 504 |
'--lines '*) |
| 505 |
tt_test_inline=${tt_test_inline#--lines } |
| 506 |
tt_test_mode='lines' |
| 507 |
;; |
| 508 |
'--exit '*) |
| 509 |
tt_test_inline=${tt_test_inline#--exit } |
| 510 |
tt_test_mode='exit' |
| 511 |
;; |
| 512 |
'--eval '*) |
| 513 |
tt_test_inline=${tt_test_inline#--eval } |
| 514 |
tt_test_mode='eval' |
| 515 |
;; |
| 516 |
'--text '*) |
| 517 |
tt_test_inline=${tt_test_inline#--text } |
| 518 |
tt_test_mode='text' |
| 519 |
;; |
| 520 |
*) |
| 521 |
tt_test_mode='text' |
| 522 |
;; |
| 523 |
esac |
| 524 |
|
| 525 |
tt_debug OK_TEXT "$tt_test_inline" |
| 526 |
|
| 527 |
# There must be a number in --lines and --exit |
| 528 |
if test "$tt_test_mode" = 'lines' || test "$tt_test_mode" = 'exit'; then |
| 529 |
case "$tt_test_inline" in |
| 530 |
'' | *[!0-9]*) |
| 531 |
tt_error "--$tt_test_mode requires a number. See line $tt_line_number of $tt_test_file" |
| 532 |
;; |
| 533 |
esac |
| 534 |
fi |
| 535 |
|
| 536 |
# An empty inline parameter is an error user must see |
| 537 |
if test -z "$tt_test_inline" && test "$tt_test_mode" != 'text'; then |
| 538 |
tt_error "empty --$tt_test_mode at line $tt_line_number of $tt_test_file" |
| 539 |
fi |
| 540 |
|
| 541 |
# Since we already have the command and the output, run test |
| 542 |
tt_run_test |
| 543 |
else |
| 544 |
# It's a normal command line, output begins in next line |
| 545 |
tt_test_mode='output' |
| 546 |
|
| 547 |
tt_debug NEW_CMD "$tt_test_command" |
| 548 |
fi |
| 549 |
;; |
| 550 |
|
| 551 |
*) |
| 552 |
# Test output, blank line or comment |
| 553 |
|
| 554 |
tt_debug 'LINE_MISC' "$tt_input_line" |
| 555 |
|
| 556 |
# Ignore this line if there's no pending test |
| 557 |
test -n "$tt_test_command" || continue |
| 558 |
|
| 559 |
# Required prefix is missing: we just left a command block |
| 560 |
if test -n "$tt_prefix" && test "${tt_input_line#"$tt_prefix"}" = "$tt_input_line"; then |
| 561 |
tt_debug BLOCK_OUT "$tt_input_line" |
| 562 |
|
| 563 |
# Run the pending test and we're done in this line |
| 564 |
tt_run_test |
| 565 |
continue |
| 566 |
fi |
| 567 |
|
| 568 |
# This line is a test output, save it (without prefix) |
| 569 |
tt_test_ok_text="$tt_test_ok_text${tt_input_line#"$tt_prefix"}$tt_nl" |
| 570 |
|
| 571 |
tt_debug OK_TEXT "${tt_input_line#"$tt_prefix"}" |
| 572 |
;; |
| 573 |
esac |
| 574 |
done < "$tt_temp_file" |
| 575 |
|
| 576 |
tt_debug LOOP_OUT "\$tt_test_command=$tt_test_command" |
| 577 |
|
| 578 |
# Run pending tests |
| 579 |
test -n "$tt_test_command" && tt_run_test |
| 580 |
} |
| 581 |
tt_make_temp_dir() { |
| 582 |
# Create private temporary dir and sets global $tt_temp_dir. |
| 583 |
# http://mywiki.wooledge.org/BashFAQ/062 |
| 584 |
|
| 585 |
# Prefer mktemp when available |
| 586 |
tt_temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/clitest.XXXXXX" 2> /dev/null) && return 0 |
| 587 |
|
| 588 |
# No mktemp, let's create the dir manually |
| 589 |
# shellcheck disable=SC2015 |
| 590 |
tt_temp_dir="${TMPDIR:-/tmp}/clitest.$(awk 'BEGIN { srand(); print rand() }').$$" && |
| 591 |
mkdir -m 700 "$tt_temp_dir" || |
| 592 |
tt_error "cannot create temporary dir: $tt_temp_dir" |
| 593 |
} |
| 594 |
|
| 595 |
### Init process |
| 596 |
|
| 597 |
# Handle command line options |
| 598 |
while test "${1#-}" != "$1"; do |
| 599 |
case "$1" in |
| 600 |
-1 | --first) |
| 601 |
shift |
| 602 |
tt_stop_on_first_fail=1 |
| 603 |
;; |
| 604 |
-l | --list) |
| 605 |
shift |
| 606 |
tt_output_mode='list' |
| 607 |
;; |
| 608 |
-L | --list-run) |
| 609 |
shift |
| 610 |
tt_output_mode='list-run' |
| 611 |
;; |
| 612 |
-q | --quiet) |
| 613 |
shift |
| 614 |
tt_output_mode='quiet' |
| 615 |
;; |
| 616 |
-t | --test) |
| 617 |
shift |
| 618 |
tt_run_range="$1" |
| 619 |
shift |
| 620 |
;; |
| 621 |
-s | --skip) |
| 622 |
shift |
| 623 |
tt_skip_range="$1" |
| 624 |
shift |
| 625 |
;; |
| 626 |
--pre-flight) |
| 627 |
shift |
| 628 |
tt_pre_command="$1" |
| 629 |
shift |
| 630 |
;; |
| 631 |
--post-flight) |
| 632 |
shift |
| 633 |
tt_post_command="$1" |
| 634 |
shift |
| 635 |
;; |
| 636 |
-P | --progress) |
| 637 |
shift |
| 638 |
tt_progress="$1" |
| 639 |
tt_output_mode='normal' |
| 640 |
shift |
| 641 |
;; |
| 642 |
--color | --colour) |
| 643 |
shift |
| 644 |
tt_color_mode="$1" |
| 645 |
shift |
| 646 |
;; |
| 647 |
--diff-options) |
| 648 |
shift |
| 649 |
tt_diff_options="$1" |
| 650 |
shift |
| 651 |
;; |
| 652 |
--inline-prefix) |
| 653 |
shift |
| 654 |
tt_inline_prefix="$1" |
| 655 |
shift |
| 656 |
;; |
| 657 |
--prefix) |
| 658 |
shift |
| 659 |
tt_prefix="$1" |
| 660 |
shift |
| 661 |
;; |
| 662 |
--prompt) |
| 663 |
shift |
| 664 |
tt_prompt="$1" |
| 665 |
shift |
| 666 |
;; |
| 667 |
-h | --help) |
| 668 |
printf '%s\n' "$tt_my_help" |
| 669 |
exit 0 |
| 670 |
;; |
| 671 |
-V | --version) |
| 672 |
printf '%s %s\n' "$tt_my_name" "$tt_my_version" |
| 673 |
exit 0 |
| 674 |
;; |
| 675 |
--debug) |
| 676 |
# Undocumented dev-only option |
| 677 |
shift |
| 678 |
tt_debug=1 |
| 679 |
;; |
| 680 |
--) |
| 681 |
# No more options to process |
| 682 |
shift |
| 683 |
break |
| 684 |
;; |
| 685 |
-) |
| 686 |
# Argument - means "read test file from STDIN" |
| 687 |
break |
| 688 |
;; |
| 689 |
*) |
| 690 |
tt_error "invalid option $1" |
| 691 |
;; |
| 692 |
esac |
| 693 |
done |
| 694 |
|
| 695 |
# Command line options consumed, now it's just the files |
| 696 |
tt_nr_files=$# |
| 697 |
|
| 698 |
# No files? |
| 699 |
if test $tt_nr_files -eq 0; then |
| 700 |
tt_error 'no test file informed (try --help)' |
| 701 |
fi |
| 702 |
|
| 703 |
# Handy shortcuts for prefixes |
| 704 |
case "$tt_prefix" in |
| 705 |
tab) |
| 706 |
tt_prefix="$tt_tab" |
| 707 |
;; |
| 708 |
0) |
| 709 |
tt_prefix='' |
| 710 |
;; |
| 711 |
[1-9] | [1-9][0-9]) # 1-99 |
| 712 |
# convert number to spaces: 2 => ' ' |
| 713 |
tt_prefix=$(printf "%${tt_prefix}s" ' ') |
| 714 |
;; |
| 715 |
*\\*) |
| 716 |
tt_prefix="$(printf %b "$tt_prefix")" # expand \t and others |
| 717 |
;; |
| 718 |
esac |
| 719 |
|
| 720 |
# Validate and normalize progress value |
| 721 |
if test "$tt_output_mode" = 'normal'; then |
| 722 |
case "$tt_progress" in |
| 723 |
test) |
| 724 |
: |
| 725 |
;; |
| 726 |
number | n | [0-9]) |
| 727 |
tt_progress='number' |
| 728 |
;; |
| 729 |
dot | .) |
| 730 |
tt_progress='.' |
| 731 |
;; |
| 732 |
none | no) |
| 733 |
tt_progress='none' |
| 734 |
;; |
| 735 |
?) # Single char, use it as the progress |
| 736 |
: |
| 737 |
;; |
| 738 |
*) |
| 739 |
tt_error "invalid value '$tt_progress' for --progress. Use: test, number, dot or none." |
| 740 |
;; |
| 741 |
esac |
| 742 |
fi |
| 743 |
|
| 744 |
# Will we use colors in the output? |
| 745 |
case "$tt_color_mode" in |
| 746 |
always | yes | y) |
| 747 |
tt_use_colors=1 |
| 748 |
;; |
| 749 |
never | no | n) |
| 750 |
tt_use_colors=0 |
| 751 |
;; |
| 752 |
auto | a) |
| 753 |
# The auto mode will use colors if the output is a terminal |
| 754 |
# Note: test -t is in POSIX |
| 755 |
if test -t 1; then |
| 756 |
tt_use_colors=1 |
| 757 |
else |
| 758 |
tt_use_colors=0 |
| 759 |
fi |
| 760 |
;; |
| 761 |
*) |
| 762 |
tt_error "invalid value '$tt_color_mode' for --color. Use: auto, always or never." |
| 763 |
;; |
| 764 |
esac |
| 765 |
|
| 766 |
# Set colors |
| 767 |
# Remember: colors must be readable in dark and light backgrounds |
| 768 |
# Customization: tweak the numbers after [ to adjust the colors |
| 769 |
if test $tt_use_colors -eq 1; then |
| 770 |
tt_color_red=$( printf '\033[31m') # fail |
| 771 |
tt_color_green=$(printf '\033[32m') # ok |
| 772 |
tt_color_cyan=$( printf '\033[36m') # debug |
| 773 |
tt_color_off=$( printf '\033[m') |
| 774 |
fi |
| 775 |
|
| 776 |
# Find the terminal width |
| 777 |
# The COLUMNS env var is set by Bash (must be exported in ~/.bashrc). |
| 778 |
# In other shells, try to use 'tput cols' (not POSIX). |
| 779 |
# If not, defaults to 50 columns, a conservative amount. |
| 780 |
: ${COLUMNS:=$(tput cols 2> /dev/null)} |
| 781 |
: "${COLUMNS:=50}" |
| 782 |
|
| 783 |
# Parse and validate --test option value, if informed |
| 784 |
tt_run_range_data=$(tt_parse_range "$tt_run_range") |
| 785 |
if test $? -ne 0; then |
| 786 |
tt_error "invalid argument for -t or --test: $tt_run_range" |
| 787 |
fi |
| 788 |
|
| 789 |
# Parse and validate --skip option value, if informed |
| 790 |
tt_skip_range_data=$(tt_parse_range "$tt_skip_range") |
| 791 |
if test $? -ne 0; then |
| 792 |
tt_error "invalid argument for -s or --skip: $tt_skip_range" |
| 793 |
fi |
| 794 |
|
| 795 |
### Real execution begins here |
| 796 |
|
| 797 |
trap tt_clean_up EXIT |
| 798 |
|
| 799 |
# Temporary files (using files because <(...) is not portable) |
| 800 |
tt_make_temp_dir # sets global $tt_temp_dir |
| 801 |
tt_temp_file="$tt_temp_dir/temp.txt" |
| 802 |
tt_stdin_file="$tt_temp_dir/stdin.txt" |
| 803 |
tt_test_ok_file="$tt_temp_dir/ok.txt" |
| 804 |
tt_test_output_file="$tt_temp_dir/output.txt" |
| 805 |
|
| 806 |
# Some preparing command to run before all the tests? |
| 807 |
if test -n "$tt_pre_command"; then |
| 808 |
eval "$tt_pre_command" || |
| 809 |
tt_error "pre-flight command failed with status=$?: $tt_pre_command" |
| 810 |
fi |
| 811 |
|
| 812 |
# For each input file in $@ |
| 813 |
for tt_test_file; do |
| 814 |
# Some tests may 'cd' to another dir, we need to get back |
| 815 |
# to preserve the relative paths of the input files |
| 816 |
cd "$tt_original_dir" || |
| 817 |
tt_error "cannot enter starting directory $tt_original_dir" |
| 818 |
|
| 819 |
# Support using '-' to read the test file from STDIN |
| 820 |
if test "$tt_test_file" = '-'; then |
| 821 |
tt_test_file="$tt_stdin_file" |
| 822 |
cat > "$tt_test_file" |
| 823 |
fi |
| 824 |
|
| 825 |
# Abort when test file is a directory |
| 826 |
if test -d "$tt_test_file"; then |
| 827 |
tt_error "input file is a directory: $tt_test_file" |
| 828 |
fi |
| 829 |
|
| 830 |
# Abort when test file not found/readable |
| 831 |
if test ! -r "$tt_test_file"; then |
| 832 |
tt_error "cannot read input file: $tt_test_file" |
| 833 |
fi |
| 834 |
|
| 835 |
# In multifile mode, identify the current file |
| 836 |
if test $tt_nr_files -gt 1; then |
| 837 |
case "$tt_output_mode" in |
| 838 |
normal) |
| 839 |
# Normal mode, show message with filename |
| 840 |
case "$tt_progress" in |
| 841 |
test | none) |
| 842 |
tt_message "Testing file $tt_test_file" |
| 843 |
;; |
| 844 |
*) |
| 845 |
test $tt_missing_nl -eq 1 && echo |
| 846 |
tt_message_part "Testing file $tt_test_file " |
| 847 |
;; |
| 848 |
esac |
| 849 |
;; |
| 850 |
list | list-run) |
| 851 |
# List mode, show ------ and the filename |
| 852 |
tt_message "$(tt_separator_line | cut -c 1-40)" "$tt_test_file" |
| 853 |
;; |
| 854 |
esac |
| 855 |
fi |
| 856 |
|
| 857 |
# Convert Windows files (CRLF) to the Unix format (LF) |
| 858 |
# Note: the temporary file is required, because doing "sed | while" opens |
| 859 |
# a subshell and global vars won't be updated outside the loop. |
| 860 |
sed "s/$(printf '\r')$//" "$tt_test_file" > "$tt_temp_file" |
| 861 |
|
| 862 |
# The magic happens here |
| 863 |
tt_process_test_file |
| 864 |
|
| 865 |
# Abort when no test found (and no active range with --test or --skip) |
| 866 |
if test $tt_nr_file_tests -eq 0 && test -z "$tt_run_range_data" && test -z "$tt_skip_range_data"; then |
| 867 |
tt_error "no test found in input file: $tt_test_file" |
| 868 |
fi |
| 869 |
|
| 870 |
# Save file stats |
| 871 |
tt_nr_file_ok=$((tt_nr_file_tests - tt_nr_file_fails - tt_nr_file_skips)) |
| 872 |
tt_files_stats="$tt_files_stats$tt_nr_file_ok $tt_nr_file_fails $tt_nr_file_skips$tt_nl" |
| 873 |
|
| 874 |
# Dots mode: any missing new line? |
| 875 |
# Note: had to force tt_missing_nl=0, even when it's done in tt_message :/ |
| 876 |
test $tt_missing_nl -eq 1 && tt_missing_nl=0 && tt_message |
| 877 |
done |
| 878 |
|
| 879 |
# Some clean up command to run after all the tests? |
| 880 |
if test -n "$tt_post_command"; then |
| 881 |
eval "$tt_post_command" || |
| 882 |
tt_error "post-flight command failed with status=$?: $tt_post_command" |
| 883 |
fi |
| 884 |
|
| 885 |
#----------------------------------------------------------------------- |
| 886 |
# From this point on, it's safe to use non-prefixed global vars |
| 887 |
#----------------------------------------------------------------------- |
| 888 |
|
| 889 |
# Range active, but no test matched :( |
| 890 |
if test $tt_nr_total_tests -eq $tt_nr_total_skips; then |
| 891 |
if test -n "$tt_run_range_data" && test -n "$tt_skip_range_data"; then |
| 892 |
tt_error "no test found. The combination of -t and -s resulted in no tests." |
| 893 |
elif test -n "$tt_run_range_data"; then |
| 894 |
tt_error "no test found for the specified number or range '$tt_run_range'" |
| 895 |
elif test -n "$tt_skip_range_data"; then |
| 896 |
tt_error "no test found. Maybe '--skip $tt_skip_range' was too much?" |
| 897 |
fi |
| 898 |
fi |
| 899 |
|
| 900 |
# List mode has no stats |
| 901 |
if test "$tt_output_mode" = 'list' || test "$tt_output_mode" = 'list-run'; then |
| 902 |
if test $tt_nr_total_fails -eq 0; then |
| 903 |
exit 0 |
| 904 |
else |
| 905 |
exit 1 |
| 906 |
fi |
| 907 |
fi |
| 908 |
|
| 909 |
# Show stats |
| 910 |
# Data: |
| 911 |
# $tt_files_stats -> "100 0 23 \n 12 34 0" |
| 912 |
# $@ -> foo.sh bar.sh |
| 913 |
# Output: |
| 914 |
# ok fail skip |
| 915 |
# 100 0 23 foo.sh |
| 916 |
# 12 34 0 bar.sh |
| 917 |
if test $tt_nr_files -gt 1 && test "$tt_output_mode" != 'quiet'; then |
| 918 |
echo |
| 919 |
printf ' %5s %5s %5s\n' ok fail skip |
| 920 |
printf %s "$tt_files_stats" | while IFS=' ' read -r ok fail skip; do |
| 921 |
printf ' %5s %5s %5s %s\n' "$ok" "$fail" "$skip" "$1" |
| 922 |
shift |
| 923 |
done | sed 's/ 0/ -/g' # hide zeros |
| 924 |
echo |
| 925 |
fi |
| 926 |
|
| 927 |
# The final message: OK or FAIL? |
| 928 |
# OK: 123 of 123 tests passed |
| 929 |
# OK: 100 of 123 tests passed (23 skipped) |
| 930 |
# FAIL: 123 of 123 tests failed |
| 931 |
# FAIL: 100 of 123 tests failed (23 skipped) |
| 932 |
skips= |
| 933 |
if test $tt_nr_total_skips -gt 0; then |
| 934 |
skips=" ($tt_nr_total_skips skipped)" |
| 935 |
fi |
| 936 |
if test $tt_nr_total_fails -eq 0; then |
| 937 |
stamp="${tt_color_green}OK:${tt_color_off}" |
| 938 |
stats="$((tt_nr_total_tests - tt_nr_total_skips)) of $tt_nr_total_tests tests passed" |
| 939 |
test $tt_nr_total_tests -eq 1 && stats=$(echo "$stats" | sed 's/tests /test /') |
| 940 |
tt_message "$stamp $stats$skips" |
| 941 |
exit 0 |
| 942 |
else |
| 943 |
test $tt_nr_files -eq 1 && tt_message # separate from previous FAILED message |
| 944 |
|
| 945 |
stamp="${tt_color_red}FAIL:${tt_color_off}" |
| 946 |
stats="$tt_nr_total_fails of $tt_nr_total_tests tests failed" |
| 947 |
test $tt_nr_total_tests -eq 1 && stats=$(echo "$stats" | sed 's/tests /test /') |
| 948 |
tt_message "$stamp $stats$skips" |
| 949 |
# test $tt_test_file = 'test.md' && tt_message "-t ${tt_failed_range%,}" # dev helper |
| 950 |
exit 1 |
| 951 |
fi |