2 ##############################################################################
3 # Copyright 2015: Mirantis Inc.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
17 # yardstick comment: this is a modified copy of
18 # rally/tests/ci/cover.sh
19 ##############################################################################
21 if [[ -n $COVER_DIR_NAME ]]; then
23 elif [[ -n $_ ]]; then
24 COVER_DIR_NAME=$( dirname $_ )
26 COVER_DIR_NAME=$( dirname $0 )
30 diff -U 0 $1 $2 | awk -f $COVER_DIR_NAME/cover.awk
35 ALLOWED_EXTRA_MISSING=10
39 # Stash uncommitted changes, checkout master and save coverage report
40 uncommited=$(git status --porcelain | grep -v "^??")
41 [[ -n ${uncommited} ]] && git stash > /dev/null
44 baseline_report=$(mktemp -t yardstick_coverageXXXXXXX)
46 find . -type f -name "*.pyc" -delete
49 coverage run -p -m unittest discover ./yardstick/tests/unit
52 coverage report > ${baseline_report}
56 tail -1 ${baseline_report}
57 baseline_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${baseline_report})
59 if [[ -z $baseline_missing ]]; then
60 echo "Failed to determine baseline missing"
64 # Checkout back and unstash uncommitted changes (if any)
66 [[ -n ${uncommited} ]] && git stash pop > /dev/null
68 # Generate and save coverage report
69 current_report=$(mktemp -t yardstick_coverageXXXXXXX)
71 find . -type f -name "*.pyc" -delete
73 coverage run -p -m unittest discover ./yardstick/tests/unit
76 coverage report > ${current_report}
80 coverage html -d cover-$PY_VER
83 tail -1 ${current_report}
84 current_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${current_report})
86 if [[ -z $current_missing ]]; then
87 echo "Failed to determine current missing"
91 # Show coverage details
92 new_missing=$((current_missing - baseline_missing))
94 echo "Missing lines allowed to introduce : ${ALLOWED_EXTRA_MISSING}"
95 echo "Missing lines introduced : ${new_missing}"
96 echo "Missing lines in master : ${baseline_missing}"
97 echo "Missing lines in proposed change : ${current_missing}"
99 if [[ ${new_missing} -gt ${ALLOWED_EXTRA_MISSING} ]];
101 show_diff ${baseline_report} ${current_report}
102 echo "Please write more unit tests, we should keep our test coverage :( "
103 rm ${baseline_report} ${current_report}
106 elif [[ ${new_missing} -gt 0 ]];
108 show_diff ${baseline_report} ${current_report}
109 echo "I believe you can cover all your code with 100% coverage!"
112 echo "Thank you! You are awesome! Keep writing unit tests! :)"
115 rm ${baseline_report} ${current_report}