Use shfmt as formatter for shell scripts

This commit is contained in:
ChillerDragon 2024-05-23 08:03:03 +08:00
parent 1e11fffb03
commit c75584406a
14 changed files with 113 additions and 141 deletions

2
.editorconfig Normal file
View file

@ -0,0 +1,2 @@
[*.sh]
space_redirects = true

View file

@ -23,6 +23,8 @@ jobs:
sudo apt-get install clang-format imagemagick ddnet-tools shellcheck pkg-config cmake ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev python3-clang libvulkan-dev glslang-tools spirv-tools -y sudo apt-get install clang-format imagemagick ddnet-tools shellcheck pkg-config cmake ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev python3-clang libvulkan-dev glslang-tools spirv-tools -y
rustup default stable rustup default stable
pip3 install pylint pip3 install pylint
wget -O ~/.local/bin/shfmt https://github.com/mvdan/sh/releases/download/v3.8.0/shfmt_v3.8.0_linux_amd64
chmod +x ~/.local/bin/shfmt
git clone https://gitlab.com/Patiga/twmap.git/ git clone https://gitlab.com/Patiga/twmap.git/
cd twmap/twmap-tools cd twmap/twmap-tools
cargo install --locked --path=. cargo install --locked --path=.
@ -60,6 +62,8 @@ jobs:
# fi # fi
- name: Shellcheck - name: Shellcheck
run: find . -type f -name '*.sh' -print0 | xargs -0 shellcheck run: find . -type f -name '*.sh' -print0 | xargs -0 shellcheck
- name: Shell format (shfmt)
run: find . -type f -name '*.sh' -print0 | xargs -0 shfmt -d
- name: Check log error case - name: Check log error case
run: | run: |
if grep -Eqr '(msg|Print).*\(.*"[Ee]rror:' src/; if grep -Eqr '(msg|Print).*\(.*"[Ee]rror:' src/;

View file

@ -1,10 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
command -v a2x > /dev/null 2>&1 || { command -v a2x > /dev/null 2>&1 || {
echo >&2 "You need asciidoc installed"; echo >&2 "You need asciidoc installed"
echo >&2 "Debian/Ubuntu: sudo apt install asciidoc"; echo >&2 "Debian/Ubuntu: sudo apt install asciidoc"
echo >&2 "http://asciidoc.org/"; echo >&2 "http://asciidoc.org/"
exit 1; exit 1
} }
set -ex set -ex
@ -20,4 +20,3 @@ EOF
a2x --doctype manpage --format manpage DDNet.adoc a2x --doctype manpage --format manpage DDNet.adoc
a2x --doctype manpage --format manpage DDNetServer.adoc a2x --doctype manpage --format manpage DDNetServer.adoc

View file

@ -5,18 +5,21 @@ case "$(uname -s)" in
explorer "$APPDATA/DDNet/" explorer "$APPDATA/DDNet/"
else else
explorer "$APPDATA/Teeworlds/" explorer "$APPDATA/Teeworlds/"
fi;; fi
;;
Darwin*) Darwin*)
if [ -d "$HOME/Library/Application Support/DDNet/" ]; then if [ -d "$HOME/Library/Application Support/DDNet/" ]; then
open "$HOME/Library/Application Support/DDNet/" open "$HOME/Library/Application Support/DDNet/"
else else
open "$HOME/Library/Application Support/Teeworlds/" open "$HOME/Library/Application Support/Teeworlds/"
fi;; fi
;;
*) *)
DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
if [ -d "$DATA_HOME/ddnet/" ]; then if [ -d "$DATA_HOME/ddnet/" ]; then
xdg-open "$DATA_HOME/ddnet/" xdg-open "$DATA_HOME/ddnet/"
else else
xdg-open "$HOME/.teeworlds/" xdg-open "$HOME/.teeworlds/"
fi;; fi
;;
esac esac

View file

@ -78,7 +78,6 @@ if [[ -z ${GE_NO_APK_BUILD} || "${GE_NO_APK_BUILD}" != "1" ]]; then
fi fi
cp build/outputs/apk/"$_RELEASE_TYPE_NAME"/"$_APK_BASENAME"-"$_RELEASE_TYPE_NAME""$_RELEASE_TYPE_APK_NAME".apk "$_APK_BASENAME".apk cp build/outputs/apk/"$_RELEASE_TYPE_NAME"/"$_APK_BASENAME"-"$_RELEASE_TYPE_NAME""$_RELEASE_TYPE_APK_NAME".apk "$_APK_BASENAME".apk
if [[ "${_APK_BUILD_TYPE}" == "Release" ]]; then if [[ "${_APK_BUILD_TYPE}" == "Release" ]]; then
java "-Dorg.gradle.appname=${APP_BASE_NAME}" -classpath "${CLASSPATH}" org.gradle.wrapper.GradleWrapperMain --warning-mode all bundleRelease java "-Dorg.gradle.appname=${APP_BASE_NAME}" -classpath "${CLASSPATH}" org.gradle.wrapper.GradleWrapperMain --warning-mode all bundleRelease

View file

@ -5,7 +5,7 @@ c_headers=(assert complex ctype errno fenv float inttypes iso646 limits locale m
c_headers_map=(cassert complex cctype cerrno cfenv cfloat cinttypes ciso646 climits clocale cmath csetjmp csignal cstdarg cstdbool cstddef cstdint cstdio cstdlib cstring ctgmath ctime cwchar cwctype) c_headers_map=(cassert complex cctype cerrno cfenv cfloat cinttypes ciso646 climits clocale cmath csetjmp csignal cstdarg cstdbool cstddef cstdint cstdio cstdlib cstring ctgmath ctime cwchar cwctype)
# Create regex dynamically from the array to match any C header # Create regex dynamically from the array to match any C header
c_headers_regex=$(IFS="|"; echo "${c_headers[*]}") c_headers_regex=$(printf "%s" "${c_headers[*]}" | tr ' ' '|')
# Find all C++ source and header files # Find all C++ source and header files
files=$(find ./src -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.h' \) ! -path "./src/engine/external/*") files=$(find ./src -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.h' \) ! -path "./src/engine/external/*")

View file

@ -77,4 +77,3 @@ function compile_all_opusfile() {
} }
compile_all_opusfile "$1" "$2" compile_all_opusfile "$1" "$2"

View file

@ -62,4 +62,3 @@ function compile_all_sqlite3() {
} }
compile_all_sqlite3 "$1" "$2" compile_all_sqlite3 "$1" "$2"

View file

@ -3,10 +3,8 @@
arg_verbose=0 arg_verbose=0
arg_valgrind_memcheck=0 arg_valgrind_memcheck=0
for arg in "$@" for arg in "$@"; do
do if [ "$arg" == "-h" ] || [ "$arg" == "--help" ]; then
if [ "$arg" == "-h" ] || [ "$arg" == "--help" ]
then
echo "usage: $(basename "$0") [OPTION..]" echo "usage: $(basename "$0") [OPTION..]"
echo "description:" echo "description:"
echo " Runs a simple integration test of the client and server" echo " Runs a simple integration test of the client and server"
@ -16,11 +14,9 @@ do
echo " --verbose|-v verbose output" echo " --verbose|-v verbose output"
echo " --valgrind-memcheck use valgrind's memcheck to run server and client" echo " --valgrind-memcheck use valgrind's memcheck to run server and client"
exit 0 exit 0
elif [ "$arg" == "-v" ] || [ "$arg" == "--verbose" ] elif [ "$arg" == "-v" ] || [ "$arg" == "--verbose" ]; then
then
arg_verbose=1 arg_verbose=1
elif [ "$arg" == "--valgrind-memcheck" ] elif [ "$arg" == "--valgrind-memcheck" ]; then
then
arg_valgrind_memcheck=1 arg_valgrind_memcheck=1
else else
echo "Error: unknown argument '$arg'" echo "Error: unknown argument '$arg'"
@ -28,13 +24,11 @@ do
fi fi
done done
if [ ! -f DDNet ] if [ ! -f DDNet ]; then
then
echo "[-] Error: client binary 'DDNet' not found" echo "[-] Error: client binary 'DDNet' not found"
exit 1 exit 1
fi fi
if [ ! -f DDNet-Server ] if [ ! -f DDNet-Server ]; then
then
echo "[-] Error: server binary 'DDNet-Server' not found" echo "[-] Error: server binary 'DDNet-Server' not found"
exit 1 exit 1
fi fi
@ -44,30 +38,25 @@ got_killed=0
function kill_all() { function kill_all() {
# needed to fix hang fifo with additional ctrl+c # needed to fix hang fifo with additional ctrl+c
if [ "$got_killed" == "1" ] if [ "$got_killed" == "1" ]; then
then
exit exit
fi fi
got_killed=1 got_killed=1
if [ "$arg_verbose" == "1" ] if [ "$arg_verbose" == "1" ]; then
then
echo "[*] Shutting down test clients and server" echo "[*] Shutting down test clients and server"
fi fi
sleep 1 sleep 1
if [[ ! -f fail_server.txt ]] if [[ ! -f fail_server.txt ]]; then
then
echo "[*] Shutting down server" echo "[*] Shutting down server"
echo "shutdown" > server.fifo echo "shutdown" > server.fifo
fi fi
sleep 1 sleep 1
local i local i
for ((i=1;i<3;i++)) for ((i = 1; i < 3; i++)); do
do if [[ ! -f fail_client$i.txt ]]; then
if [[ ! -f fail_client$i.txt ]]
then
echo "[*] Shutting down client$i" echo "[*] Shutting down client$i"
echo "quit" > "client$i.fifo" echo "quit" > "client$i.fifo"
fi fi
@ -81,8 +70,7 @@ function cleanup() {
trap cleanup EXIT trap cleanup EXIT
function fail() function fail() {
{
sleep 1 sleep 1
tail -n2 "$1".log > fail_"$1".txt tail -n2 "$1".log > fail_"$1".txt
echo "$1 exited with code $2" >> fail_"$1".txt echo "$1 exited with code $2" >> fail_"$1".txt
@ -90,7 +78,7 @@ function fail()
} }
# Get unused port from the system by binding to port 0 and immediately closing the socket again # Get unused port from the system by binding to port 0 and immediately closing the socket again
port=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'); port=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
if [[ $OSTYPE == 'darwin'* ]]; then if [[ $OSTYPE == 'darwin'* ]]; then
DETECT_LEAKS=0 DETECT_LEAKS=0
@ -113,8 +101,7 @@ function print_results() {
return 1 return 1
fi fi
else else
if test -n "$(find . -maxdepth 1 -name 'SAN.*' -print -quit)" if test -n "$(find . -maxdepth 1 -name 'SAN.*' -print -quit)"; then
then
echo "[-] Error: ASAN has detected the following errors:" echo "[-] Error: ASAN has detected the following errors:"
cat SAN.* cat SAN.*
return 1 return 1
@ -153,15 +140,12 @@ function wait_for_fifo() {
# give the server/client time to launch and create the fifo file # give the server/client time to launch and create the fifo file
# but assume after X secs that the server/client crashed before # but assume after X secs that the server/client crashed before
# being able to create the file # being able to create the file
while [[ ! -p "$fifo" ]] while [[ ! -p "$fifo" ]]; do
do
fails="$((fails + 1))" fails="$((fails + 1))"
if [ "$arg_verbose" == "1" ] if [ "$arg_verbose" == "1" ]; then
then
echo "[!] Note: $fifo not found (attempts $fails/$tries)" echo "[!] Note: $fifo not found (attempts $fails/$tries)"
fi fi
if [ "$fails" -gt "$tries" ] if [ "$fails" -gt "$tries" ]; then
then
echo "[-] Error: $(basename "$fifo" .fifo) possibly crashed on launch" echo "[-] Error: $(basename "$fifo" .fifo) possibly crashed on launch"
kill_all kill_all
print_results print_results
@ -282,34 +266,29 @@ fi
# Kill all processes first so all outputs are fully written # Kill all processes first so all outputs are fully written
kill_all kill_all
if ! grep -qE '^[0-9]{4}-[0-9]{2}-[0-9]{2} ([0-9]{2}:){2}[0-9]{2} I chat: 0:-2:client1: hello world$' server.log if ! grep -qE '^[0-9]{4}-[0-9]{2}-[0-9]{2} ([0-9]{2}:){2}[0-9]{2} I chat: 0:-2:client1: hello world$' server.log; then
then
touch fail_chat.txt touch fail_chat.txt
echo "[-] Error: chat message not found in server log" echo "[-] Error: chat message not found in server log"
fi fi
if ! grep -q 'cmdlist' client1.log || \ if ! grep -q 'cmdlist' client1.log ||
! grep -q 'pause' client1.log || \ ! grep -q 'pause' client1.log ||
! grep -q 'rank' client1.log || \ ! grep -q 'rank' client1.log ||
! grep -q 'points' client1.log ! grep -q 'points' client1.log; then
then
touch fail_chatcommand.txt touch fail_chatcommand.txt
echo "[-] Error: did not find output of /cmdlist command" echo "[-] Error: did not find output of /cmdlist command"
fi fi
if ! grep -q "hello from admin" server.log if ! grep -q "hello from admin" server.log; then
then
touch fail_rcon.txt touch fail_rcon.txt
echo "[-] Error: admin message not found in server log" echo "[-] Error: admin message not found in server log"
fi fi
if ! grep -q "demo_player: Stopped playback" client1.log if ! grep -q "demo_player: Stopped playback" client1.log; then
then
touch fail_demo_server.txt touch fail_demo_server.txt
echo "[-] Error: demo playback of server demo in client 1 was not started/finished" echo "[-] Error: demo playback of server demo in client 1 was not started/finished"
fi fi
if ! grep -q "demo_player: Stopped playback" client2.log if ! grep -q "demo_player: Stopped playback" client2.log; then
then
touch fail_demo_client.txt touch fail_demo_client.txt
echo "[-] Error: demo playback of client demo in client 2 was not started/finished" echo "[-] Error: demo playback of client demo in client 2 was not started/finished"
fi fi
@ -324,33 +303,27 @@ player: client1 time: 168300.5 cps: 0.02 0.06 0.12 15300.14 15300.18 30600.2 306
player: client2 time: 302.02 cps: 0.42 0.5 0.0 0.66 0.92 0.02 300.18 300.46 300.76 300.88 300.98 301.16 301.24 301.28 301.3 301.86 301.96 0.0 0.0 0.0 0.0 0.0 0.0" player: client2 time: 302.02 cps: 0.42 0.5 0.0 0.66 0.92 0.02 300.18 300.46 300.76 300.88 300.98 301.16 301.24 301.28 301.3 301.86 301.96 0.0 0.0 0.0 0.0 0.0 0.0"
# require at least one rank in all cases. Exact finishes only with valgrind disabled # require at least one rank in all cases. Exact finishes only with valgrind disabled
if [ "$ranks" == "" ] if [ "$ranks" == "" ]; then
then
touch fail_ranks.txt touch fail_ranks.txt
echo "[-] Error: no ranks found in database" echo "[-] Error: no ranks found in database"
elif [ "$arg_valgrind_memcheck" != "1" ] && [ "$rank_time" != "$expected_times" ] elif [ "$arg_valgrind_memcheck" != "1" ] && [ "$rank_time" != "$expected_times" ]; then
then
touch fail_ranks.txt touch fail_ranks.txt
echo "[-] Error: unexpected finish time" echo "[-] Error: unexpected finish time"
echo " expected: $expected_times" echo " expected: $expected_times"
echo " got: $rank_time" echo " got: $rank_time"
fi fi
for logfile in client1.log client2.log server.log for logfile in client1.log client2.log server.log; do
do if [ "$arg_valgrind_memcheck" == "1" ]; then
if [ "$arg_valgrind_memcheck" == "1" ]
then
break break
fi fi
if [ ! -f "$logfile" ] if [ ! -f "$logfile" ]; then
then
echo "[-] Error: logfile '$logfile' not found" echo "[-] Error: logfile '$logfile' not found"
touch fail_logs.txt touch fail_logs.txt
continue continue
fi fi
logdiff="$(diff -u <(grep -v "console: .* access for .* is now .*abled" "$logfile" | sort) <(sort "stdout_$(basename "$logfile" .log).txt"))" logdiff="$(diff -u <(grep -v "console: .* access for .* is now .*abled" "$logfile" | sort) <(sort "stdout_$(basename "$logfile" .log).txt"))"
if [ "$logdiff" != "" ] if [ "$logdiff" != "" ]; then
then
echo "[-] Error: logfile '$logfile' differs from stdout" echo "[-] Error: logfile '$logfile' differs from stdout"
echo "$logdiff" echo "$logdiff"
echo "[-] Error: logfile '$logfile' differs from stdout" >> fail_logs.txt echo "[-] Error: logfile '$logfile' differs from stdout" >> fail_logs.txt
@ -358,24 +331,19 @@ do
fi fi
done done
for stderr in ./stderr_*.txt for stderr in ./stderr_*.txt; do
do if [ ! -f "$stderr" ]; then
if [ ! -f "$stderr" ]
then
continue continue
fi fi
if [ "$(cat "$stderr")" == "" ] if [ "$(cat "$stderr")" == "" ]; then
then
continue continue
fi fi
echo "[!] Warning: $stderr" echo "[!] Warning: $stderr"
cat "$stderr" cat "$stderr"
done done
if test -n "$(find . -maxdepth 1 -name 'fail_*' -print -quit)" if test -n "$(find . -maxdepth 1 -name 'fail_*' -print -quit)"; then
then for fail in fail_*; do
for fail in fail_*
do
cat "$fail" cat "$fail"
done done
print_results print_results

View file

@ -46,8 +46,7 @@ function prine_line_for_address() {
} }
ADDR_PC_REGEX='[0-9A-Fa-f]+ [0-9A-Fa-f]+ [0-9A-Fa-f]+ [0-9A-Fa-f]+' ADDR_PC_REGEX='[0-9A-Fa-f]+ [0-9A-Fa-f]+ [0-9A-Fa-f]+ [0-9A-Fa-f]+'
while read -r line while read -r line; do
do
if [[ $line =~ $ADDR_PC_REGEX ]]; then if [[ $line =~ $ADDR_PC_REGEX ]]; then
# Check for main executable file with address information # Check for main executable file with address information
EXE_FILE_RELATIVE_ADDR=$(echo "$line" | grep -E -o -m 1 "\s${EXE_FILE_FILENAME_REGEX}!.*0x[0-9A-Fa-f]+" | grep -E -o "0x[0-9A-Fa-f]+" | head -1) EXE_FILE_RELATIVE_ADDR=$(echo "$line" | grep -E -o -m 1 "\s${EXE_FILE_FILENAME_REGEX}!.*0x[0-9A-Fa-f]+" | grep -E -o "0x[0-9A-Fa-f]+" | head -1)