2021-06-26 01:05:01 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Make sure the current directory is the location of this script to simplify matters
|
|
|
|
cd "$(dirname "$(readlink -f "$0")")" || { echo "Error: Failed to cd to script directory" >&2; exit 1; };
|
|
|
|
|
2021-07-30 17:12:28 +00:00
|
|
|
# To run Luacheck:
|
|
|
|
#
|
|
|
|
# luacheck . --ignore 631 61[124] 412 21[123] --globals minetest worldedit worldeditadditions worldeditadditions_commands worldeditadditions_core vector assert bit it describe bonemeal --codes -j "$(nproc)" --quiet --exclude-files .luarocks/*
|
|
|
|
|
2021-06-26 01:05:01 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
log_msg() {
|
|
|
|
echo "[ $SECONDS ] >>> $*" >&2;
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1 - Command name to check for
|
|
|
|
check_command() {
|
|
|
|
set +e;
|
|
|
|
which $1 >/dev/null 2>&1; exit_code=$?
|
|
|
|
if [[ "${exit_code}" -ne 0 ]]; then
|
|
|
|
log_msg "Error: Couldn't locate $1. Make sure it's installed and in your path.";
|
|
|
|
fi
|
|
|
|
set -e;
|
|
|
|
}
|
|
|
|
|
2023-07-12 20:44:12 +00:00
|
|
|
# Display a url in the terminal.
|
|
|
|
# See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
|
|
|
|
# $1 - The url to link to.
|
|
|
|
# $2 - The display text to show.
|
|
|
|
display_url() {
|
|
|
|
local URL_START='\e]8;;';
|
|
|
|
#shellcheck disable=SC1003
|
|
|
|
local URL_DISPLAY_TEXT='\e\\';
|
|
|
|
#shellcheck disable=SC1003
|
|
|
|
local URL_END='\e]8;;\e\\';
|
|
|
|
|
|
|
|
url="$1";
|
|
|
|
display_text="$2";
|
|
|
|
|
|
|
|
if [[ -z "$display_text" ]]; then
|
|
|
|
display_text="${url}";
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "${URL_START}${url}${URL_DISPLAY_TEXT}${display_text}${URL_END}";
|
|
|
|
}
|
|
|
|
|
2021-06-26 01:05:01 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
check_command luarocks;
|
|
|
|
|
|
|
|
luarocks_root="${PWD}/.luarocks";
|
|
|
|
|
|
|
|
# Setup the lua module path
|
2022-05-25 01:14:17 +00:00
|
|
|
if [[ "${OSTYPE}" == *"msys"* ]]; then
|
|
|
|
PATH="$(luarocks --tree "${luarocks_root}" path --lr-bin):${PATH}";
|
|
|
|
LUA_PATH="$(luarocks --tree "${luarocks_root}" path --lr-path);init.lua;./?.lua;${LUA_PATH}";
|
|
|
|
LUA_CPATH="$(luarocks --tree "${luarocks_root}" path --lr-cpath);./?.so;${LUA_CPATH}";
|
|
|
|
else
|
|
|
|
eval "$(luarocks --tree "${luarocks_root}" path)";
|
|
|
|
fi
|
2022-05-24 23:45:37 +00:00
|
|
|
|
|
|
|
export PATH LUA_PATH LUA_CPATH;
|
2021-06-26 01:05:01 +00:00
|
|
|
|
2021-06-26 13:10:28 +00:00
|
|
|
mode="${1}"; if [[ "$#" -gt 0 ]]; then shift; fi
|
2021-06-26 01:05:01 +00:00
|
|
|
|
|
|
|
run_setup() {
|
|
|
|
log_msg "Installing busted";
|
|
|
|
luarocks --tree "${luarocks_root}" install busted;
|
2023-08-15 01:59:19 +00:00
|
|
|
log_msg "Installing lua-json";
|
|
|
|
luarocks --tree "${luarocks_root}" install lua-json;
|
|
|
|
|
2023-07-14 14:06:11 +00:00
|
|
|
if [[ "${OSTYPE}" != *"msys"* ]]; then
|
2023-08-15 01:59:19 +00:00
|
|
|
log_msg "Installing luacov";
|
2023-07-14 14:06:11 +00:00
|
|
|
luarocks --tree "${luarocks_root}" install luacov;
|
2023-08-15 01:59:19 +00:00
|
|
|
log_msg "Installing cluacov";
|
2023-07-14 14:06:11 +00:00
|
|
|
luarocks --tree "${luarocks_root}" install cluacov;
|
2023-08-15 01:59:19 +00:00
|
|
|
log_msg "Installing luacov-html";
|
2023-07-14 14:06:11 +00:00
|
|
|
luarocks --tree "${luarocks_root}" install luacov-html;
|
|
|
|
fi
|
2021-06-26 01:05:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:12:28 +00:00
|
|
|
run_syntax_check() {
|
|
|
|
find . -type f -name '*.lua' -not -path '*luarocks*' -not -path '*.git/*' -print0 | xargs -0 -n1 -P "$(nproc)" luac -p;
|
|
|
|
}
|
|
|
|
|
2021-06-26 01:05:01 +00:00
|
|
|
run_test() {
|
2023-07-12 20:44:12 +00:00
|
|
|
if [[ -r "luacov.stats.out" ]]; then rm "luacov.stats.out"; fi
|
|
|
|
if [[ -r "luacov.report.out" ]]; then rm "luacov.report.out"; fi
|
|
|
|
if [[ -d "luacov-html" ]]; then rm -r "luacov-html"; fi
|
|
|
|
|
2022-05-25 00:33:07 +00:00
|
|
|
busted_path=".luarocks/bin/busted";
|
|
|
|
if [[ ! -r "${busted_path}" ]]; then
|
|
|
|
busted_path=".luarocks/bin/busted.bat";
|
|
|
|
fi
|
|
|
|
if [[ ! -r "${busted_path}" ]]; then
|
|
|
|
echo "Error: Failed to find busted at .luarocks/bin/busted or .luarocks/bin/busted.bat" >&2;
|
|
|
|
exit 1;
|
|
|
|
fi
|
2023-07-12 20:44:12 +00:00
|
|
|
|
2023-07-14 14:06:11 +00:00
|
|
|
if [[ "${OSTYPE}" == *"msys"* ]]; then
|
|
|
|
"${busted_path}" --no-auto-insulate --pattern ".test.lua" .tests;
|
|
|
|
else
|
2023-08-14 23:06:24 +00:00
|
|
|
if [[ -r "luacov.stats.out" ]]; then rm "luacov.stats.out"; fi
|
|
|
|
|
2023-08-15 02:01:22 +00:00
|
|
|
# Delete any pre-existing coverage info from any prev runs
|
|
|
|
if [[ -d "luacov-html" ]]; then rm -r "luacov-html"; fi
|
2023-08-15 02:07:16 +00:00
|
|
|
if [[ -d ".luacov-html" ]]; then rm -r ".luacov-html"; fi
|
2023-08-15 02:01:22 +00:00
|
|
|
|
2023-08-14 23:06:24 +00:00
|
|
|
set +e;
|
2023-07-14 14:06:11 +00:00
|
|
|
"${busted_path}" --coverage --no-auto-insulate --pattern ".test.lua" .tests;
|
2023-08-14 23:06:24 +00:00
|
|
|
set -e;
|
2023-07-14 14:06:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
# If it doesn't begin with a dot, then Minetest *will* complain
|
|
|
|
if [[ -d "luacov-html" ]]; then
|
|
|
|
mv "luacov-html" ".luacov-html";
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove, but only if empty
|
|
|
|
if [[ -s "luacov.report.out" ]]; then :
|
2023-08-14 23:06:24 +00:00
|
|
|
elif [[ -e "luacov.report.out" ]]; then rm "luacov.report.out"; fi
|
2023-07-14 14:06:11 +00:00
|
|
|
|
2023-08-15 02:01:22 +00:00
|
|
|
echo -e "Output written to $(display_url "file://$PWD/.luacov-html/index.html" "./.luacov-html/index.html")";
|
2023-07-12 21:27:37 +00:00
|
|
|
fi
|
2021-06-26 01:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "${mode}" in
|
|
|
|
setup )
|
|
|
|
run_setup;
|
|
|
|
;;
|
|
|
|
|
|
|
|
run )
|
|
|
|
if [[ ! -d "${luarocks_root}" ]]; then
|
|
|
|
run_setup;
|
|
|
|
fi
|
2021-07-30 17:12:28 +00:00
|
|
|
run_syntax_check;
|
2021-06-26 01:05:01 +00:00
|
|
|
run_test;
|
|
|
|
;;
|
|
|
|
|
|
|
|
* )
|
|
|
|
echo -e "Usage:
|
|
|
|
path/to/run.sh setup # Setup to run the tests
|
|
|
|
path/to/run.sh run # Run the tests" >&2;
|
|
|
|
;;
|
|
|
|
esac
|