computable-pandoc/scripts/test.sh

72 lines
1.8 KiB
Bash
Raw Normal View History

2023-03-09 15:58:14 -06:00
# Makes tests
2023-02-16 19:04:01 -06:00
# Variables
2023-03-16 09:52:04 -06:00
FILTER=dist/lin.lua
2023-03-15 16:12:48 -06:00
FILES="tests/*.md"
2023-03-20 19:28:32 -06:00
VERBOSE=""
2023-03-16 18:00:24 -06:00
AST=false
2023-03-15 16:12:48 -06:00
CMD="sh $0"
2023-02-16 19:04:01 -06:00
2023-03-15 16:12:48 -06:00
# Prints help
echo_help () {
echo "Usage:"
2023-03-16 18:00:24 -06:00
echo " $CMD [-vah] [FILES]"
2023-03-15 16:12:48 -06:00
echo "Examples:"
2023-03-16 18:00:24 -06:00
echo " $CMD Tests for 'tests/*.md'"
echo " $CMD -v Verbose tests for 'tests/*.md'"
echo " $CMD -a Tests and AST for 'tests/*.md'"
echo " $CMD tests/fail* Tests for 'tests/fail*'"
echo " $CMD -va tests/fail* Verbose tests and AST for 'tests/fail*'"
echo " $CMD -h Display this help"
2023-03-15 16:12:48 -06:00
exit
}
# Obtains result as "pass" | "fail" | "diff" (AST doesn't match)
get_result () {
2023-03-15 17:33:53 -06:00
result=$([[ $1 -ne 0 ]] && echo "fail" || echo "pass")
2023-03-15 16:12:48 -06:00
if [ -f $2.json ]; then
diff1=$(cat tmp.json | jq)
diff2=$(cat $2.json | jq)
difference=$(diff <( printf '%s\n' "$diff1" ) <( printf '%s\n' "$diff2" ))
result=$([[ $? -eq 0 ]] && echo "pass" || echo "diff")
fi
echo $result
}
# Checks options
2023-03-16 18:00:24 -06:00
while getopts ':vah' opt; do
2023-03-15 16:12:48 -06:00
case "$opt" in
2023-03-20 19:28:32 -06:00
v) VERBOSE="--verbose" ; shift ;;
2023-03-16 18:00:24 -06:00
a) AST=true ; shift ;;
2023-03-15 16:12:48 -06:00
h) echo_help ;;
2023-03-16 18:00:24 -06:00
?) echo "ERROR: only -v, -a or -h is allowed" ; exit 1 ;;
2023-03-15 16:12:48 -06:00
esac
done
# Checks for files
if [ $# -ne 0 ]; then
for file in $*; do
if [ ! -f $file ]; then
echo "ERROR: '$file' is not a file; do '$CMD -h' for more info" ; exit 1
fi
done
FILES=$*
2023-02-16 19:04:01 -06:00
fi
2023-03-11 14:14:24 -06:00
# Makes distribution bundle
sh scripts/make_dist.sh
2023-02-16 19:04:01 -06:00
# Does tests
2023-03-10 21:09:31 -06:00
clear && echo "🐾 Starting tests"
2023-03-15 17:33:53 -06:00
for file in $FILES; do
echo "⚗️ $file:"
expectation=${file:6:4}
2023-03-20 19:28:32 -06:00
ast=$(pandoc $VERBOSE -L $FILTER -t json -o tmp.json $file)
2023-03-15 17:33:53 -06:00
result=$(get_result $? $file)
2023-03-15 16:12:48 -06:00
echo " Expect: $expectation"
echo " Result: $result"
2023-03-20 19:28:32 -06:00
[ "$VERBOSE" = "--verbose" ] && echo -e "$ast"
2023-03-16 18:00:24 -06:00
[ "$AST" = true ] && pandoc -L $FILTER -t native $file
2023-03-15 16:12:48 -06:00
rm tmp.json
2023-02-16 19:04:01 -06:00
done