blob: 08811f604193b6f66845dbc2f8099a8f582f8385 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001#!/usr/bin/env ksh
2# this will fail if run with bash!
3
4#==================================================================================
E. Scott Daniels5efb1e62019-05-02 17:09:35 +00005# Copyright (c) 2019 Nokia
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04006# 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 Daniels5efb1e62019-05-02 17:09:35 +000024# Abstract: Execute unit test(s) in the directory and produce a more
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040025# meaningful summary than gcov gives by default (exclude
26# coverage on the unit test functions).
27#
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000028# Test files must be named *_test.c, or must explicitly be
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040029# 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 Daniels5efb1e62019-05-02 17:09:35 +000034# Functions with coverage less than 80% will be reported as
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040035# [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 Daniels5efb1e62019-05-02 17:09:35 +000041# make is assumed and -M will shift to Plan-9. Use -C xxx to
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040042# 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 Daniels5efb1e62019-05-02 17:09:35 +000046# inside of if() statements that are checking return from
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040047# (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 Daniels5efb1e62019-05-02 17:09:35 +000050# and a pass/fail message from the discount test are listed,
51# but only the result of the discount test is taken into
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040052# consideration with regard to overall success.
53#
E. Scott Daniels8dd46412019-04-16 20:47:54 +000054# Overall Pass/Fail
55# By default the overall state is based only on the success
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000056# or failure of the unit tests and NOT on the perceived
E. Scott Daniels8dd46412019-04-16 20:47:54 +000057# 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 Sridharanfd9cc7a2019-04-03 16:47:02 -040061# Date: 16 January 2018
62# Author: E. Scott Daniels
63# -------------------------------------------------------------------------
64
65function usage {
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000066 echo "usage: $0 [-G|-M|-C custom-command-string] [-c cov-target] [-f] [-F] [-v] [-x] [files]"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040067 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 Daniels8dd46412019-04-16 20:47:54 +000072 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 Sridharanfd9cc7a2019-04-03 16:47:02 -040074 echo " -v will write additional information to the tty and save the disccounted file if discount run or -f given"
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000075 echo " -x generates the coverage XML files for Sonar (implies -f)"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040076}
77
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000078# read through the given file and add any functions that are static to the
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040079# ignored list. Only test and test tools files should be parsed.
80#
81function 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 Daniels5efb1e62019-05-02 17:09:35 +000089 {
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040090 gsub( "[(].*", "" )
91 print $3
92 }
93 ' | while read f
94 do
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000095 iflist="${iflist}$f "
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040096 done
97}
98
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000099
100# Merge two coverage files to preserve the total lines covered by different
101# test programmes.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400102#
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000103function 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 Sridharanfd9cc7a2019-04-03 16:47:02 -0400147# 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 Daniels8dd46412019-04-16 20:47:54 +0000149# and shoudn't contribute to coverage deficiencies.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400150#
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.
156function 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 Daniels5efb1e62019-05-02 17:09:35 +0000165 f=${1##*/}
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400166 else
167 echo "cant find: $f"
168 return
169 fi
170 fi
171
172 awk -v module_cov_target=$mct \
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000173 -v cfail=${cfail:-WARN} \
174 -v show_all=$show_all \
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400175 -v full_name="${1}" \
176 -v module="${f%.*}" \
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000177 -v chatty=$chatty \
178 -v replace_flags=$replace_flags \
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400179 '
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 Daniels5efb1e62019-05-02 17:09:35 +0000189 next
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400190 }
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 Daniels5efb1e62019-05-02 17:09:35 +0000209 if( replace_flags ) {
210 gsub( "#####", " 1", $0 )
211 //gsub( "#####", "=====", $0 )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400212 }
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 Daniels5efb1e62019-05-02 17:09:35 +0000256 {
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400257 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 Daniels8dd46412019-04-16 20:47:54 +0000264 pass_fail = adj_cov < module_cov_target ? cfail : "PASS"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400265 rc = adj_cov < module_cov_target ? 1 : 0
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000266 if( pass_fail == cfail || show_all ) {
267 if( chatty ) {
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000268 printf( "[%s] %s executable=%d unexecuted=%d discounted=%d net_unex=%d cov=%d%% ==> %d%% target=%d%%\n",
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000269 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 Sridharanfd9cc7a2019-04-03 16:47:02 -0400273 }
274
275 exit( rc )
276 }
277 ' $f
278}
279
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000280# Given a file name ($1) see if it is in the ./.targets file. If it is
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400281# return the coverage listed, else return (echo) the default $module_cov_target
282#
283function 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 Daniels5efb1e62019-05-02 17:09:35 +0000294# 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#
298function 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 Sridharanfd9cc7a2019-04-03 16:47:02 -0400303
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000304
305# -----------------------------------------------------------------------------------------------------------------
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400306
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000307# we assume that the project has been built in the ../[.]build directory
E. Scott Daniels85c5bf72019-05-29 13:38:43 +0000308if [[ -d ../build ]]
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000309then
E. Scott Daniels85c5bf72019-05-29 13:38:43 +0000310 export LD_LIBRARY_PATH=../build/lib:../build/lib64
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000311else
E. Scott Daniels85c5bf72019-05-29 13:38:43 +0000312 if [[ -d ../.build ]]
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000313 then
E. Scott Daniels85c5bf72019-05-29 13:38:43 +0000314 export LD_LIBRARY_PATH=../.build/lib:../.build/lib64
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000315 export C_INCLUDE_PATH=../.build/include
316
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000317 else
E. Scott Daniels85c5bf72019-05-29 13:38:43 +0000318 echo "[WARN] cannot find build directory (tried ../build and ../.build); things might not work"
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000319 echo ""
320 fi
321fi
322
E. Scott Daniels69e742d2019-06-11 13:35:39 -0400323export LIBRARY_PATH=$LD_LIBRARY_PATH
324
E. Scott Daniels412d53d2019-05-20 20:00:52 +0000325export C_INCLUDE_PATH="../src/rmr/common/include:$C_INCLUDE_PATH"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400326
327module_cov_target=80
328builder="make -B %s" # default to plain ole make
329verbose=0
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000330show_all=1 # show all things -F sets to show failures only
331strict=0 # -s (strict) will set; when off, coverage state ignored in final pass/fail
E. Scott Danielsd7109572019-04-18 14:01:16 +0000332show_output=0 # show output from each test execution (-S)
E. Scott Danielsa41c6f52019-04-23 18:24:25 +0000333quiet=0
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000334gen_xml=0
335replace_flags=1 # replace ##### in gcov for discounted lines
E. Scott Danielsb0ba22e2019-08-06 13:26:12 -0400336run_nano_tests=0
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400337
E. Scott Daniels6511ac72019-08-27 10:17:21 -0400338export RMR_WARNING=1 # turn on warnings
339
E. Scott Daniels0d4def62020-01-28 16:50:27 -0500340ulimit -c unlimited
341
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400342while [[ $1 == "-"* ]]
343do
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000344 case $1 in
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400345 -C) builder="$2"; shift;; # custom build command
346 -G) builder="gmake %s";;
347 -M) builder="mk -a %s";; # use plan-9 mk (better, but sadly not widly used)
E. Scott Danielsb0ba22e2019-08-06 13:26:12 -0400348 -N) run_nano_tests=1;;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400349
350 -c) module_cov_target=$2; shift;;
E. Scott Danielsd9de79a2019-10-31 09:20:33 -0400351 -e) capture_file=$2; >$capture_file; shift;; # capture errors from failed tests rather than spewing on tty
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000352 -f) force_discounting=1;
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000353 trigger_discount_str="WARN|FAIL|PASS" # check all outcomes for each module
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400354 ;;
355
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000356 -F) show_all=0;;
357
358 -s) strict=1;; # coverage counts toward pass/fail state
E. Scott Danielsd7109572019-04-18 14:01:16 +0000359 -S) show_output=1;; # test output shown even on success
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400360 -v) (( verbose++ ));;
E. Scott Danielsa41c6f52019-04-23 18:24:25 +0000361 -q) quiet=1;; # less chatty when spilling error log files
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000362 -x) gen_xml=1
363 force_discounting=1
364 trigger_discount_str="WARN|FAIL|PASS" # check all outcomes for each module
E. Scott Daniels7958ddf2019-05-14 20:07:36 +0000365 rm -fr *cov.xml
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000366 ;;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400367
368 -h) usage; exit 0;;
369 --help) usage; exit 0;;
370 -\?) usage; exit 0;;
371
372 *) echo "unrecognised option: $1" >&2
373 usage >&2
374 exit 1
375 ;;
376 esac
377
378 shift
379done
380
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000381
382if (( strict )) # if in strict mode, coverage shortcomings are failures
383then
384 cfail="FAIL"
385else
386 cfail="WARN"
387fi
388if [[ -z $trigger_discount_str ]]
389then
390 trigger_discount_str="$cfail"
391fi
392
393
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400394if [[ -z $1 ]]
395then
396 flist=""
397 for tfile in *_test.c
398 do
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000399 if [[ $tfile != *"static_test.c" ]]
400 then
E. Scott Danielsb0ba22e2019-08-06 13:26:12 -0400401 if(( ! run_nano_tests )) && [[ $tfile == *"nano"* ]]
402 then
403 continue
404 fi
405
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000406 flist="${flist}$tfile "
407 fi
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400408 done
409else
410 flist="$@"
411fi
412
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000413
E. Scott Daniels7958ddf2019-05-14 20:07:36 +0000414rm -fr *.gcov # ditch the previous coverage files
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000415ut_errors=0 # unit test errors (not coverage errors)
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400416errors=0
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000417
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400418for tfile in $flist
419do
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000420 for x in *.gcov
421 do
422 if [[ -e $x ]]
423 then
424 cp $x $x-
425 fi
426 done
427
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400428 echo "$tfile --------------------------------------"
429 bcmd=$( printf "$builder" "${tfile%.c}" )
430 if ! $bcmd >/tmp/PID$$.log 2>&1
431 then
432 echo "[FAIL] cannot build $tfile"
433 cat /tmp/PID$$.log
434 rm -f /tmp/PID$$
435 exit 1
436 fi
437
438 iflist="main sig_clean_exit " # ignore external functions from our tools
439 add_ignored_func $tfile # ignore all static functions in our test driver
440 add_ignored_func test_support.c # ignore all static functions in our test tools
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000441 add_ignored_func test_nng_em.c # the nng/nano emulated things
442 for f in *_static_test.c # all static modules here
443 do
E. Scott Danielsb0ba22e2019-08-06 13:26:12 -0400444 if(( ! run_nano_tests )) && [[ $f == *"nano"* ]]
445 then
446 continue
447 fi
448
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000449 add_ignored_func $f
450 done
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000451
E. Scott Daniels11642d92019-04-17 14:53:34 +0000452 if ! ./${tfile%.c} >/tmp/PID$$.log 2>&1
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400453 then
454 echo "[FAIL] unit test failed for: $tfile"
E. Scott Daniels0b79fc22019-12-04 15:20:16 -0500455 if [[ -n $capture_file ]]
E. Scott Danielsa41c6f52019-04-23 18:24:25 +0000456 then
E. Scott Danielsd9de79a2019-10-31 09:20:33 -0400457 echo "all errors captured in $capture_file, listing only fail message on tty"
458 echo "$tfile --------------------------------------" >>$capture_file
459 cat /tmp/PID$$.log >>$capture_file
460 grep "^<FAIL>" /tmp/PID$$.log
461 echo ""
E. Scott Danielsa41c6f52019-04-23 18:24:25 +0000462 else
E. Scott Danielsd9de79a2019-10-31 09:20:33 -0400463 if (( quiet ))
464 then
465 grep "^<" /tmp/PID$$.log|grep -v "^<EM>" # in quiet mode just dump <...> messages which are assumed from the test programme not appl
466 else
467 cat /tmp/PID$$.log
468 fi
E. Scott Danielsa41c6f52019-04-23 18:24:25 +0000469 fi
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000470 (( ut_errors++ )) # cause failure even if not in strict mode
471 continue # skip coverage tests for this
E. Scott Danielsd7109572019-04-18 14:01:16 +0000472 else
473 if (( show_output ))
474 then
475 printf "\n============= test programme output =======================\n"
476 cat /tmp/PID$$.log
477 printf "===========================================================\n"
478 fi
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400479 fi
480
481 (
482 touch ./.targets
483 sed '/^#/ d; /^$/ d; s/^/TARGET: /' ./.targets
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000484 gcov -f ${tfile%.c} | sed "s/'//g"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400485 ) | awk \
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000486 -v cfail=$cfail \
487 -v show_all=$show_all \
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400488 -v ignore_list="$iflist" \
489 -v module_cov_target=$module_cov_target \
490 -v chatty=$verbose \
491 '
492 BEGIN {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000493 announce_target = 1;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400494 nignore = split( ignore_list, ignore, " " )
495 for( i = 1; i <= nignore; i++ ) {
496 imap[ignore[i]] = 1
497 }
498
499 exit_code = 0 # assume good
500 }
501
502 /^TARGET:/ {
503 if( NF > 1 ) {
504 target[$2] = $NF
505 }
506 next;
507 }
508
509 /File.*_test/ || /File.*test_/ { # dont report on test files
510 skip = 1
511 file = 1
512 fname = $2
513 next
514 }
515
516 /File/ {
517 skip = 0
518 file = 1
519 fname = $2
520 next
521 }
522
523 /Function/ {
524 fname = $2
525 file = 0
526 if( imap[fname] ) {
527 fname = "skipped: " fname # should never see and make it smell if we do
528 skip = 1
529 } else {
530 skip = 0
531 }
532 next
533 }
534
535 skip { next }
536
537 /Lines executed/ {
538 split( $0, a, ":" )
539 pct = a[2]+0
540
541 if( file ) {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000542 if( announce_target ) { # announce default once at start
543 announce_target = 0;
544 printf( "\n[INFO] default target coverage for modules is %d%%\n", module_cov_target )
545 }
546
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400547 if( target[fname] ) {
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000548 mct = target[fname]
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000549 announce_target = 1;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400550 } else {
551 mct = module_cov_target
552 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000553
554 if( announce_target ) { # annoucne for module if different from default
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400555 printf( "[INFO] target coverage for %s is %d%%\n", fname, mct )
556 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000557
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400558 if( pct < mct ) {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000559 printf( "[%s] %3d%% %s\n", cfail, pct, fname ) # CAUTION: write only 3 things here
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400560 exit_code = 1
561 } else {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000562 printf( "[PASS] %3d%% %s\n", pct, fname )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400563 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000564
565 announce_target = 0;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400566 } else {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000567 if( pct < 70 ) {
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400568 printf( "[LOW] %3d%% %s\n", pct, fname )
569 } else {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000570 if( pct < 80 ) {
571 printf( "[MARG] %3d%% %s\n", pct, fname )
572 } else {
573 if( show_all ) {
574 printf( "[OK] %3d%% %s\n", pct, fname )
575 }
576 }
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400577 }
578 }
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000579
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400580 }
581
582 END {
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000583 printf( "\n" );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400584 exit( exit_code )
585 }
586 ' >/tmp/PID$$.log # capture output to run discount on failures
587 rc=$?
588 cat /tmp/PID$$.log
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000589
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400590 if (( rc || force_discounting )) # didn't pass, or forcing, see if discounting helps
591 then
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000592 if (( ! verbose ))
593 then
594 echo "[INFO] checking to see if discounting improves coverage for failures listed above"
595 fi
596
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400597 egrep "$trigger_discount_str" /tmp/PID$$.log | while read state junk name
598 do
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400599 if ! discount_an_checks $name.gcov >/tmp/PID$$.disc
600 then
601 (( errors++ ))
602 fi
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000603
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000604 tail -1 /tmp/PID$$.disc | grep '\['
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000605
606 if (( verbose > 1 )) # updated file was generated, keep here
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400607 then
608 echo "[INFO] discounted coverage info in: ${tfile##*/}.dcov"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400609 fi
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000610
611 mv /tmp/PID$$.disc ${name##*/}.dcov
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400612 done
613 fi
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000614
615 for x in *.gcov # merge any previous coverage file with this one
616 do
617 if [[ -e $x && -e $x- ]]
618 then
619 merge_cov $x $x- >/tmp/PID$$.mc
620 cp /tmp/PID$$.mc $x
621 rm $x-
622 fi
623 done
624done
625
626echo ""
627echo "[INFO] final discount checks on merged gcov files"
628show_all=1
629for xx in *.gcov
630do
631 if [[ $xx != *"test"* ]]
632 then
633 of=${xx%.gcov}.dcov
634 discount_an_checks $xx >$of
635 tail -1 $of | grep '\['
636 fi
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400637done
638
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000639state=0 # final state
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400640rm -f /tmp/PID$$.*
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000641if (( strict )) # fail if some coverage failed too
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400642then
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000643 if (( errors + ut_errors ))
644 then
645 state=1
646 fi
647else # not strict; fail only if unit tests themselves failed
648 if (( ut_errors ))
649 then
650 state=1
651 fi
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400652fi
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000653
654echo""
655if (( state ))
656then
657 echo "[FAIL] overall unit testing fails: coverage errors=$errors unit test errors=$ut_errors"
658else
659 echo "[PASS] overall unit testing passes"
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000660 if (( gen_xml ))
661 then
662 mk_xml
663 fi
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000664fi
665exit $state
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400666