pip_license: quick hack script to add license info to requirements.txt
[yardstick.git] / tests / ci / cover.sh
1 ##############################################################################
2 # Copyright 2015: Mirantis Inc.
3 # All Rights Reserved.
4 #
5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 #    not use this file except in compliance with the License. You may obtain
7 #    a copy of the License at
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #    Unless required by applicable law or agreed to in writing, software
12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 #    License for the specific language governing permissions and limitations
15 #    under the License.
16 # yardstick comment: this is a modified copy of
17 # rally/tests/ci/cover.sh
18 ##############################################################################
19
20 show_diff () {
21     head -1 $1
22     diff -U 0 $1 $2 | sed 1,2d
23 }
24
25
26 run_coverage_test() {
27
28     ALLOWED_EXTRA_MISSING=10
29
30
31     # Stash uncommitted changes, checkout master and save coverage report
32     uncommited=$(git status --porcelain | grep -v "^??")
33     [[ -n $uncommited ]] && git stash > /dev/null
34     git checkout HEAD^
35
36     baseline_report=$(mktemp -t yardstick_coverageXXXXXXX)
37     # workaround 'db type could not be determined' bug
38     # https://bugs.launchpad.net/testrepository/+bug/1229445
39     rm -rf .testrepository
40     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --slowest --testr-args="$*"
41     coverage report > $baseline_report
42     baseline_missing=$(awk 'END { print $3 }' $baseline_report)
43
44     # Checkout back and unstash uncommitted changes (if any)
45     git checkout -
46     [[ -n $uncommited ]] && git stash pop > /dev/null
47
48     # Generate and save coverage report
49     current_report=$(mktemp -t yardstick_coverageXXXXXXX)
50     # workaround 'db type could not be determined' bug
51     # https://bugs.launchpad.net/testrepository/+bug/1229445
52     rm -rf .testrepository
53     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --slowest --testr-args="$*"
54     coverage report > $current_report
55     current_missing=$(awk 'END { print $3 }' $current_report)
56
57     # Show coverage details
58     allowed_missing=$((baseline_missing+ALLOWED_EXTRA_MISSING))
59
60     echo "Allowed to introduce missing lines : ${ALLOWED_EXTRA_MISSING}"
61     echo "Missing lines in master            : ${baseline_missing}"
62     echo "Missing lines in proposed change   : ${current_missing}"
63
64     if [ $allowed_missing -gt $current_missing ];
65     then
66         if [ $baseline_missing -lt $current_missing ];
67         then
68             show_diff $baseline_report $current_report
69             echo "I believe you can cover all your code with 100% coverage!"
70         else
71             echo "Thank you! You are awesome! Keep writing unit tests! :)"
72         fi
73     else
74         show_diff $baseline_report $current_report
75         echo "Please write more unit tests, we should keep our test coverage :( "
76         rm $baseline_report $current_report
77         exit 1
78     fi
79
80     rm $baseline_report $current_report
81 }