Skip to content

Instantly share code, notes, and snippets.

@misebox
Created May 18, 2020 04:24
Show Gist options
  • Save misebox/c2c03121b675166cfa2efe6bae82707a to your computer and use it in GitHub Desktop.
Save misebox/c2c03121b675166cfa2efe6bae82707a to your computer and use it in GitHub Desktop.
Simple I/O test manager written in shellscript
#!/bin/bash
set -eu
test_prefix="tests"
function name_cmd () {
local grp=$1
echo "./${test_prefix}/cmd_${grp}"
}
function name_test () {
local grp=$1
local no=$2
echo "./${test_prefix}/testcase_${grp}_${no}"
}
function exec_test () {
local grp=$1
local no=$2
local testdata="./$test_prefix/tmp_testdata"
local expected="./$test_prefix/tmp_expected"
echo -n >$testdata
echo -n >$expected
b=true
while read line
do
if [ "$line" = "" ];then b=false; continue; fi
$b && echo $line >>$testdata || echo $line >>$expected
done < `name_test $grp $no`
# check
local cmd=$(cat `name_cmd $grp`)
local result="$( cat $testdata | $cmd )"
echo -en "TEST \033[04m$grp-$no\033[00m $cmd .. "
if [ "$result" = "`cat $expected`" ]
then
echo -e "\033[32m[PASS]\033[00m"
else
echo -e "\033[31m[FAILED]\033[00m"
echo -e "\033[33m[TEST DATA]\033[00m"
cat $testdata
echo "[EXPECT]"
cat $expected
echo "[ACTUAL RESULT]"
echo "$result"
fi
}
if [ $# -eq 1 ]
then
grp=$1
echo Run Tests in $grp
grp_prefix="testcase_${grp}_"
for f in $(find "./$test_prefix" -name "${grp_prefix}*")
do
no=${f#./$test_prefix/$grp_prefix}
exec_test $grp $no
done
elif [ $# -eq 2 ]
then
exec_test $1 $2
elif [ $# -ge 3 ] && [ $1 = "new" ]
then
grp=$2
no=$3
shift 3
cmd=$*
if [ "$cmd" = "" ]
then
cmd="python ${grp}.py"
fi
if [ ! -d "./$test_prefix" ]
then
mkdir "./$test_prefix"
fi
echo "Register Test [$grp - $no]"
f=`name_test $grp $no`
echo "Please input test data"
while read line
do
if [ "$line" = "" ]
then
break
fi
echo $line
done > $f
echo >> $f
echo "Please input expected result"
while read line
do
if [ "$line" = "" ]
then
break
fi
echo $line
done >> $f
echo "$cmd" > `name_cmd $grp`
echo "Test [$grp - $no] has been registered."
else
echo
echo "Usage:"
echo -e "\t$ ut A # Run tests in group [A]"
echo -e "\t$ ut A B # Run a test [A-B]"
echo -e "\t$ ut new A B [cmd] # Register a test [A-B] for a command [cmd]"
echo -e "\t$ ut new A B # Register a test [A-B] for python A.py"
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment