#!/bin/sh
set -e

if [ -n "${AUTOPKGTEST_TMP}" ]; then
	#FIXME: the python tests (probably parso) need a writeable home
	export HOME="${AUTOPKGTEST_TMP}/home"
	mkdir -p "$HOME"
fi

TEST_DIR='ycmd/tests'

run_unittest() {
	PYTHONPATH=/usr/lib/ycmd/ python3 -m unittest "$@" < /dev/null
}

run_all_tests_except() {
	local ARGS=''
	local LINE
	while read LINE; do
		if [ -z "$LINE" ] || [ "$(echo "$LINE" | cut -c 1)" = '#' ]; then continue; fi
		ARGS="${ARGS} ${TEST_DIR}/${LINE}"
	done <<EOF
$({ cat; cd "$TEST_DIR"; find -name '*_test.py'; } | sed -e "s#^\./##" | sort | uniq -u)
EOF
	run_unittest ${ARGS}
}
run_given_tests() {
	local ARGS=''
	local LINE
	while read LINE; do
		if [ -z "$LINE" ] || [ "$(echo "$LINE" | cut -c 1)" = '#' ]; then continue; fi
		LINE="${LINE%% #*}"
		if  [ -n "${LINE##*_test.py}" ]; then
			ARGS="${ARGS} ${TEST_DIR}/${LINE}/*_test.py"
		else
			ARGS="${ARGS} ${TEST_DIR}/${LINE}"
		fi
	done
	run_unittest ${ARGS}
}

case "$(basename "$0")" in
'upstream-unittest')
	run_all_tests_except <<EOF
# language-server not packaged
cs/debug_info_test.py
cs/diagnostics_test.py
cs/get_completions_test.py
cs/signature_help_test.py
cs/subcommands_test.py
java/debug_info_test.py
java/diagnostics_test.py
java/get_completions_test.py
java/java_completer_test.py
java/server_management_test.py
java/signature_help_test.py
java/subcommands_test.py
rust/debug_info_test.py
rust/diagnostics_test.py
rust/get_completions_proc_macro_test.py
rust/get_completions_test.py
rust/rust_completer_test.py
rust/server_management_test.py
rust/signature_help_test.py
rust/subcommands_test.py
tern/debug_info_test.py
tern/event_notification_test.py
tern/get_completions_test.py
tern/subcommands_test.py
language_server/generic_completer_test.py
# run as its own test later on
javascript/debug_info_test.py
javascript/diagnostics_test.py
javascript/get_completions_test.py
javascript/subcommands_test.py
javascriptreact/get_completions_test.py
javascriptreact/subcommands_test.py
typescript/debug_info_test.py
typescript/diagnostics_test.py
typescript/event_notification_test.py
typescript/get_completions_test.py
typescript/signature_help_test.py
typescript/subcommands_test.py
typescript/typescript_completer_test.py
shutdown_test.py
EOF
;;
'upstream-unittest-golang')
	run_given_tests << EOF
# tested above with gccgo, now with golang
go/debug_info_test.py
go/diagnostics_test.py
go/get_completions_test.py
go/go_completer_test.py
go/server_management_test.py
go/signature_help_test.py
go/subcommands_test.py
EOF
;;
'upstream-unittest-node')
	run_given_tests << EOF
javascript/debug_info_test.py
javascript/diagnostics_test.py
javascript/get_completions_test.py
javascript/subcommands_test.py
javascriptreact/get_completions_test.py
javascriptreact/subcommands_test.py
typescript/debug_info_test.py
typescript/diagnostics_test.py
typescript/event_notification_test.py
typescript/get_completions_test.py
typescript/signature_help_test.py
typescript/subcommands_test.py
typescript/typescript_completer_test.py
shutdown_test.py
EOF
;;
'upstream-unittest-flaky')
	run_given_tests << EOF
# tested above already, but this time the unversioned clangd is used which breaks some tests
# as upstream hardcodes exact behaviour of the version they embedded
clangd
EOF
;;
*)
	echo 'ERROR: Test name "$0" is unknown.'
	exit 1
;;
esac
