[Main] Options added, test group features added
[platform/upstream/SSAT.git] / ssat.sh
1 #!/usr/bin/env bash
2 ##
3 # @file ssat.sh
4 # @author MyungJoo Ham <myungjoo.ham@gmail.com>
5 # @date Jun 22 2018
6 # @license Apache-2.0
7 # @brief This executes test groups and reports aggregated test results.
8 # @exit 0 if all PASSED. Positive if some FAILED.
9 # @dependency sed
10 #
11 # If there is no arguments specified, this will search for all "runTest.sh" in
12 # the subdirectory of this file and regard them as the test groups.
13 #
14 # If a testgroup (runTest.sh) returns 0 while there are failed testcase,
15 # it implies that the failed testcases may be ignored and it's good to go.
16 #
17 # If --help or -h is given, this will show detailed description.
18
19 TARGET=$(pwd)
20 BASEPATH=`dirname "$0"`
21 BASENAME=`basename "$0"`
22 TESTCASE="runTest.sh"
23
24 #
25 SILENT=1
26
27 # Handle arguments
28 POSITIONAL=()
29 while [[ $# -gt 0 ]]
30 do
31         key="$1"
32         case $key in
33         -h|--help)
34                 printf "usage: ${BASENAME} [--help] [<path>] [--testcase <filename>] [--nocolor] [--showstdout]\n\n"
35                 printf "These are common ${Red}ssat${NC} commands used:\n\n"
36                 printf "Test all test-groups in the current ($(pwd)) directory, recursively\n"
37                 printf "    (no options specified)\n"
38                 printf "    $ ${BASENAME}\n"
39                 printf "\n"
40                 printf "Test all test-groups in the specified directory, recursively\n"
41                 printf "    <path>\n"
42                 printf "    $ ${BASENAME} /home/username/test\n"
43                 printf "    If there are multiple paths, the last one will be used\n"
44                 printf "\n"
45                 printf "Search for \"filename\" as the testcase scripts\n"
46                 printf "    --testcase or -t\n"
47                 printf "    $ ${BASENAME} --testcase cases.sh\n"
48                 printf "    Search for cases.sh instead of runTest.sh\n"
49                 printf "\n"
50                 printf "Do not emit colored text\n"
51                 printf "    --nocolor or -n\n"
52                 printf "\n"
53                 printf "Show stdout of test cases\n"
54                 printf "    --showstdout or -s\n"
55                 printf "\n"
56                 printf "Shows this message\n"
57                 printf "    --help or -h\n"
58                 printf "    $ ${BASENAME} --help \n"
59                 printf "\n\n"
60                 exit 0
61         ;;
62         -n|--nocolor)
63         nocolor=1
64         shift
65         ;;
66         -t|--testcase)
67         TESTCASE="$2"
68         shift
69         shift
70         ;;
71         -s|--showstdout)
72         SILENT=0
73         shift
74         ;;
75         *) # Unknown, which is probably target (the path to root-dir of test groups).
76         TARGET="$1"
77         esac
78 done
79
80 source ${BASEPATH}/ssat-api.sh
81
82 if [[ "${#TARGET}" -eq "0" ]]
83 then
84         TARGET="."
85 fi
86
87 TNtc=0
88 TNtcpass=0
89 TNtcfail=0
90 TNgroup=0
91 TNgrouppass=0
92 TNgroupfail=0
93 log=""
94 groupLog=""
95
96 while read -d $'\0' file
97 do
98         CASEBASEPATH=`dirname "$file"`
99         CASENAME=`basename "$CASEBASEPATH"`
100         Ntc=0
101         Npass=0
102         Nfail=0
103         tmpfile=$(mktemp)
104
105         pushd $CASEBASEPATH > /dev/null
106         output=$(. $file)
107         retcode=$?
108         if [[ "${SILENT}" -eq "0" ]]
109         then
110                 printf "${output}" | sed '$d'
111         fi
112         popd > /dev/null
113
114         logfile="${output##*$'\n'}"
115
116         resultlog=$(<$logfile)
117         effectiveOutput=`printf "$resultlog" | sed '$d'`
118         log="$log$effectiveOutput\n"
119
120         lastline=`printf "${resultlog}" | sed '$!d'`
121         IFS=/
122         set $lastline
123         Ntc=$1
124         Npass=$2
125         Nfail=$3
126
127         TNtc=$((TNtc+Ntc))
128         TNtcpass=$((TNtcpass+Npass))
129         TNtcfail=$((TNtcfail+Nfail))
130
131         TNgroup=$((TNgroup+1))
132         if [[ "$retcode" -eq "0" ]]
133         then
134                 TNgrouppass=$((TNgrouppass+1))
135                 groupLog="${groupLog}${LightGreen}[PASSED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
136         else
137                 TNgroupfail=$((TNgroupfail+1))
138                 groupLog="${groupLog}${Red}[FAILED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
139         fi
140
141 done < <(find $TARGET -name $TESTCASE -print0)
142
143 printf "\n\n==================================================\n\n"
144
145 printf "$log\n"
146 printf "==================================================\n\n"
147 printf "$groupLog"
148 printf "==================================================\n"
149
150 if (( ${TNgroupfail} == 0 ))
151 then
152         printf "${LightGreen}[PASSED] ${Blue}All Test Groups (${TNgroup}) Passed!${NC}\n\n"
153         exit 0
154 else
155         printf "${Red}[FAILED] ${Purple}There are failed test groups! (${TNgroupfail})${NC}\n\n"
156         exit 1
157 fi
158 # gather reports & publish them.