CI: Lint run_test: all in function

This commit is contained in:
Tinmarino
2023-03-22 01:17:20 -03:00
parent 72d02207b0
commit 57107c7c56
+199 -159
View File
@@ -1,22 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values
# credit to https://github.com/w0rp/ale for script ideas and the color vader : '
# output function. Vimwiki vader test script
# Say Hi Credit: https://github.com/w0rp/ale for script ideas and the color vader output function.
echo -en "Starting $(basename "$0") for VimWiki\n" '
start_time=$(date +%s)
red='\033[0;31m' # Capture start time now
green='\033[0;32m' declare -g start_time=$(date +%s)
nc='\033[0m'
# Declare color helper
declare -g red='\033[0;31m'
declare -g green='\033[0;32m'
declare -g nc='\033[0m'
# For windows: Cmder bash is appending busybox to the path and
# and a smlll vim is included, so that override the windows path vim
if [[ -v OLD_PATH ]]; then
echo "Setting path from OLD_PATH : $OLD_PATH"
export PATH="$OLD_PATH"
fi
printHelp() { printHelp() {
cat << ' EOF' | sed -e 's/^ //' cat << ' EOF' | sed -e 's/^ //'
@@ -67,31 +65,34 @@ print_versions() {
run_vader() { run_vader() {
# Run Vader tests # Run Vader tests
echo -e "\nStarting Vader tests." echo -e "\nStarting Vader tests."
local err=0 local -i res=0
local opt='' current_test=''
echo "Tin 1 $g_file_test"
# Parse tests files to execute # Parse tests files to execute
if [[ -z $file_test ]]; then if [[ -z "$g_file_test" ]]; then
res="test/*" opt="test/*"
else else
read -ra TEST <<< "$file_test" read -ra TEST <<< "$g_file_test"
for i in "${TEST[@]}"; do for current_test in "${TEST[@]}"; do
# Remove quotes # Remove quotes
i=${i#\'} current_test=${current_test#\'}
i=${i%\'} current_test=${current_test%\'}
if [[ "$i" == *"*"* ]]; then if [[ "$current_test" == *"*"* ]]; then
res="$res test/${i}" opt+=" test/${current_test}"
elif [[ -f "$i" ]]; then elif [[ -f "$current_test" ]]; then
res="$res test/${i}" opt+=" test/${current_test}"
elif [[ -f "${i}.vader" ]]; then elif [[ -f "${current_test}.vader" ]]; then
res="$res test/${i}.vader" opt+=" test/${current_test}.vader"
else else
printf "WARNING: Test \"%s\" not found.\n", "$i" printf "WARNING: Test \"%s\" not found.\n", "$current_test"
fi fi
done done
fi fi
echo "Tin 2 $opt"
# Run tests for each specified version # Run tests for each specified version
for v in $vers; do for v in $g_vers; do
echo -e "\n\nRunning version: $v" echo -e "\n\nRunning version: $v"
echo -e "=============================" echo -e "============================="
@@ -101,14 +102,14 @@ run_vader() {
home_save="$HOME" home_save="$HOME"
# Create temporary root # Create temporary root
mkdir -p "$tmp_dir/vader_wiki" mkdir -p "$g_tmp_dir/vader_wiki"
mkdir -p "$tmp_dir/vader_wiki/home" mkdir -p "$g_tmp_dir/vader_wiki/home"
mkdir -p "$tmp_dir/vader_wiki/home/test" mkdir -p "$g_tmp_dir/vader_wiki/home/test"
mkdir -p "$tmp_dir/vader_wiki/testplugin" mkdir -p "$g_tmp_dir/vader_wiki/testplugin"
# Set vars # Set vars
export ROOT="$tmp_dir/vader_wiki/" export ROOT="$g_tmp_dir/vader_wiki/"
export HOME="$tmp_dir/vader_wiki/home" export HOME="$g_tmp_dir/vader_wiki/home"
vim="vim" vim="vim"
vim_opt="-u ~/test/vimrc" vim_opt="-u ~/test/vimrc"
else else
@@ -134,11 +135,11 @@ run_vader() {
# flags=(--rm -v "$PWD/../:/testplugin" -v "$PWD/../test:/home" -w /testplugin vimwiki) # flags=(--rm -v "$PWD/../:/testplugin" -v "$PWD/../test:/home" -w /testplugin vimwiki)
echo -e "\nCopying resources to $ROOT" echo -e "\nCopying resources to $ROOT"
# Copy testplugin # Copy testplugin
cp -rf "$wiki_path/"* "$ROOT/testplugin/" cp -rf "$g_wiki_path/"* "$ROOT/testplugin/"
# Copy home # Copy home
cp -rf "$script_path/"* "$HOME/test/" cp -rf "$g_script_path/"* "$HOME/test/"
# Copy rtp.vim # Copy rtp.vim
cp -rf "$script_path/resources/rtp_local.vim" "$ROOT/rtp.vim" cp -rf "$g_script_path/resources/rtp_local.vim" "$ROOT/rtp.vim"
# Copy vader <- internet # Copy vader <- internet
echo 'Cloning Vader (git, do not care the fatal)' echo 'Cloning Vader (git, do not care the fatal)'
git clone --depth 10 https://github.com/junegunn/vader.vim /tmp/vader_wiki/vader 2>&1 git clone --depth 10 https://github.com/junegunn/vader.vim /tmp/vader_wiki/vader 2>&1
@@ -146,77 +147,80 @@ run_vader() {
# Run batch of tests # Run batch of tests
# shellcheck disable=SC2086,SC2206 # shellcheck disable=SC2086,SC2206
if [[ "$res" != "" ]]; then if [[ "$opt" != "" ]]; then
if [[ "$v" == "local" ]]; then if [[ "$v" == "local" ]]; then
pushd "$tmp_dir/vader_wiki/testplugin" \ pushd "$g_tmp_dir/vader_wiki/testplugin" \
|| echo 'Warning pushd testplugin failed' || echo 'Warning pushd testplugin failed'
# Run the tests # Run the tests
fcmd(){ fcmd(){
$vim $vim_opt "+Vader! ${res}" 2>&1 \ $vim $vim_opt "+Vader! ${opt}" 2>&1 \
| vader_filter | vader_color | vader_filter | vader_color
return ${PIPESTATUS[1]} return ${PIPESTATUS[1]}
} }
echo -e "\nStarting Batch Vim/Vader:\n<- $res\n" echo -e "\nStarting Batch Vim/Vader:\n<- $opt\n"
type fcmd | sed -n '/^ /{s/^ //p}' | sed '$s/.*/&;/' ; shift ; type fcmd | sed -n '/^ /{s/^ //p}' | sed '$s/.*/&;/' ; shift ;
fcmd; ret=$? fcmd; ret=$?
err=$(( err + ret ))
echo -e "\nReturned Batch Vim/Vader -> $ret" echo -e "\nReturned Batch Vim/Vader -> $ret"
(( res |= ret ))
popd \ popd || echo 'Warning popd failed'
|| echo 'Warning popd also failed'
else else
# In docker # In docker
fcmd() { fcmd() {
docker run -a stderr -e "VADER_OUTPUT_FILE=/dev/stderr" \ docker run -a stderr -e "VADER_OUTPUT_FILE=/dev/stderr" \
"${flags[@]}" "$v" $vim_opt "+Vader! ${res}" 2>&1 \ "${flags[@]}" "$v" $vim_opt "+Vader! ${opt}" 2>&1 \
| vader_filter | vader_color | vader_filter | vader_color
return ${PIPESTATUS[1]} return ${PIPESTATUS[1]}
} }
echo -e "\nStarting Batch Vim/Vader:\n<- $res\n" echo -e "\nStarting Batch Vim/Vader with: $opt\n"
type fcmd | sed -n '/^ /{s/^ //p}' | sed '$s/.*/&;/' ; shift ; type fcmd | sed -n '/^ /{s/^ //p}' | sed '$s/.*/&;/' ; shift ;
fcmd; ret=$? fcmd; ret=$?
err=$(( err + ret ))
echo -e "\nReturned Batch Docker/Vim/Vader -> $ret : ${PIPESTATUS[*]}" echo -e "\nReturned Batch Docker/Vim/Vader -> $ret : ${PIPESTATUS[*]}"
(( res |= ret ))
fi fi
fi fi
#set +o pipefail #set +o pipefail
# Restore what must (I know it should be refactored in a while) # Restore what must (I know it should be refactored in a while)
if [[ "$v" == "local" ]]; then if [[ "$v" == local ]]; then
export HOME=$home_save export HOME=$home_save
fi fi
done done
return $err return "$res"
} }
run_vint() { run_vint() {
local err=0 local -i res=0
cmd="vint -s . && vint -s test/vimrc"
if echo "$vers" | grep "local" > /dev/null; then local cmd="vint -s . && vint -s test/vimrc"
echo -e "\nRunning Vint: $cmd : in $wiki_path"
pushd "$wiki_path" > /dev/null \ if echo "$g_vers" | grep "local" > /dev/null; then
echo -e "\nRunning Vint: $cmd : in $g_wiki_path"
pushd "$g_wiki_path" > /dev/null \
|| echo 'Warning pushd wiki_path failed' || echo 'Warning pushd wiki_path failed'
$cmd $cmd
err=$(( err | $? )) res=$(( res | $? ))
popd > /dev/null \ popd > /dev/null \
|| echo 'Warning popd also failed' || echo 'Warning popd also failed'
else else
echo -e "\nStarting Docker container and running Vint: $cmd" echo -e "\nStarting Docker container and running Vint: $cmd"
docker run -a stdout "${flags[@]}" bash -c "$cmd" docker run -a stdout "${flags[@]}" bash -c "$cmd"
err=$(( err | $? )) res=$(( res | $? ))
fi fi
return $err
return "$res"
} }
vader_filter() { vader_filter() {
# Filter Vader Stdout # Filter Vader Stdout
local err=0 local -i res=0
# Keep indentation # Keep indentation
local IFS='' local IFS=''
while read -r REPLY; do while read -r REPLY; do
# Print only possible error cases # Print only possible error cases
if [[ "$REPLY" = *'docker:'* ]] || \ if [[ "$REPLY" = *'docker:'* ]] || \
@@ -228,27 +232,28 @@ vader_filter() {
[[ "$REPLY" = *'[ DO] (X)'* ]] || \ [[ "$REPLY" = *'[ DO] (X)'* ]] || \
[[ "$REPLY" = *'[ EXPECT] (X)'* ]]; then [[ "$REPLY" = *'[ EXPECT] (X)'* ]]; then
echo -e "$red$REPLY$nc" echo -e "$red$REPLY$nc"
err=1 res=1
elif [[ "$REPLY" = *'Success/Total:'* ]]; then elif [[ "$REPLY" = *'Success/Total:'* ]]; then
success="$(echo -n "$REPLY" | grep -o '[0-9]\+/' | head -n1 | cut -d/ -f1)" success="$(echo -n "$REPLY" | grep -o '[0-9]\+/' | head -n1 | cut -d/ -f1)"
total="$(echo -n "$REPLY" | grep -o '/[0-9]\+' | head -n1 | cut -d/ -f2)" total="$(echo -n "$REPLY" | grep -o '/[0-9]\+' | head -n1 | cut -d/ -f2)"
if [ "$success" -lt "$total" ]; then if [ "$success" -lt "$total" ]; then
err=1 res=1
fi fi
echo "$REPLY" echo "$REPLY"
elif [[ "$verbose" != 0 ]]; then elif [[ "$g_verbose" != 0 ]]; then
# just print everything # just print everything
echo "$REPLY" echo "$REPLY"
fi fi
done done
if [[ "$err" == 1 ]]; then if (( res == 1 )); then
echo -e "\033[0;31m" echo -e "\033[0;31m"
echo -e "!---------Failed tests detected---------!" echo -e "!---------Failed tests detected---------!"
echo -e "Run with the '-v' flag for verbose output" echo -e "Run with the '-v' flag for verbose output"
echo -e "\033[0m" echo -e "\033[0m"
fi fi
return $err
return "$res"
} }
@@ -283,103 +288,138 @@ vader_color() {
echo -en "$nc" echo -en "$nc"
} }
# path of the script, supposing no spaces
script_file="$(dirname "$0")"
script_path="$( realpath "$script_file" )"
wiki_path="$( realpath "$script_path/.." )"
tmp_dir="$(dirname "$(mktemp -u)")"
# list of vim/nvim versions parse_argument(){
vers="$(print_versions)" : 'Parse user argument'
# type of tests to run - vader/vint/all # Declare: Path of the script, supposing no spaces
type="all" declare -g g_script_file=$(dirname "$0")
declare -g g_script_path=$(realpath "$g_script_file")
# verbose output flag declare -g g_wiki_path=$(realpath "$g_script_path/..")
verbose=0 declare -g g_tmp_dir=$(dirname "$(mktemp -u)")
# only run these tests # Declare: list of vim/nvim versions
file_test="" declare -g g_vers="$(print_versions)"
# docker flags # Declare: type of tests to run - vader/vint/all
flags=(--rm -v "$PWD/../:/testplugin" -v "$PWD/../test:/home" -w /testplugin vimwiki) declare -g g_type="all"
while getopts ":hvn:lt:f:" opt; do # Declare: verbose output flag
case ${opt} in declare -g g_verbose=0
h)
printHelp # Declare: only run these tests
;; declare -g g_file_test=""
n)
vers="$OPTARG" # Declare: docker flags
;; declare -g flags=(--rm -v "$PWD/../:/testplugin" -v "$PWD/../test:/home" -w /testplugin vimwiki)
v)
verbose=1 # Parse all argument options
;; while getopts ":hvn:lt:f:" opt; do
l) case ${opt} in
print_versions h)
;; printHelp
t) ;;
type="$OPTARG" n)
;; g_vers="$OPTARG"
f) ;;
file_test="$OPTARG" v)
;; g_verbose=1
\?) ;;
echo "Invalid option: $OPTARG" 1>&2 l)
exit 1 print_versions
;; ;;
:) t)
echo "Invalid option: $OPTARG requires an argument" 1>&2 g_type="$OPTARG"
exit 1 ;;
;; f)
esac g_file_test="$OPTARG"
done ;;
\?)
# shift out processed parameters echo "Invalid option: $OPTARG" 1>&2
shift $((OPTIND -1)) exit 1
;;
# error handling for non-option arguments :)
if [[ $# -ne 0 ]]; then echo "Invalid option: $OPTARG requires an argument" 1>&2
echo "Error: Got $# non-option arguments." 1>&2 exit 1
exit 1 ;;
fi esac
done
# stop tests on ctrl-c or ctrl-z
trap exit 1 SIGINT SIGTERM # Shift out parameters already processed
shift $((OPTIND -1))
# Global error return of the script
o_error=0 # Handle error for non-option arguments
if [[ $# -ne 0 ]]; then
# Select which tests should run echo "Error: Got $# non-option arguments." 1>&2
case $type in
vader)
run_vader ; err=$?
echo "Main Vader: returned $err"
o_error=$(( err | o_error ))
;;
vint)
run_vint ; err=$?
echo "Main Vint: returned $err"
o_error=$(( err | o_error ))
;;
all)
run_vint ; err=$?
echo "Main Vint: returned $err"
o_error=$(( err | o_error ))
run_vader ; err=$?
echo "Main Vader: returned $err"
o_error=$(( err | o_error ))
;;
*)
echo "Error: invalid type - '$type'" 1>&2
exit 1 exit 1
esac fi
}
# Calcultate time
end_time=$(date +%s)
sec_time=$((end_time - start_time))
printf -v script_time '%dh:%dm:%ds' $((sec_time/3600)) $((sec_time%3600/60)) $((sec_time%60))
# Exit execute_test_argument(){
echo -ne "Script $(basename "$0"), in $script_time, Returned -> $o_error\n\n" : 'Execute test according to global variable'
exit $o_error # Global error return of the script
local -i res=0 ret=0
# Select and run tests
case $g_type in
vader)
run_vader; ret=$?
echo "Main Vader: returned $ret"
((res |= ret))
;;
vint)
run_vint; ret=$?
echo "Main Vint: returned $ret"
((res |= ret))
;;
all)
run_vint; ret=$?
echo "Main Vint: returned $ret"
((res |= ret))
run_vader; ret=$?
echo "Main Vader: returned $ret"
((res |= ret))
;;
*)
echo "Error: invalid type - '$g_type'" 1>&2
exit 1
esac
return "$res"
}
run_test(){
local -i res=0
# Hi
echo -en "Starting $(basename "$0") for VimWiki\n"
# Hook ctrl-c or ctrl-z to stop tests
trap exit 1 SIGINT SIGTERM
# For windows: Cmder bash is appending busybox to the path and
# and a smlll vim is included, so that override the windows path vim
if [[ -v OLD_PATH ]]; then
echo "Setting path from OLD_PATH : $OLD_PATH"
export PATH="$OLD_PATH"
fi
# WORK
parse_argument "$@"; ((res |= $?))
execute_test_argument; ((res |= $?))
# Print ellapsed time (after calculate it)
end_time=$(date +%s)
sec_time=$((end_time - start_time))
printf -v script_time '%dh:%dm:%ds' $((sec_time/3600)) $((sec_time%3600/60)) $((sec_time%60))
echo -ne "Script $(basename "$0"), in $script_time, Returned -> $res\n\n"
return "$res"
}
if ! (return 0 2>/dev/null); then
run_test "$@"; exit $?
fi