Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 1 | #!/usr/bin/env ksh |
| 2 | # this will fail if run with bash! |
| 3 | |
| 4 | #================================================================================== |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 5 | # Copyright (c) 2019 Nokia |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 6 | # Copyright (c) 2018-2019 AT&T Intellectual Property. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | #================================================================================== |
| 20 | |
| 21 | |
| 22 | # |
| 23 | # Mnemonic: unit_test.ksh |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 24 | # Abstract: Execute unit test(s) in the directory and produce a more |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 25 | # meaningful summary than gcov gives by default (exclude |
| 26 | # coverage on the unit test functions). |
| 27 | # |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 28 | # Test files must be named *_test.c, or must explicitly be |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 29 | # supplied on the command line. Functions in the test |
| 30 | # files will not be reported on provided that they have |
| 31 | # their prototype (all on the SAME line) as: |
| 32 | # static type name() { |
| 33 | # |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 34 | # Functions with coverage less than 80% will be reported as |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 35 | # [LOW] in the output. A file is considered to pass if the |
| 36 | # overall execution percentage for the file is >= 80% regardless |
| 37 | # of the number of functions that reported low. |
| 38 | # |
| 39 | # Test programmes are built prior to execution. Plan-9 mk is |
| 40 | # the preferred builder, but as it's not widly adopted (sigh) |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 41 | # make is assumed and -M will shift to Plan-9. Use -C xxx to |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 42 | # invoke a customised builder. |
| 43 | # |
| 44 | # For a module which does not pass, we will attempt to boost |
| 45 | # the coverage by discounting the unexecuted lines which are |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 46 | # inside of if() statements that are checking return from |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 47 | # (m)alloc() calls or are checking for nil pointers as these |
| 48 | # cases are likely impossible to drive. When discount testing |
| 49 | # is done both the failure message from the original analysis |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 50 | # and a pass/fail message from the discount test are listed, |
| 51 | # but only the result of the discount test is taken into |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 52 | # consideration with regard to overall success. |
| 53 | # |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 54 | # Overall Pass/Fail |
| 55 | # By default the overall state is based only on the success |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 56 | # or failure of the unit tests and NOT on the perceived |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 57 | # state of coverage. If the -s (strict) option is given, then |
| 58 | # overall state will be failure if code coverage expectations |
| 59 | # are not met. |
| 60 | # |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 61 | # Date: 16 January 2018 |
| 62 | # Author: E. Scott Daniels |
| 63 | # ------------------------------------------------------------------------- |
| 64 | |
| 65 | function usage { |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 66 | echo "usage: $0 [-G|-M|-C custom-command-string] [-c cov-target] [-f] [-F] [-v] [-x] [files]" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 67 | echo " if -C is used to provide a custom build command then it must " |
| 68 | echo " contain a %s which will be replaced with the unit test file name." |
| 69 | echo ' e.g.: -C "mk -a %s"' |
| 70 | echo " -c allows user to set the target coverage for a module to pass; default is 80" |
| 71 | echo " -f forces a discount check (normally done only if coverage < target)" |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 72 | echo " -F show only failures at the function level" |
| 73 | echo " -s strict mode; code coverage must also pass to result in a good exit code" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 74 | echo " -v will write additional information to the tty and save the disccounted file if discount run or -f given" |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 75 | echo " -x generates the coverage XML files for Sonar (implies -f)" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 76 | } |
| 77 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 78 | # read through the given file and add any functions that are static to the |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 79 | # ignored list. Only test and test tools files should be parsed. |
| 80 | # |
| 81 | function add_ignored_func { |
| 82 | if [[ ! -r $1 ]] |
| 83 | then |
| 84 | return |
| 85 | fi |
| 86 | |
| 87 | typeset f="" |
| 88 | grep "^static.*(.*).*{" $1 | awk ' # get list of test functions to ignore |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 89 | { |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 90 | gsub( "[(].*", "" ) |
| 91 | print $3 |
| 92 | } |
| 93 | ' | while read f |
| 94 | do |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 95 | iflist="${iflist}$f " |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 96 | done |
| 97 | } |
| 98 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 99 | |
| 100 | # Merge two coverage files to preserve the total lines covered by different |
| 101 | # test programmes. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 102 | # |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 103 | function merge_cov { |
| 104 | if [[ -z $1 || -z $2 ]] |
| 105 | then |
| 106 | return |
| 107 | fi |
| 108 | |
| 109 | if [[ ! -e $1 || ! -e $2 ]] |
| 110 | then |
| 111 | return |
| 112 | fi |
| 113 | |
| 114 | ( |
| 115 | cat $1 |
| 116 | echo "==merge==" |
| 117 | cat $2 |
| 118 | ) | awk ' |
| 119 | /^==merge==/ { |
| 120 | merge = 1 |
| 121 | next |
| 122 | } |
| 123 | |
| 124 | merge && /#####:/ { |
| 125 | line = $2+0 |
| 126 | if( executed[line] ) { |
| 127 | $1 = sprintf( "%9d:", executed[line] ) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | merge { |
| 132 | print |
| 133 | next |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | line = $2+0 |
| 138 | if( $1+0 > 0 ) { |
| 139 | executed[line] = $1+0 |
| 140 | } |
| 141 | } |
| 142 | ' |
| 143 | } |
| 144 | |
| 145 | # |
| 146 | # Parse the .gcov file and discount any unexecuted lines which are in if() |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 147 | # blocks that are testing the result of alloc/malloc calls, or testing for |
| 148 | # nil pointers. The feeling is that these might not be possible to drive |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 149 | # and shoudn't contribute to coverage deficiencies. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 150 | # |
| 151 | # In verbose mode, the .gcov file is written to stdout and any unexecuted |
| 152 | # line which is discounted is marked with ===== replacing the ##### marking |
| 153 | # that gcov wrote. |
| 154 | # |
| 155 | # The return value is 0 for pass; non-zero for fail. |
| 156 | function discount_an_checks { |
| 157 | typeset f="$1" |
| 158 | |
| 159 | mct=$( get_mct ${1%.gcov} ) # see if a special coverage target is defined for this |
| 160 | |
| 161 | if [[ ! -f $1 ]] |
| 162 | then |
| 163 | if [[ -f ${1##*/} ]] |
| 164 | then |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 165 | f=${1##*/} |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 166 | else |
| 167 | echo "cant find: $f" |
| 168 | return |
| 169 | fi |
| 170 | fi |
| 171 | |
| 172 | awk -v module_cov_target=$mct \ |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 173 | -v cfail=${cfail:-WARN} \ |
| 174 | -v show_all=$show_all \ |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 175 | -v full_name="${1}" \ |
| 176 | -v module="${f%.*}" \ |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 177 | -v chatty=$chatty \ |
| 178 | -v replace_flags=$replace_flags \ |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 179 | ' |
| 180 | function spit_line( ) { |
| 181 | if( chatty ) { |
| 182 | printf( "%s\n", $0 ) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /-:/ { # skip unexecutable lines |
| 187 | spit_line() |
| 188 | seq++ # allow blank lines in a sequence group |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 189 | next |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | { |
| 193 | nexec++ # number of executable lines |
| 194 | } |
| 195 | |
| 196 | /#####:/ { |
| 197 | unexec++; |
| 198 | if( $2+0 != seq+1 ) { |
| 199 | prev_malloc = 0 |
| 200 | prev_if = 0 |
| 201 | seq = 0 |
| 202 | spit_line() |
| 203 | next |
| 204 | } |
| 205 | |
| 206 | if( prev_if && prev_malloc ) { |
| 207 | if( prev_malloc ) { |
| 208 | #printf( "allow discount: %s\n", $0 ) |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 209 | if( replace_flags ) { |
| 210 | gsub( "#####", " 1", $0 ) |
| 211 | //gsub( "#####", "=====", $0 ) |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 212 | } |
| 213 | discount++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | seq++;; |
| 218 | spit_line() |
| 219 | next; |
| 220 | } |
| 221 | |
| 222 | /if[(].*alloc.*{/ { # if( (x = malloc( ... )) != NULL ) or if( (p = sym_alloc(...)) != NULL ) |
| 223 | seq = $2+0 |
| 224 | prev_malloc = 1 |
| 225 | prev_if = 1 |
| 226 | spit_line() |
| 227 | next |
| 228 | } |
| 229 | |
| 230 | /if[(].* == NULL/ { # a nil check likely not easily forced if it wasnt driven |
| 231 | prev_malloc = 1 |
| 232 | prev_if = 1 |
| 233 | spit_line() |
| 234 | seq = $2+0 |
| 235 | next |
| 236 | } |
| 237 | |
| 238 | /if[(]/ { |
| 239 | if( seq+1 == $2+0 && prev_malloc ) { // malloc on previous line |
| 240 | prev_if = 1 |
| 241 | } else { |
| 242 | prev_malloc = 0 |
| 243 | prev_if = 0 |
| 244 | } |
| 245 | spit_line() |
| 246 | next |
| 247 | } |
| 248 | |
| 249 | /alloc[(]/ { |
| 250 | seq = $2+0 |
| 251 | prev_malloc = 1 |
| 252 | spit_line() |
| 253 | next |
| 254 | } |
| 255 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 256 | { |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 257 | spit_line() |
| 258 | } |
| 259 | |
| 260 | END { |
| 261 | net = unexec - discount |
| 262 | orig_cov = ((nexec-unexec)/nexec)*100 # original coverage |
| 263 | adj_cov = ((nexec-net)/nexec)*100 # coverage after discount |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 264 | pass_fail = adj_cov < module_cov_target ? cfail : "PASS" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 265 | rc = adj_cov < module_cov_target ? 1 : 0 |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 266 | if( pass_fail == cfail || show_all ) { |
| 267 | if( chatty ) { |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 268 | printf( "[%s] %s executable=%d unexecuted=%d discounted=%d net_unex=%d cov=%d%% ==> %d%% target=%d%%\n", |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 269 | pass_fail, full_name ? full_name : module, nexec, unexec, discount, net, orig_cov, adj_cov, module_cov_target ) |
| 270 | } else { |
| 271 | printf( "[%s] %d%% (%d%%) %s\n", pass_fail, adj_cov, orig_cov, full_name ? full_name : module ) |
| 272 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | exit( rc ) |
| 276 | } |
| 277 | ' $f |
| 278 | } |
| 279 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 280 | # Given a file name ($1) see if it is in the ./.targets file. If it is |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 281 | # return the coverage listed, else return (echo) the default $module_cov_target |
| 282 | # |
| 283 | function get_mct { |
| 284 | typeset v=$module_cov_target |
| 285 | |
| 286 | if [[ -f ./.targets ]] |
| 287 | then |
| 288 | grep "^$1 " ./.targets | head -1 | read junk tv |
| 289 | fi |
| 290 | |
| 291 | echo ${tv:-$v} |
| 292 | } |
| 293 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 294 | # Remove unneeded coverage files, then generate the xml files that can be given |
| 295 | # to sonar. gcov.xml is based on the "raw" coverage and dcov.xml is based on |
| 296 | # the discounted coverage. |
| 297 | # |
| 298 | function mk_xml { |
| 299 | rm -fr *_test.c.gcov test_*.c.gcov *_test.c.dcov test_*.c.dcov # we don't report on the unit test code, so ditch |
| 300 | cat *.gcov | cov2xml.ksh >gcov.xml |
| 301 | cat *.dcov | cov2xml.ksh >dcov.xml |
| 302 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 303 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 304 | |
| 305 | # ----------------------------------------------------------------------------------------------------------------- |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 306 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 307 | # we assume that the project has been built in the ../[.]build directory |
| 308 | if [[ -d ../build/lib ]] |
| 309 | then |
| 310 | export LD_LIBRARY_PATH=../build/lib |
| 311 | else |
| 312 | if [[ -d ../.build/lib ]] |
| 313 | then |
| 314 | export LD_LIBRARY_PATH=../.build/lib |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 315 | export C_INCLUDE_PATH=../.build/include |
| 316 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 317 | else |
| 318 | echo "[WARN] cannot find ../[.]build/lib; things might not work" |
| 319 | echo "" |
| 320 | fi |
| 321 | fi |
| 322 | |
E. Scott Daniels | 11642d9 | 2019-04-17 14:53:34 +0000 | [diff] [blame] | 323 | export C_INCLUDE_PATH="../src/common/include:$C_INCLUDE_PATH" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 324 | |
| 325 | module_cov_target=80 |
| 326 | builder="make -B %s" # default to plain ole make |
| 327 | verbose=0 |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 328 | show_all=1 # show all things -F sets to show failures only |
| 329 | strict=0 # -s (strict) will set; when off, coverage state ignored in final pass/fail |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 330 | show_output=0 # show output from each test execution (-S) |
E. Scott Daniels | a41c6f5 | 2019-04-23 18:24:25 +0000 | [diff] [blame] | 331 | quiet=0 |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 332 | gen_xml=0 |
| 333 | replace_flags=1 # replace ##### in gcov for discounted lines |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 334 | |
| 335 | while [[ $1 == "-"* ]] |
| 336 | do |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 337 | case $1 in |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 338 | -C) builder="$2"; shift;; # custom build command |
| 339 | -G) builder="gmake %s";; |
| 340 | -M) builder="mk -a %s";; # use plan-9 mk (better, but sadly not widly used) |
| 341 | |
| 342 | -c) module_cov_target=$2; shift;; |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 343 | -f) force_discounting=1; |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 344 | trigger_discount_str="WARN|FAIL|PASS" # check all outcomes for each module |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 345 | ;; |
| 346 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 347 | -F) show_all=0;; |
| 348 | |
| 349 | -s) strict=1;; # coverage counts toward pass/fail state |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 350 | -S) show_output=1;; # test output shown even on success |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 351 | -v) (( verbose++ ));; |
E. Scott Daniels | a41c6f5 | 2019-04-23 18:24:25 +0000 | [diff] [blame] | 352 | -q) quiet=1;; # less chatty when spilling error log files |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 353 | -x) gen_xml=1 |
| 354 | force_discounting=1 |
| 355 | trigger_discount_str="WARN|FAIL|PASS" # check all outcomes for each module |
| 356 | rm *cov.xml |
| 357 | ;; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 358 | |
| 359 | -h) usage; exit 0;; |
| 360 | --help) usage; exit 0;; |
| 361 | -\?) usage; exit 0;; |
| 362 | |
| 363 | *) echo "unrecognised option: $1" >&2 |
| 364 | usage >&2 |
| 365 | exit 1 |
| 366 | ;; |
| 367 | esac |
| 368 | |
| 369 | shift |
| 370 | done |
| 371 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 372 | |
| 373 | if (( strict )) # if in strict mode, coverage shortcomings are failures |
| 374 | then |
| 375 | cfail="FAIL" |
| 376 | else |
| 377 | cfail="WARN" |
| 378 | fi |
| 379 | if [[ -z $trigger_discount_str ]] |
| 380 | then |
| 381 | trigger_discount_str="$cfail" |
| 382 | fi |
| 383 | |
| 384 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 385 | if [[ -z $1 ]] |
| 386 | then |
| 387 | flist="" |
| 388 | for tfile in *_test.c |
| 389 | do |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 390 | if [[ $tfile != *"static_test.c" ]] |
| 391 | then |
| 392 | flist="${flist}$tfile " |
| 393 | fi |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 394 | done |
| 395 | else |
| 396 | flist="$@" |
| 397 | fi |
| 398 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 399 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 400 | rm *.gcov # ditch the previous coverage files |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 401 | ut_errors=0 # unit test errors (not coverage errors) |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 402 | errors=0 |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 403 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 404 | for tfile in $flist |
| 405 | do |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 406 | for x in *.gcov |
| 407 | do |
| 408 | if [[ -e $x ]] |
| 409 | then |
| 410 | cp $x $x- |
| 411 | fi |
| 412 | done |
| 413 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 414 | echo "$tfile --------------------------------------" |
| 415 | bcmd=$( printf "$builder" "${tfile%.c}" ) |
| 416 | if ! $bcmd >/tmp/PID$$.log 2>&1 |
| 417 | then |
| 418 | echo "[FAIL] cannot build $tfile" |
| 419 | cat /tmp/PID$$.log |
| 420 | rm -f /tmp/PID$$ |
| 421 | exit 1 |
| 422 | fi |
| 423 | |
| 424 | iflist="main sig_clean_exit " # ignore external functions from our tools |
| 425 | add_ignored_func $tfile # ignore all static functions in our test driver |
| 426 | add_ignored_func test_support.c # ignore all static functions in our test tools |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 427 | add_ignored_func test_nng_em.c # the nng/nano emulated things |
| 428 | for f in *_static_test.c # all static modules here |
| 429 | do |
| 430 | add_ignored_func $f |
| 431 | done |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 432 | |
E. Scott Daniels | 11642d9 | 2019-04-17 14:53:34 +0000 | [diff] [blame] | 433 | if ! ./${tfile%.c} >/tmp/PID$$.log 2>&1 |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 434 | then |
| 435 | echo "[FAIL] unit test failed for: $tfile" |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 436 | if (( quiet )) |
E. Scott Daniels | a41c6f5 | 2019-04-23 18:24:25 +0000 | [diff] [blame] | 437 | then |
| 438 | grep "^<" /tmp/PID$$.log # in quiet mode just dump <...> messages which are assumed from the test programme not appl |
| 439 | else |
| 440 | cat /tmp/PID$$.log |
| 441 | fi |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 442 | (( ut_errors++ )) # cause failure even if not in strict mode |
| 443 | continue # skip coverage tests for this |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 444 | else |
| 445 | if (( show_output )) |
| 446 | then |
| 447 | printf "\n============= test programme output =======================\n" |
| 448 | cat /tmp/PID$$.log |
| 449 | printf "===========================================================\n" |
| 450 | fi |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 451 | fi |
| 452 | |
| 453 | ( |
| 454 | touch ./.targets |
| 455 | sed '/^#/ d; /^$/ d; s/^/TARGET: /' ./.targets |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 456 | gcov -f ${tfile%.c} | sed "s/'//g" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 457 | ) | awk \ |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 458 | -v cfail=$cfail \ |
| 459 | -v show_all=$show_all \ |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 460 | -v ignore_list="$iflist" \ |
| 461 | -v module_cov_target=$module_cov_target \ |
| 462 | -v chatty=$verbose \ |
| 463 | ' |
| 464 | BEGIN { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 465 | announce_target = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 466 | nignore = split( ignore_list, ignore, " " ) |
| 467 | for( i = 1; i <= nignore; i++ ) { |
| 468 | imap[ignore[i]] = 1 |
| 469 | } |
| 470 | |
| 471 | exit_code = 0 # assume good |
| 472 | } |
| 473 | |
| 474 | /^TARGET:/ { |
| 475 | if( NF > 1 ) { |
| 476 | target[$2] = $NF |
| 477 | } |
| 478 | next; |
| 479 | } |
| 480 | |
| 481 | /File.*_test/ || /File.*test_/ { # dont report on test files |
| 482 | skip = 1 |
| 483 | file = 1 |
| 484 | fname = $2 |
| 485 | next |
| 486 | } |
| 487 | |
| 488 | /File/ { |
| 489 | skip = 0 |
| 490 | file = 1 |
| 491 | fname = $2 |
| 492 | next |
| 493 | } |
| 494 | |
| 495 | /Function/ { |
| 496 | fname = $2 |
| 497 | file = 0 |
| 498 | if( imap[fname] ) { |
| 499 | fname = "skipped: " fname # should never see and make it smell if we do |
| 500 | skip = 1 |
| 501 | } else { |
| 502 | skip = 0 |
| 503 | } |
| 504 | next |
| 505 | } |
| 506 | |
| 507 | skip { next } |
| 508 | |
| 509 | /Lines executed/ { |
| 510 | split( $0, a, ":" ) |
| 511 | pct = a[2]+0 |
| 512 | |
| 513 | if( file ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 514 | if( announce_target ) { # announce default once at start |
| 515 | announce_target = 0; |
| 516 | printf( "\n[INFO] default target coverage for modules is %d%%\n", module_cov_target ) |
| 517 | } |
| 518 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 519 | if( target[fname] ) { |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 520 | mct = target[fname] |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 521 | announce_target = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 522 | } else { |
| 523 | mct = module_cov_target |
| 524 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 525 | |
| 526 | if( announce_target ) { # annoucne for module if different from default |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 527 | printf( "[INFO] target coverage for %s is %d%%\n", fname, mct ) |
| 528 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 529 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 530 | if( pct < mct ) { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 531 | printf( "[%s] %3d%% %s\n", cfail, pct, fname ) # CAUTION: write only 3 things here |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 532 | exit_code = 1 |
| 533 | } else { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 534 | printf( "[PASS] %3d%% %s\n", pct, fname ) |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 535 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 536 | |
| 537 | announce_target = 0; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 538 | } else { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 539 | if( pct < 70 ) { |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 540 | printf( "[LOW] %3d%% %s\n", pct, fname ) |
| 541 | } else { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 542 | if( pct < 80 ) { |
| 543 | printf( "[MARG] %3d%% %s\n", pct, fname ) |
| 544 | } else { |
| 545 | if( show_all ) { |
| 546 | printf( "[OK] %3d%% %s\n", pct, fname ) |
| 547 | } |
| 548 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 549 | } |
| 550 | } |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 551 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | END { |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 555 | printf( "\n" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 556 | exit( exit_code ) |
| 557 | } |
| 558 | ' >/tmp/PID$$.log # capture output to run discount on failures |
| 559 | rc=$? |
| 560 | cat /tmp/PID$$.log |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 561 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 562 | if (( rc || force_discounting )) # didn't pass, or forcing, see if discounting helps |
| 563 | then |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 564 | if (( ! verbose )) |
| 565 | then |
| 566 | echo "[INFO] checking to see if discounting improves coverage for failures listed above" |
| 567 | fi |
| 568 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 569 | egrep "$trigger_discount_str" /tmp/PID$$.log | while read state junk name |
| 570 | do |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 571 | if ! discount_an_checks $name.gcov >/tmp/PID$$.disc |
| 572 | then |
| 573 | (( errors++ )) |
| 574 | fi |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 575 | |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 576 | tail -1 /tmp/PID$$.disc | grep '\[' |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 577 | |
| 578 | if (( verbose > 1 )) # updated file was generated, keep here |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 579 | then |
| 580 | echo "[INFO] discounted coverage info in: ${tfile##*/}.dcov" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 581 | fi |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 582 | |
| 583 | mv /tmp/PID$$.disc ${name##*/}.dcov |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 584 | done |
| 585 | fi |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 586 | |
| 587 | for x in *.gcov # merge any previous coverage file with this one |
| 588 | do |
| 589 | if [[ -e $x && -e $x- ]] |
| 590 | then |
| 591 | merge_cov $x $x- >/tmp/PID$$.mc |
| 592 | cp /tmp/PID$$.mc $x |
| 593 | rm $x- |
| 594 | fi |
| 595 | done |
| 596 | done |
| 597 | |
| 598 | echo "" |
| 599 | echo "[INFO] final discount checks on merged gcov files" |
| 600 | show_all=1 |
| 601 | for xx in *.gcov |
| 602 | do |
| 603 | if [[ $xx != *"test"* ]] |
| 604 | then |
| 605 | of=${xx%.gcov}.dcov |
| 606 | discount_an_checks $xx >$of |
| 607 | tail -1 $of | grep '\[' |
| 608 | fi |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 609 | done |
| 610 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 611 | state=0 # final state |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 612 | rm -f /tmp/PID$$.* |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 613 | if (( strict )) # fail if some coverage failed too |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 614 | then |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 615 | if (( errors + ut_errors )) |
| 616 | then |
| 617 | state=1 |
| 618 | fi |
| 619 | else # not strict; fail only if unit tests themselves failed |
| 620 | if (( ut_errors )) |
| 621 | then |
| 622 | state=1 |
| 623 | fi |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 624 | fi |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 625 | |
| 626 | echo"" |
| 627 | if (( state )) |
| 628 | then |
| 629 | echo "[FAIL] overall unit testing fails: coverage errors=$errors unit test errors=$ut_errors" |
| 630 | else |
| 631 | echo "[PASS] overall unit testing passes" |
E. Scott Daniels | 5efb1e6 | 2019-05-02 17:09:35 +0000 | [diff] [blame^] | 632 | if (( gen_xml )) |
| 633 | then |
| 634 | mk_xml |
| 635 | fi |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 636 | fi |
| 637 | exit $state |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 638 | |