Created osm.h, re-factoring, unit test script bug fix
[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 paths, cut .pro extension
32     UNIT_TEST_PROJECTS=(`find $UNIT_TESTS_ROOT_DIR_ABSOLUTE | egrep \.pro$ | sed -e s,.pro$,,g`)
33
34     echo "###################################################"
35     echo "All unit test executables will be deleted"
36     echo "and qmake + make will be run for root tests.pro"
37     echo "###################################################"
38     echo ""
39
40     # remove all unit test binaries
41     for BINARY in "${UNIT_TEST_PROJECTS[@]}"
42     do
43         # remove only if file really exists
44         if [ -a $BINARY ]; then
45             rm $BINARY
46         fi
47     done
48
49     # run qmake
50     cd $UNIT_TESTS_ROOT_DIR_ABSOLUTE
51     qmake -r
52     make
53     cd -
54
55     echo ""
56     echo "###################################################"
57     echo "Executing tests..."
58     echo "###################################################"
59
60     # run tests
61     for PROJECT in "${UNIT_TEST_PROJECTS[@]}"
62     do
63         # do not try to test the root tests.pro project
64         if [ $PROJECT != "$UNIT_TESTS_ROOT_DIR_ABSOLUTE/tests" ]; then
65             # check if unit test file is found and is executable
66             if [ -x $PROJECT ]; then
67                 # go to unit test directory
68                 cd "`echo $PROJECT | grep --perl-regexp -o .*\(?=/\)`"
69                 # run unit test, save to temp file, ignore strerr stream
70                 $PROJECT -silent -o $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE 2>&1 | grep -v "atk_object_set_name..assertion"
71                 cd - > /dev/null
72                 # print to screen and append to summary
73                 cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE
74                 cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
75                 echo "" >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
76             else
77                 # save path of missing test to temporary file
78                 MISSING=(`echo $PROJECT | sed -e s,$UNIT_TESTS_ROOT_DIR_ABSOLUTE,,g`)
79                 echo "$MISSING" >> "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND"
80             fi
81         fi
82     done
83
84     if [ -f "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND" ]; then
85         # print list of missing test executables
86         echo ""
87         echo "###################################################"
88         echo "Executables for following unit tests were not found"
89         echo -e "###################################################\E[31m\E[1m" ## set red color & bold
90         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND
91         tput sgr0 # restore terminal defaults
92         echo ""
93         echo "Some possible causes:"
94         echo "  - project has set target name explicitly. Target name must be same as the directory name"
95         echo "  - don't use shadow build system"
96         echo "  - test project is not included in the master test project (tests/tests.pro)"
97
98         # and save same list also to test summary file
99         echo "" >> $REPORT
100         echo "###################################################" >> $REPORT
101         echo "Executables for following unit tests were not found" >> $REPORT
102         echo "###################################################"  >> $REPORT
103         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND  >> $REPORT
104     fi
105
106     # check if unit test output contained any qDebug or qWarning prints
107     DEBUG_COUNT=$(egrep -c "(QDEBUG|QWARN)" $REPORT_PATH_ABSOLUTE/$REPORT_FILE)
108     if [ $DEBUG_COUNT -ge 1 ]; then
109         echo ""
110         # print message in red and bold
111         echo -e "\E[31m\E[1mDisable debug output from unit tests so test output stays readable!!!"
112         tput sgr0 # restore terminal defaults
113     fi
114
115     # remove temporary files
116     rm $REPORT_PATH_ABSOLUTE/temp_*.txt
117
118     # remove all unit test binaries so they doesn't accidentally get into repository
119     for BINARY in "${UNIT_TEST_PROJECTS[@]}"
120     do
121         # remove only if file really exists
122         if [ -a $BINARY ]; then
123             rm $BINARY
124         fi
125     done
126
127     # tell that script reached the end
128     echo "" >> $REPORT
129     echo "All done!" >> $REPORT
130     echo ""
131     echo "All done!"
132 else
133     echo "Paths for report and/or tests not found!"
134     echo "Script must be run from scripts directory"
135 fi
136 echo ""