E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # vim: ts=4 sw=4 noet: |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 3 | #================================================================================== |
| 4 | # Copyright (c) 2020 Nokia |
| 5 | # Copyright (c) 2020 AT&T Intellectual Property. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | #================================================================================== |
| 19 | |
| 20 | # |
| 21 | # Mnemonic: unit_test.sh |
| 22 | # Abstract: This drives the unit tests and combs out the needed .gcov |
| 23 | # files which are by some magic collected for Sonar. |
| 24 | # |
| 25 | # Date: 23 March 2020 |
| 26 | # Author: E. Scott Daniels |
| 27 | # ----------------------------------------------------------------------------- |
| 28 | |
| 29 | |
| 30 | # Make a list of our modules under test so that we don't look at gcov |
| 31 | # files that are generated for system lib headers in /usr/* |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 32 | # (bash makes the process of building a list of names harder than it |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 33 | # needs to be, so use caution with the printf() call.) |
| 34 | # |
| 35 | function mk_list { |
| 36 | grep -l "Source:\.\./src" *.gcov | while read f |
| 37 | do |
| 38 | printf "$f " # do NOT use echo or add \n! |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 39 | done |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | function abort_if_error { |
| 43 | if (( $1 == 0 )) |
| 44 | then |
| 45 | return |
| 46 | fi |
| 47 | |
| 48 | if [[ -n /tmp/PID$$.log ]] |
| 49 | then |
| 50 | $spew /tmp/PID$$.log |
| 51 | fi |
| 52 | echo "abort: $2" |
| 53 | |
| 54 | rm -f /tmp/PID$$.* |
| 55 | exit 1 |
| 56 | } |
| 57 | |
| 58 | # ------------------------------------------------------------------------- |
| 59 | |
| 60 | spew="cat" # default to dumping all make output on failure (-q turns it to ~40 lines) |
| 61 | |
E. Scott Daniels | ef36205 | 2020-07-22 15:49:54 -0400 | [diff] [blame] | 62 | if [[ -d ../.build ]] |
| 63 | then |
| 64 | build_dir="../.build" |
| 65 | else |
| 66 | if [[ -d ../build ]] |
| 67 | then |
| 68 | build_dir="../build" |
| 69 | fi |
| 70 | fi |
| 71 | |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 72 | while [[ $1 == "-"* ]] |
| 73 | do |
| 74 | case $1 in |
| 75 | -q) spew="head -40";; |
| 76 | -v) spew="cat";; |
| 77 | esac |
| 78 | |
| 79 | shift |
| 80 | done |
| 81 | |
E. Scott Daniels | ef36205 | 2020-07-22 15:49:54 -0400 | [diff] [blame] | 82 | while [[ $1 == *"="* ]] |
| 83 | do |
| 84 | case ${1%%=*} in |
| 85 | CMBUILD) |
| 86 | export build_dir=${1##*=} |
| 87 | ;; |
| 88 | esac |
| 89 | |
| 90 | shift |
| 91 | done |
| 92 | |
| 93 | echo "## INFO ##" |
| 94 | echo "build dir=$build_dir" |
E. Scott Daniels | cfe644e | 2020-08-13 12:48:38 -0400 | [diff] [blame] | 95 | if ! find $build_dir | grep "libricxfcpp.*" # find returns good even if none; must grep to see error |
| 96 | then |
| 97 | echo "building first..." |
| 98 | ( |
| 99 | cd $build_dir |
| 100 | make package |
| 101 | ) |
| 102 | |
| 103 | echo "build finished" |
| 104 | find $build_dir -name "libricxfcpp.*" |
| 105 | fi |
E. Scott Daniels | ef36205 | 2020-07-22 15:49:54 -0400 | [diff] [blame] | 106 | echo "## INFO ##" |
| 107 | |
| 108 | export LD_LIBRARY_PATH=$build_dir:/usr/local/lib:$LD_LIBRARY_PATH |
| 109 | export LIBRARY_PATH=$build_dir:/usr/local/lib:$LIBRARY_PATH |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 110 | |
E. Scott Daniels | d486a17 | 2020-07-29 12:39:54 -0400 | [diff] [blame] | 111 | cp config1.json config-file.json # ensure default named file is there too |
| 112 | |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 113 | make nuke >/dev/null |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 114 | make tests >/tmp/PID$$.log 2>&1 |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 115 | abort_if_error $? "unable to make" |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 116 | echo "tests successfully built" >&2 |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 117 | |
| 118 | spew="cat" |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 119 | |
E. Scott Daniels | b0c88ed | 2020-08-13 15:12:35 -0400 | [diff] [blame] | 120 | # order here is important to ensure coverage files accumulate |
| 121 | tests="metrics_test jhash_test config_test unit_test" |
| 122 | |
E. Scott Daniels | d486a17 | 2020-07-29 12:39:54 -0400 | [diff] [blame] | 123 | #run everything, then generate coverage stats after all have run |
E. Scott Daniels | b0c88ed | 2020-08-13 15:12:35 -0400 | [diff] [blame] | 124 | for x in $tests |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 125 | do |
| 126 | ./$x >/tmp/PID$$.log 2>&1 |
| 127 | abort_if_error $? "test failed: $x" |
E. Scott Daniels | 23d0e61 | 2020-09-17 15:46:48 -0400 | [diff] [blame] | 128 | grep SUMMARY /tmp/PID$$.log |
E. Scott Daniels | d486a17 | 2020-07-29 12:39:54 -0400 | [diff] [blame] | 129 | done |
| 130 | |
| 131 | # it seems that we loose coverage reporting if metrics_test's gcov file is generated |
| 132 | # after unit test. Very strange. To be safe, run unit_test last. |
| 133 | # |
E. Scott Daniels | b0c88ed | 2020-08-13 15:12:35 -0400 | [diff] [blame] | 134 | for x in $tests |
E. Scott Daniels | d486a17 | 2020-07-29 12:39:54 -0400 | [diff] [blame] | 135 | do |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 136 | gcov $x.c >/dev/null 2>&1 |
| 137 | done |
| 138 | |
| 139 | # wrapper_test is driven by jhash_test, but must be covered explicitly |
| 140 | gcov jwrapper_test.c >/dev/null 2>&1 |
| 141 | |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 142 | ./scrub_gcov.sh # remove cruft |
| 143 | |
| 144 | list=$( mk_list ) |
E. Scott Daniels | 97204c8 | 2020-06-29 15:39:57 -0400 | [diff] [blame] | 145 | echo "" |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 146 | echo "[INFO] coverage stats, discounted (raw), for the various modules:" |
E. Scott Daniels | 4e4fb50 | 2020-03-24 12:28:06 -0400 | [diff] [blame] | 147 | ./parse_gcov.sh $list # generate simple, short, coverage stats |
| 148 | |
| 149 | rm -f /tmp/PID$$.* |
| 150 | |