Merge "Unify different VNF testcases TG and VNF names"
[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     coverage erase
48
49     coverage run -p -m unittest discover ./yardstick/tests/unit
50     coverage combine
51
52     coverage report > ${baseline_report}
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 yardstick_coverageXXXXXXX)
70
71     find . -type f -name "*.pyc" -delete
72
73     coverage run -p -m unittest discover ./yardstick/tests/unit
74     coverage combine
75
76     coverage report > ${current_report}
77     coverage erase
78
79     rm -rf cover-$PY_VER
80     coverage html -d cover-$PY_VER
81
82     # debug awk
83     tail -1 ${current_report}
84     current_missing=$(awk 'END { if (int($3) > 0) print $3 }' ${current_report})
85
86     if [[ -z $current_missing ]]; then
87         echo "Failed to determine current missing"
88         exit 1
89     fi
90
91     # Show coverage details
92     new_missing=$((current_missing - baseline_missing))
93
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}"
98
99     if [[ ${new_missing} -gt ${ALLOWED_EXTRA_MISSING} ]];
100     then
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}
104         exit 1
105
106     elif [[ ${new_missing} -gt 0 ]];
107     then
108         show_diff ${baseline_report} ${current_report}
109         echo "I believe you can cover all your code with 100% coverage!"
110
111     else
112         echo "Thank you! You are awesome! Keep writing unit tests! :)"
113     fi
114
115     rm ${baseline_report} ${current_report}
116 }