Proposing Abhijit Sinha as a committer in Yardstick
[yardstick.git] / tools / cover.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright 2015: Mirantis Inc.
4 # All Rights Reserved.
5 #
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
9 #
10 #         http://www.apache.org/licenses/LICENSE-2.0
11 #
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
16 #    under the License.
17 # yardstick comment: this is a modified copy of
18 # rally/tests/ci/cover.sh
19 ##############################################################################
20
21 if [[ -n $COVER_DIR_NAME ]]; then
22     :
23 elif [[ -n $_ ]]; then
24     COVER_DIR_NAME=$( dirname $_ )
25 else
26     COVER_DIR_NAME=$( dirname $0 )
27 fi
28
29 show_diff () {
30     diff -U 0 $1 $2 | awk -f $COVER_DIR_NAME/cover.awk
31 }
32
33 run_coverage_test() {
34
35     ALLOWED_EXTRA_MISSING=10
36     # enable debugging
37     set -x
38
39     # Stash uncommitted changes, checkout master and save coverage report
40     uncommited=$(git status --porcelain | grep -v "^??")
41     [[ -n ${uncommited} ]] && git stash > /dev/null
42     git checkout HEAD^
43
44     baseline_report=$(mktemp -t yardstick_coverageXXXXXXX)
45
46     find . -type f -name "*.pyc" -delete
47
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
51     coverage combine
52
53     # Temporarily omit yardstick/tests from the report
54     coverage report --omit=yardstick/tests/*/* > ${baseline_report}
55     coverage erase
56
57     # debug awk
58     tail -1 ${baseline_report}
59     baseline_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${baseline_report})
60
61     if [[ -z $baseline_missing ]]; then
62         echo "Failed to determine baseline missing"
63         exit 1
64     fi
65
66     # Checkout back and unstash uncommitted changes (if any)
67     git checkout -
68     [[ -n ${uncommited} ]] && git stash pop > /dev/null
69
70     # Generate and save coverage report
71     current_report=$(mktemp -t yardstick_coverageXXXXXXX)
72
73     find . -type f -name "*.pyc" -delete
74
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
78     coverage combine
79
80     # Temporarily omit yardstick/tests from the report
81     coverage report --omit=yardstick/tests/*/* > ${current_report}
82     coverage erase
83
84     rm -rf cover-$PY_VER
85     coverage html -d cover-$PY_VER
86
87     # debug awk
88     tail -1 ${current_report}
89     current_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${current_report})
90
91     if [[ -z $current_missing ]]; then
92         echo "Failed to determine current missing"
93         exit 1
94     fi
95
96     # Show coverage details
97     new_missing=$((current_missing - baseline_missing))
98
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}"
103
104     if [[ ${new_missing} -gt ${ALLOWED_EXTRA_MISSING} ]];
105     then
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}
109         exit 1
110
111     elif [[ ${new_missing} -gt 0 ]];
112     then
113         show_diff ${baseline_report} ${current_report}
114         echo "I believe you can cover all your code with 100% coverage!"
115
116     else
117         echo "Thank you! You are awesome! Keep writing unit tests! :)"
118     fi
119
120     rm ${baseline_report} ${current_report}
121 }