Implementation of opnfv-yardstick-tc021 in Yardstick
[yardstick.git] / ci / cover.sh
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # make coverage report
9 ##############################################################################
10
11 show_diff () {
12     head -1 $1
13     diff -U 0 $1 $2 | sed 1,2d
14 }
15
16
17 run_coverage_test() {
18
19     ALLOWED_EXTRA_MISSING=10
20
21
22     # Stash uncommitted changes, checkout master and save coverage report
23     uncommited=$(git status --porcelain | grep -v "^??")
24     [[ -n $uncommited ]] && git stash > /dev/null
25     git checkout HEAD^
26
27     baseline_report=$(mktemp -t yardstick_coverageXXXXXXX)
28     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$*"
29     coverage report > $baseline_report
30     baseline_missing=$(awk 'END { print $3 }' $baseline_report)
31
32     # Checkout back and unstash uncommitted changes (if any)
33     git checkout -
34     [[ -n $uncommited ]] && git stash pop > /dev/null
35
36     # Generate and save coverage report
37     current_report=$(mktemp -t yardstick_coverageXXXXXXX)
38     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$*"
39     coverage report > $current_report
40     current_missing=$(awk 'END { print $3 }' $current_report)
41
42     # Show coverage details
43     allowed_missing=$((baseline_missing+ALLOWED_EXTRA_MISSING))
44
45     echo "Allowed to introduce missing lines : ${ALLOWED_EXTRA_MISSING}"
46     echo "Missing lines in master            : ${baseline_missing}"
47     echo "Missing lines in proposed change   : ${current_missing}"
48
49     if [ $allowed_missing -gt $current_missing ];
50     then
51         if [ $baseline_missing -lt $current_missing ];
52         then
53             show_diff $baseline_report $current_report
54             echo "I believe you can cover all your code with 100% coverage!"
55         else
56             echo "Thank you! You are awesome! Keep writing unit tests! :)"
57         fi
58     else
59         show_diff $baseline_report $current_report
60         echo "Please write more unit tests, we should keep our test coverage :( "
61         rm $baseline_report $current_report
62         exit 1
63     fi
64
65     rm $baseline_report $current_report
66 }