Refactor the unit test run script to be more idiomatic shell

This commit is contained in:
Maxime Coste 2014-10-12 12:27:22 +01:00
parent 1bf5a1eee5
commit ff27b385d2

174
test/run
View File

@ -2,65 +2,75 @@
# Main ├──────────────────────────────────────────────────────────────────────── # Main ├────────────────────────────────────────────────────────────────────────
main() { number_tests=0 number_failures=0 main() {
dirs=$@ local number_tests=0
test=$(pwd) local number_failures=0
work=$(mktemp --directory) local dirs=$@
local test=$(pwd)
local work=$(mktemp --directory)
cp --recursive . $work cp --recursive . $work
trap "rm --recursive $work" EXIT trap "rm --recursive $work" EXIT
for dir in $(find $dirs -type d); do for dir in $(find $dirs -type d); do
cd $test/$dir; test_files=$(find * -name out -o -name selections -o -name state) cd $test/$dir;
cd $work/$dir; { IFS=¬ local test_files=$(find * -name out -o -name selections -o -name state)
indent=$(repeat ' ' $(pwd | sed "s|$test||" | tr --delete --complement / | awk '{ print length }')) cd $work/$dir;
name=$(basename $PWD) IFS=¬
! exists cmd && { local indent=$(repeat ' ' $(pwd | sed "s|$test||" | tr --delete --complement / | awk '{ print length }'))
echo "$indent$name" local name=$(basename $PWD)
} || { nop=$((number_tests++)) if ! exists cmd; then
touch in; cp in out echo "$indent$name"
kak_commands="set global autoreload yes else
try %{ ((number_tests++))
source rc touch in; cp in out
} local kak_commands="
try %{ set global autoreload yes
exec '%s%[(](.+?)[)]<ret>i<del><del><esc>a<backspace><c-u><esc>' try %{
} \ source rc
catch %{ }
exec gg try %{
} exec '%s%[(](.+?)[)]<ret>i<del><del><esc>a<backspace><c-u><esc>'
exec '$(<cmd)' } \
eval -buffer *debug* write debug catch %{
nop %sh{ IFS== exec gg
echo \"\$kak_selections\" > selections }
echo \"\$kak_selections_desc\" > state exec '$(<cmd)'
} eval -buffer *debug* write debug
write out nop %sh{ IFS==
quit! echo \"\$kak_selections\" > selections
" echo \"\$kak_selections_desc\" > state
kak out -n -e $kak_commands }
IFS=$'\n' write out
for expect in $test_files; do quit!
cmp --quiet $test/$dir/$expect $expect && { "
echo "$indent$name" | colorize green normal kak out -n -e $kak_commands
} || { nop=$((number_failures++)) IFS=$'\n'
echo "$indent$name" | colorize red normal for expect in $test_files; do
echo if cmp --quiet $test/$dir/$expect $expect; then
IFS=$'\n' echo "$indent$name" | colorize green normal
for line in $(diff --unified $test/$dir/$expect $expect); do IFS=¬ else
first_character=$(head --bytes 1 <<< $line) ((number_failures++))
color=$(match $first_character + green - red @ magenta none) echo "$indent$name" | colorize red normal
colorize $color normal <<< $line echo
done IFS=$'\n'
echo for line in $(diff --unified $test/$dir/$expect $expect); do
colorize yellow normal <<< "debug buffer:" IFS=¬
cat debug local first_character=$(echo "$line" | head --bytes 1)
echo local color=$(match $first_character + green - red @ magenta none)
} echo "$line" | colorize $color normal
done done
} echo
} echo "debug buffer:" | colorize yellow normal
cat debug
echo
fi
done
fi
done done
(( $number_failures > 0 )) && color=red || if (( $number_failures > 0 )); then
color=green color=red
else
color=green
fi
echo echo
echo Resume: echo Resume:
echo $number_tests tests, $number_failures failures | colorize $color normal echo $number_tests tests, $number_failures failures | colorize $color normal
@ -69,22 +79,29 @@ main() { number_tests=0 number_failures=0
# Utility ├───────────────────────────────────────────────────────────────────── # Utility ├─────────────────────────────────────────────────────────────────────
match() { match() {
expression=$1; shift expression="$1";
while [[ $@ ]]; do shift
pattern=$1; shift; value=$1 next=$1 default_value=$pattern while [[ "$@" ]]; do
[[ $next ]] || { local pattern="$1"
shift
local value="$1"
local next="$1"
local default_value="$pattern"
if [[ -z "$next" ]]; then
echo $default_value echo $default_value
return 1 return 1
} fi
[[ $expression = $pattern ]] && { if [[ "$expression" = "$pattern" ]]; then
echo $value echo "$value"
return 0 return 0
} fi
shift shift
done done
} }
repeat() { text=$1 count=${2:-0} repeat() {
local text=$1
local count=${2:-0}
echo $(yes $text | head --lines $count | join) echo $(yes $text | head --lines $count | join)
} }
@ -96,20 +113,25 @@ exists() {
test -e $@ test -e $@
} }
get_ansi_code() { color_name=${1:-none} style_name=${2:-none} get_ansi_code() {
color='none 00 local color_name=${1:-none}
red 31 local style_name=${2:-none}
green 32 local color='none 00
yellow 33 red 31
magenta 35' green 32
style='none 00 yellow 33
bold 01' magenta 35'
color_nr=$(awk "/$color_name/ { print \$2 }" <<< "$color") local style='none 00
style_nr=$(awk "/$style_name/ { print \$2 }" <<< "$style") bold 01'
sed s/COLOR_NR/$color_nr/';'s/STYLE_NR/$style_nr/ <<< '\e[STYLE_NR;COLOR_NRm' 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)" echo -e "$(get_ansi_code $color_name $style_name)$text$(get_ansi_code none none)"
} }