Browse Source

Merge "Make coverage tox job fail when test coverage was reduced"

Jenkins 3 years ago
parent
commit
c0c073316d
3 changed files with 78 additions and 3 deletions
  1. 8
    0
      .coveragerc
  2. 68
    0
      tools/cover.sh
  3. 2
    3
      tox.ini

+ 8
- 0
.coveragerc View File

@@ -0,0 +1,8 @@
1
+[run]
2
+branch = True
3
+source = manila
4
+omit = manila/test*,manila/openstack/*
5
+
6
+[report]
7
+ignore-errors = True
8
+precision = 2

+ 68
- 0
tools/cover.sh View File

@@ -0,0 +1,68 @@
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
+
18
+ALLOWED_EXTRA_MISSING=4
19
+TESTR_ARGS="$*"
20
+
21
+show_diff () {
22
+    head -1 $1
23
+    diff -U 0 $1 $2 | sed 1,2d
24
+}
25
+
26
+# Stash uncommited changes, checkout master and save coverage report
27
+uncommited=$(git status --porcelain | grep -v "^??")
28
+[[ -n $uncommited ]] && git stash > /dev/null
29
+git checkout HEAD^
30
+
31
+baseline_report=$(mktemp -t manila_coverageXXXXXXX)
32
+find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$TESTR_ARGS"
33
+coverage report > $baseline_report
34
+baseline_missing=$(awk 'END { print $3 }' $baseline_report)
35
+
36
+# Checkout back and unstash uncommited changes (if any)
37
+git checkout -
38
+[[ -n $uncommited ]] && git stash pop > /dev/null
39
+
40
+# Generate and save coverage report
41
+current_report=$(mktemp -t manila_coverageXXXXXXX)
42
+find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$TESTR_ARGS"
43
+coverage report > $current_report
44
+current_missing=$(awk 'END { print $3 }' $current_report)
45
+
46
+# Show coverage details
47
+allowed_missing=$((baseline_missing+ALLOWED_EXTRA_MISSING))
48
+
49
+echo "Allowed to introduce missing lines : ${ALLOWED_EXTRA_MISSING}"
50
+echo "Missing lines in master            : ${baseline_missing}"
51
+echo "Missing lines in proposed change   : ${current_missing}"
52
+
53
+if [ $allowed_missing -gt $current_missing ]; then
54
+    if [ $baseline_missing -lt $current_missing ]; then
55
+        show_diff $baseline_report $current_report
56
+        echo "I believe you can cover all your code with 100% coverage!"
57
+    else
58
+        echo "Thank you! You are awesome! Keep writing unit tests! :)"
59
+    fi
60
+    exit_code=0
61
+else
62
+    show_diff $baseline_report $current_report
63
+    echo "Please write more unit tests, we should keep our test coverage :( "
64
+    exit_code=1
65
+fi
66
+
67
+rm $baseline_report $current_report
68
+exit $exit_code

+ 2
- 3
tox.ini View File

@@ -23,6 +23,7 @@ commands =
23 23
          contrib/ci/pre_test_hook.sh \
24 24
          contrib/ci/post_test_hook.sh \
25 25
          devstack/plugin.sh \
26
+         tools/cover.sh \
26 27
          run_tests.sh
27 28
 
28 29
 [testenv:genconfig]
@@ -37,9 +38,7 @@ commands = {posargs}
37 38
 commands = python setup.py build_sphinx
38 39
 
39 40
 [testenv:cover]
40
-commands =
41
-  python setup.py testr --coverage \
42
-    --testr-args='^(?!.*test.*coverage).*$'
41
+commands = {toxinidir}/tools/cover.sh {posargs}
43 42
 
44 43
 [testenv:pylint]
45 44
 deps = -r{toxinidir}/requirements.txt

Loading…
Cancel
Save