Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | ############################################################################## |
| 3 | # Copyright 2018 EuropeanSoftwareMarketingLtd. |
| 4 | # =================================================================== |
| 5 | # Licensed under the ApacheLicense, Version2.0 (the"License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and limitations under |
| 13 | # the License |
| 14 | ############################################################################## |
| 15 | # vnftest comment: this is a modified copy of |
| 16 | # yardstick/tools/cover.sh |
| 17 | |
| 18 | if [[ -n $COVER_DIR_NAME ]]; then |
| 19 | : |
| 20 | elif [[ -n $_ ]]; then |
| 21 | COVER_DIR_NAME=$( dirname $_ ) |
| 22 | else |
| 23 | COVER_DIR_NAME=$( dirname $0 ) |
| 24 | fi |
| 25 | |
| 26 | show_diff () { |
| 27 | diff -U 0 $1 $2 | awk -f $COVER_DIR_NAME/cover.awk |
| 28 | } |
| 29 | |
| 30 | run_coverage_test() { |
| 31 | |
| 32 | ALLOWED_EXTRA_MISSING=10 |
| 33 | # enable debugging |
| 34 | set -x |
| 35 | |
| 36 | # Stash uncommitted changes, checkout master and save coverage report |
| 37 | uncommited=$(git status --porcelain | grep -v "^??") |
| 38 | [[ -n ${uncommited} ]] && git stash > /dev/null |
| 39 | git checkout HEAD^ |
| 40 | |
| 41 | baseline_report=$(mktemp -t vnftest_coverageXXXXXXX) |
| 42 | |
| 43 | find . -type f -name "*.pyc" -delete |
| 44 | |
| 45 | # Temporarily run tests from two directories, until all tests have moved |
| 46 | coverage run -p -m unittest discover ./tests/unit |
Moshe | ea90f9b | 2018-03-25 17:00:40 +0300 | [diff] [blame^] | 47 | coverage run -p -m unittest discover ./vnftest/tests/unit/core |
| 48 | coverage run -p -m unittest discover ./vnftest/tests/unit/onap |
Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 49 | coverage combine |
| 50 | |
| 51 | # Temporarily omit vnftest/tests from the report |
Moshe | ea90f9b | 2018-03-25 17:00:40 +0300 | [diff] [blame^] | 52 | coverage report > ${baseline_report} |
Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 53 | coverage erase |
| 54 | |
| 55 | # debug awk |
| 56 | tail -1 ${baseline_report} |
| 57 | baseline_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${baseline_report}) |
| 58 | |
| 59 | if [[ -z $baseline_missing ]]; then |
| 60 | echo "Failed to determine baseline missing" |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | # Checkout back and unstash uncommitted changes (if any) |
| 65 | git checkout - |
| 66 | [[ -n ${uncommited} ]] && git stash pop > /dev/null |
| 67 | |
| 68 | # Generate and save coverage report |
| 69 | current_report=$(mktemp -t vnftest_coverageXXXXXXX) |
| 70 | |
| 71 | find . -type f -name "*.pyc" -delete |
| 72 | |
| 73 | # Temporarily run tests from two directories, until all tests have moved |
| 74 | coverage run -p -m unittest discover ./tests/unit |
Moshe | ea90f9b | 2018-03-25 17:00:40 +0300 | [diff] [blame^] | 75 | coverage run -p -m unittest discover ./vnftest/tests/unit/core |
| 76 | coverage run -p -m unittest discover ./vnftest/tests/unit/onap |
Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 77 | coverage combine |
| 78 | |
| 79 | # Temporarily omit vnftest/tests from the report |
| 80 | coverage report --omit=vnftest/tests/*/* > ${current_report} |
| 81 | coverage erase |
| 82 | |
| 83 | rm -rf cover-$PY_VER |
| 84 | coverage html -d cover-$PY_VER |
| 85 | |
| 86 | # debug awk |
| 87 | tail -1 ${current_report} |
| 88 | current_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${current_report}) |
| 89 | |
| 90 | if [[ -z $current_missing ]]; then |
| 91 | echo "Failed to determine current missing" |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | # Show coverage details |
| 96 | new_missing=$((current_missing - baseline_missing)) |
| 97 | |
| 98 | echo "Missing lines allowed to introduce : ${ALLOWED_EXTRA_MISSING}" |
| 99 | echo "Missing lines introduced : ${new_missing}" |
| 100 | echo "Missing lines in master : ${baseline_missing}" |
| 101 | echo "Missing lines in proposed change : ${current_missing}" |
| 102 | |
| 103 | if [[ ${new_missing} -gt ${ALLOWED_EXTRA_MISSING} ]]; |
| 104 | then |
| 105 | show_diff ${baseline_report} ${current_report} |
| 106 | echo "Please write more unit tests, we should keep our test coverage :( " |
| 107 | rm ${baseline_report} ${current_report} |
| 108 | exit 1 |
| 109 | |
| 110 | elif [[ ${new_missing} -gt 0 ]]; |
| 111 | then |
| 112 | show_diff ${baseline_report} ${current_report} |
| 113 | echo "I believe you can cover all your code with 100% coverage!" |
| 114 | |
| 115 | else |
| 116 | echo "Thank you! You are awesome! Keep writing unit tests! :)" |
| 117 | fi |
| 118 | |
| 119 | rm ${baseline_report} ${current_report} |
| 120 | } |