Refactor colorize function away from the test script

This commit is contained in:
Alex Leferry 2 2019-03-07 18:06:27 +01:00
parent 017b7c970e
commit 25630f4c91

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# Color codes ├────────────────────────────────────────────────────────────────── # Color codes ├──────────────────────────────────────────────────────────────────
none="00"; red="31"; green="32"; yellow="33"; magenta="35"; bold="01" none='\033[0m'; red='\033[31m'; green='\033[32m'; yellow='\033[33m'; magenta='\033[35m'; bold='\033[1m'
# Main ├──────────────────────────────────────────────────────────────────────── # Main ├────────────────────────────────────────────────────────────────────────
@ -50,7 +50,7 @@ main() {
echo "$indent$name" echo "$indent$name"
continue continue
elif [ -x enabled ] && ! ./enabled; then elif [ -x enabled ] && ! ./enabled; then
echo "$indent$name (disabled)" | colorize $yellow $none printf "${yellow}$indent%s (disabled)${none}\n" "$name"
continue continue
fi fi
@ -66,21 +66,21 @@ main() {
failed=0 failed=0
if [ ! -e error ]; then # failure not expected if [ ! -e error ]; then # failure not expected
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
echo "$indent$name" | colorize $red $none printf "${red}$indent%s${none}\n" "$name"
echo "$indent Kakoune returned error $retval" echo "$indent Kakoune returned error $retval"
failed=1 failed=1
else else
for file in out selections state ui-out; do for file in out selections state ui-out; do
if [ -f $root/$dir/$file ] && ! cmp -s $root/$dir/$file $file; then if [ -f $root/$dir/$file ] && ! cmp -s $root/$dir/$file $file; then
if [ $failed -eq 0 ]; then if [ $failed -eq 0 ]; then
echo "$indent$name" | colorize $red $none printf "${red}$indent%s${none}\n" "$name"
failed=1 failed=1
fi fi
show_diff $root/$dir/$file $file show_diff $root/$dir/$file $file
fi fi
done done
if [ $failed -ne 0 ] && [ -f debug ]; then if [ $failed -ne 0 ] && [ -f debug ]; then
printf "\ndebug buffer:\n" | colorize $yellow $none printf "\n${yellow}debug buffer:${none}\n"
cat debug cat debug
fi fi
fi fi
@ -88,19 +88,19 @@ main() {
if [ -f stderr ]; then if [ -f stderr ]; then
sed -i -e 's/^[0-9]*:[0-9]*: //g' stderr sed -i -e 's/^[0-9]*:[0-9]*: //g' stderr
if [ -s error ] && ! cmp -s error stderr; then if [ -s error ] && ! cmp -s error stderr; then
echo "$indent$name" | colorize $yellow $none printf "${yellow}$indent%s${none}\n" "$name"
show_diff error stderr show_diff error stderr
failed=1 failed=1
fi fi
elif [ $retval -eq 0 ]; then elif [ $retval -eq 0 ]; then
echo "$indent$name" | colorize $red $none printf "${red}$indent%s${none}\n" "$name"
echo "$indent Expected failure, but Kakoune returned 0" echo "$indent Expected failure, but Kakoune returned 0"
failed=1 failed=1
fi fi
fi fi
if [ $failed -eq 0 ]; then if [ $failed -eq 0 ]; then
echo "$indent$name" | colorize $green $none printf "${green}$indent%s${none}\n" "$name"
else else
number_failures=$(($number_failures + 1)) number_failures=$(($number_failures + 1))
fi fi
@ -111,18 +111,12 @@ main() {
else else
color=$green color=$green
fi fi
printf "\nSummary: %s tests, %s failures\n" $number_tests $number_failures | colorize $color $none printf "\n${color}Summary: %s tests, %s failures${none}\n" $number_tests $number_failures
exit $number_failures exit $number_failures
} }
# Utility ├───────────────────────────────────────────────────────────────────── # Utility ├─────────────────────────────────────────────────────────────────────
colorize() {
color=${1:-$none}
style=${2:-$none}
printf '\033[%s;%sm%s\033[00;00m\n' $style $color "$(cat)"
}
show_diff() { show_diff() {
diff -u $1 $2 | while IFS='' read -r line; do diff -u $1 $2 | while IFS='' read -r line; do
first_character=$(printf '%s\n' "$line" | cut -b 1) first_character=$(printf '%s\n' "$line" | cut -b 1)
@ -132,7 +126,7 @@ show_diff() {
@) color=$magenta ;; @) color=$magenta ;;
*) color=$none ;; *) color=$none ;;
esac esac
printf '%s\n' "$line" | colorize $color $none printf "${color}%s${none}\n" "$line"
done done
} }