Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtests: judging the -f file type to avoid misuse by customers #100

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions runtests
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ source .env

START_TIME=""
SUMMRY_LOG=""
TESTS_CLIENT="tests-client"
TESTS_SERVER="tests-server"
TEST_FILES=""
SUB_FOLDERS=""

usage() {
cat << _EOF
Expand All @@ -17,6 +21,8 @@ Usage: ${0##*/} [-f CMDFILES] [-c CMDLINE] [-o LOGFILE]
Examples:
./runtests -f cet/tests
./runtests -f cet/tests -o cet_tests.log
./runtests -f tests-client // exeucte tests-client/tests for all subfolders
./runtests -f tests-server // exeucte tests-server/tests for all subfolders
./runtests -c ./cet/quick_test -o cet_quick_test.log
_EOF
}
Expand Down Expand Up @@ -93,6 +99,7 @@ runcmdfile() {
local cmdfile=$1
local logfile=$2
local subfolder=""
local file_type=""

if [[ "$cmdfile" == *"/"* ]]; then
subfolder=${cmdfile%/*}
Expand All @@ -109,6 +116,48 @@ runcmdfile() {
done < "$cmdfile"
}

prepare_files_list() {
local test_file_type=$1
local sub_folder=""

for sub_folder in $SUB_FOLDERS; do
# Remov the / in the end
sub_folder="${sub_folder%/}"
[[ "$sub_folder" == "common" ]] && continue

if [[ -e "${sub_folder}/${test_file_type}" ]]; then
if [[ -z "$TEST_FILES" ]]; then
TEST_FILES="${sub_folder}/${test_file_type}"
else
TEST_FILES="${TEST_FILES},${sub_folder}/${test_file_type}"
fi
elif [[ -e "${sub_folder}/tests" ]]; then
if [[ -z "$TEST_FILES" ]]; then
TEST_FILES="${sub_folder}/tests"
else
TEST_FILES="${TEST_FILES},${sub_folder}/tests"
fi
else
echo "WARNING: No $test_file_type or tests file under $sub_folder folder."
fi
done
}

test_all() {
local test_file_type=$1

# Don't quote */, otherwise it could not list all folders.
SUB_FOLDERS=$(ls -1 -d */)

prepare_files_list "$test_file_type"
CMDFILES="$TEST_FILES"
if [[ -z "$LOGFILE" ]]; then
echo "Test files: $CMDFILES"
else
echo "Test files: $CMDFILES" | tee "$LOGFILE"
fi
}

: LOGFILE=""
: CMDFILES=""
: CMDLINE=""
Expand All @@ -120,6 +169,8 @@ while getopts ":o:f:c:h" opt; do
;;
f)
CMDFILES=$OPTARG
[[ "$CMDFILES" == "$TESTS_CLIENT" ]] && test_all "$TESTS_CLIENT"
[[ "$CMDFILES" == "$TESTS_SERVER" ]] && test_all "$TESTS_SERVER"
;;
c)
CMDLINE=$OPTARG
Expand Down Expand Up @@ -158,6 +209,18 @@ for cmdfile in $(tr "," " " <<< "$CMDFILES"); do
echo "WARNING: $cmdfile not found!"
continue
fi

[[ $(which file) ]] && {
file_type=""
file_type=$(file "$cmdfile")
[[ "$file_type" == *"text"* ]] || {
echo "WARNING: -f $cmdfile is not a text type file, it's real type:|$file_type|"
echo "Please choose the correct tests file."
usage
continue
}
}

runcmdfile "$cmdfile" "$LOGFILE"
done

Expand Down