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