Fixed unit tests
[situare] / scripts / run_unit_tests.sh
1 #!/bin/bash
2
3 clear
4
5 # set default values
6 UNIT_TESTS_ROOT_DIR="../tests"
7 REPORT_PATH="../doc"
8 REPORT_FILE="unit_test_results.txt"
9 TEMP_FILE_EXEC_NOT_FOUND="temp_exec_not_found.txt"
10 TEMP_REPORT_FILE="temp_report.txt"
11
12 # check that directories are found
13 if [[ -d $REPORT_PATH && -d $UNIT_TESTS_ROOT_DIR ]]; then
14     # convert relational paths to absolute ones
15     UNIT_TESTS_ROOT_DIR_ABSOLUTE=`cd $UNIT_TESTS_ROOT_DIR; pwd`
16     REPORT_PATH_ABSOLUTE=`cd $REPORT_PATH; pwd`
17     REPORT="$REPORT_PATH_ABSOLUTE/$REPORT_FILE"
18     echo ""
19     echo "Running recursively all tests under $UNIT_TESTS_ROOT_DIR_ABSOLUTE"
20     echo "Saving results to $REPORT"
21     echo ""
22
23     # overwrite report summary file and write header
24     echo "##########################################" > $REPORT
25     echo "# Summary of unit tests executed" >> $REPORT
26     echo "# Date: `date`" >> $REPORT
27     echo "# User: `whoami`" >> $REPORT
28     echo "##########################################" >> $REPORT
29     echo "" >> $REPORT
30
31     # find all test .pro files pahts, cut .pro extension
32     UNIT_TEST_PROJECTS=(`find $UNIT_TESTS_ROOT_DIR_ABSOLUTE | egrep .pro$ | sed -e s/.pro//g`)
33
34     # run tests
35     for project in "${UNIT_TEST_PROJECTS[@]}"
36     do
37         # do not try to test the root tests.pro project
38         if [ $project != "$UNIT_TESTS_ROOT_DIR_ABSOLUTE/tests" ]; then
39             # check if unit test file is found and is executable
40             if [ -x $project ]; then
41                 echo ""
42                 # run unit test, save to temp file, ignore strerr stream
43                 $project -silent -o $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE 2> /dev/null
44                 # print to screen and append to summary
45                 cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE
46                 cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
47                 echo "" >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
48             else
49                 # save path of missing test to temporary file
50                 missing=(`echo $project | sed -e s,$UNIT_TESTS_ROOT_DIR_ABSOLUTE,,g`)
51                 echo "$missing" >> "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND"
52             fi
53         fi
54     done
55
56     if [ -f "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND" ]; then
57         # print list of missing test executables
58         echo ""
59         echo "###################################################"
60         echo "Executables for following unit tests were not found"
61         echo -e "###################################################\E[31m\E[1m" ## set red color & bold
62         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND
63         tput sgr0 # restore terminal defaults
64         echo ""
65         echo "Some possible causes:"
66         echo "  - project has set target name explicitly. Target name must be same as directory name"
67         echo "  - don't use shadow build system"
68
69         # and save same list also to test summary file
70         echo "" >> $REPORT
71         echo "###################################################" >> $REPORT
72         echo "Executables for following unit tests were not found" >> $REPORT
73         echo "###################################################"  >> $REPORT
74         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND  >> $REPORT
75     fi
76
77     # check if unit test output contained any qDebug or qWarning prints
78     debugs=$(egrep -c "(QDEBUG|QWARN)" $REPORT_PATH_ABSOLUTE/$REPORT_FILE)
79     if [ $debugs -ge 1 ]; then
80         echo ""
81         # print message in red and bold
82         echo -e "\E[31m\E[1mDisable debug output from unit tests so test output stays readable!!!"
83         tput sgr0 # restore terminal defaults
84     fi
85
86     # remove temporary files
87     rm $REPORT_PATH_ABSOLUTE/temp_*.txt
88
89     # tell that script reached the end
90     echo "" >> $REPORT
91     echo "All done!" >> $REPORT
92     echo ""
93     echo "All done!"
94 else
95     echo "Paths for report and/or tests not found!"
96     echo "Script must be run from scripts directory"
97 fi
98 echo ""