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
48 # Temporarily run tests from two directories, until all tests have moved
49 coverage run -p -m unittest discover ./tests/unit
50 coverage run -p -m unittest discover ./yardstick/tests/unit
53 # Temporarily omit yardstick/tests from the report
54 coverage report --omit=yardstick/tests/*/* > ${baseline_report}
58 tail -1 ${baseline_report}
59 baseline_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${baseline_report})
61 if [[ -z $baseline_missing ]]; then
62 echo "Failed to determine baseline missing"
66 # Checkout back and unstash uncommitted changes (if any)
68 [[ -n ${uncommited} ]] && git stash pop > /dev/null
70 # Generate and save coverage report
71 current_report=$(mktemp -t yardstick_coverageXXXXXXX)
73 find . -type f -name "*.pyc" -delete
75 # Temporarily run tests from two directories, until all tests have moved
76 coverage run -p -m unittest discover ./tests/unit
77 coverage run -p -m unittest discover ./yardstick/tests/unit
80 # Temporarily omit yardstick/tests from the report
81 coverage report --omit=yardstick/tests/*/* > ${current_report}
85 coverage html -d cover-$PY_VER
88 tail -1 ${current_report}
89 current_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${current_report})
91 if [[ -z $current_missing ]]; then
92 echo "Failed to determine current missing"
96 # Show coverage details
97 new_missing=$((current_missing - baseline_missing))
99 echo "Missing lines allowed to introduce : ${ALLOWED_EXTRA_MISSING}"
100 echo "Missing lines introduced : ${new_missing}"
101 echo "Missing lines in master : ${baseline_missing}"
102 echo "Missing lines in proposed change : ${current_missing}"
104 if [[ ${new_missing} -gt ${ALLOWED_EXTRA_MISSING} ]];
106 show_diff ${baseline_report} ${current_report}
107 echo "Please write more unit tests, we should keep our test coverage :( "
108 rm ${baseline_report} ${current_report}
111 elif [[ ${new_missing} -gt 0 ]];
113 show_diff ${baseline_report} ${current_report}
114 echo "I believe you can cover all your code with 100% coverage!"
117 echo "Thank you! You are awesome! Keep writing unit tests! :)"
120 rm ${baseline_report} ${current_report}