From ff27b385d28e2afedc092e17c8e2b32e6c42fdd0 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 12 Oct 2014 12:27:22 +0100 Subject: [PATCH] Refactor the unit test run script to be more idiomatic shell --- test/run | 174 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 98 insertions(+), 76 deletions(-) diff --git a/test/run b/test/run index ce359a0c..f3cf1594 100755 --- a/test/run +++ b/test/run @@ -2,65 +2,75 @@ # Main ├──────────────────────────────────────────────────────────────────────── -main() { number_tests=0 number_failures=0 - dirs=$@ - test=$(pwd) - work=$(mktemp --directory) +main() { + local number_tests=0 + local number_failures=0 + local dirs=$@ + local test=$(pwd) + local work=$(mktemp --directory) cp --recursive . $work trap "rm --recursive $work" EXIT for dir in $(find $dirs -type d); do - cd $test/$dir; test_files=$(find * -name out -o -name selections -o -name state) - cd $work/$dir; { IFS=¬ - indent=$(repeat ' ' $(pwd | sed "s|$test||" | tr --delete --complement / | awk '{ print length }')) - name=$(basename $PWD) - ! exists cmd && { - echo "$indent$name" - } || { nop=$((number_tests++)) - touch in; cp in out - kak_commands="set global autoreload yes - try %{ - source rc - } - try %{ - exec '%s%[(](.+?)[)]ia' - } \ - catch %{ - exec gg - } - exec '$( selections - echo \"\$kak_selections_desc\" > state - } - write out - quit! - " - kak out -n -e $kak_commands - IFS=$'\n' - for expect in $test_files; do - cmp --quiet $test/$dir/$expect $expect && { - echo "$indent$name" | colorize green normal - } || { nop=$((number_failures++)) - echo "$indent$name" | colorize red normal - echo - IFS=$'\n' - for line in $(diff --unified $test/$dir/$expect $expect); do IFS=¬ - first_character=$(head --bytes 1 <<< $line) - color=$(match $first_character + green - red @ magenta none) - colorize $color normal <<< $line - done - echo - colorize yellow normal <<< "debug buffer:" - cat debug - echo - } - done - } - } + cd $test/$dir; + local test_files=$(find * -name out -o -name selections -o -name state) + cd $work/$dir; + IFS=¬ + local indent=$(repeat ' ' $(pwd | sed "s|$test||" | tr --delete --complement / | awk '{ print length }')) + local name=$(basename $PWD) + if ! exists cmd; then + echo "$indent$name" + else + ((number_tests++)) + touch in; cp in out + local kak_commands=" + set global autoreload yes + try %{ + source rc + } + try %{ + exec '%s%[(](.+?)[)]ia' + } \ + catch %{ + exec gg + } + exec '$( selections + echo \"\$kak_selections_desc\" > state + } + write out + quit! + " + kak out -n -e $kak_commands + IFS=$'\n' + for expect in $test_files; do + if cmp --quiet $test/$dir/$expect $expect; then + echo "$indent$name" | colorize green normal + else + ((number_failures++)) + echo "$indent$name" | colorize red normal + echo + IFS=$'\n' + for line in $(diff --unified $test/$dir/$expect $expect); do + IFS=¬ + local first_character=$(echo "$line" | head --bytes 1) + local color=$(match $first_character + green - red @ magenta none) + echo "$line" | colorize $color normal + done + echo + echo "debug buffer:" | colorize yellow normal + cat debug + echo + fi + done + fi done - (( $number_failures > 0 )) && color=red || - color=green + if (( $number_failures > 0 )); then + color=red + else + color=green + fi echo echo Resume: echo $number_tests tests, $number_failures failures | colorize $color normal @@ -69,22 +79,29 @@ main() { number_tests=0 number_failures=0 # Utility ├───────────────────────────────────────────────────────────────────── match() { - expression=$1; shift - while [[ $@ ]]; do - pattern=$1; shift; value=$1 next=$1 default_value=$pattern - [[ $next ]] || { + expression="$1"; + shift + while [[ "$@" ]]; do + local pattern="$1" + shift + local value="$1" + local next="$1" + local default_value="$pattern" + if [[ -z "$next" ]]; then echo $default_value return 1 - } - [[ $expression = $pattern ]] && { - echo $value + fi + if [[ "$expression" = "$pattern" ]]; then + echo "$value" return 0 - } + fi shift done } -repeat() { text=$1 count=${2:-0} +repeat() { + local text=$1 + local count=${2:-0} echo $(yes $text | head --lines $count | join) } @@ -96,20 +113,25 @@ exists() { test -e $@ } -get_ansi_code() { color_name=${1:-none} style_name=${2:-none} - color='none 00 - red 31 - green 32 - yellow 33 - magenta 35' - style='none 00 - bold 01' - color_nr=$(awk "/$color_name/ { print \$2 }" <<< "$color") - style_nr=$(awk "/$style_name/ { print \$2 }" <<< "$style") - sed s/COLOR_NR/$color_nr/';'s/STYLE_NR/$style_nr/ <<< '\e[STYLE_NR;COLOR_NRm' +get_ansi_code() { + local color_name=${1:-none} + local style_name=${2:-none} + local color='none 00 + red 31 + green 32 + yellow 33 + magenta 35' + local style='none 00 + bold 01' + local color_nr=$(echo "$color" | awk "/$color_name/ { print \$2 }") + local style_nr=$(echo "$style" | awk "/$style_name/ { print \$2 }") + echo '\e[STYLE_NR;COLOR_NRm' | sed s/COLOR_NR/$color_nr/';'s/STYLE_NR/$style_nr/ } -colorize() { text=$(cat) color_name=${1:-none} style_name=${2:-none} +colorize() { + local text=$(cat) + local color_name=${1:-none} + local style_name=${2:-none} echo -e "$(get_ansi_code $color_name $style_name)$text$(get_ansi_code none none)" }