Skip to content

Commit e259c7a

Browse files
committed
add condition coverage
1 parent 5fa2fd8 commit e259c7a

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

.travis.yml

+39-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ language: python
66
cache: pip
77
before_cache:
88
- rm -f $HOME/.cache/pip/log/debug.log
9+
# place the slowest (instrumental and py2.6) first
910
matrix:
1011
include:
12+
- python: 2.7
13+
env: INSTRUMENTAL=yes
1114
- python: 2.6
1215
env: TOX_ENV=py26
1316
- python: 2.7
@@ -29,13 +32,48 @@ matrix:
2932
- python: pypy3
3033
env: TOX_ENV=pypy3
3134

35+
# for instrumental we're checking if the coverage changed from base branch
36+
# so collect that info
37+
before_install:
38+
- |
39+
echo -e "TRAVIS_PULL_REQUEST=$TRAVIS_PULL_REQUEST\n" \
40+
"TRAVIS_REPO_SLUG=$TRAVIS_REPO_SLUG\n" \
41+
"TRAVIS_PULL_REQUEST=$TRAVIS_PULL_REQUEST\n" \
42+
"TRAVIS_COMMIT=$TRAVIS_COMMIT\n" \
43+
"TRAVIS_PYTHON_VERSION=$TRAVIS_PYTHON_VERSION"
44+
- |
45+
# workaround https://github.com/travis-ci/travis-ci/issues/2666
46+
if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
47+
URL="https://github.com/${TRAVIS_REPO_SLUG}/pull/${TRAVIS_PULL_REQUEST}.patch"
48+
# `--location` makes curl follow redirects
49+
PR_FIRST=$(curl --silent --show-error --location $URL | head -1 | grep -o -E '\b[0-9a-f]{40}\b' | tr -d '\n')
50+
TRAVIS_COMMIT_RANGE=$PR_FIRST^..$TRAVIS_COMMIT
51+
fi
52+
# sanity check current commit
53+
- git rev-parse HEAD
54+
- echo "TRAVIS_COMMIT_RANGE=$TRAVIS_COMMIT_RANGE"
55+
- git fetch origin master:refs/remotes/origin/master
56+
57+
3258
install:
3359
- pip list
3460
- if [[ -e build-requirements-${TRAVIS_PYTHON_VERSION}.txt ]]; then travis_retry pip install -r build-requirements-${TRAVIS_PYTHON_VERSION}.txt; else travis_retry pip install -r build-requirements.txt; fi
61+
- if [[ $INSTRUMENTAL ]]; then travis_retry pip install instrumental; fi
3562
- pip list
3663
script:
37-
- tox -e $TOX_ENV
64+
- if [[ $TOX_ENV ]]; then tox -e $TOX_ENV; fi
3865
- tox -e speed
66+
- |
67+
if [[ $INSTRUMENTAL && $TRAVIS_PULL_REQUEST != "false" ]]; then
68+
git checkout $PR_FIRST^
69+
instrumental -t ecdsa -i 'test.*|_version' python -m pytest src
70+
instrumental -f .instrumental.cov -s
71+
instrumental -f .instrumental.cov -s | python diff-instrumental.py --save .diff-instrumental
72+
git checkout $TRAVIS_COMMIT
73+
instrumental -t ecdsa -i 'test.*|_version' python -m pytest src
74+
instrumental -f .instrumental.cov -sr
75+
instrumental -f .instrumental.cov -s | python diff-instrumental.py --read .diff-instrumental --fail-under 70 --max-difference 0.1
76+
fi
3977
after_success:
4078
- coveralls
4179

diff-instrumental.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import sys
2+
import getopt
3+
4+
fail_under = None
5+
max_difference = 0
6+
read_location = None
7+
save_location = None
8+
9+
argv = sys.argv[1:]
10+
11+
opts, args = getopt.getopt(
12+
argv, "s:r:",
13+
["fail-under=", "max-difference=", "save=", "read="])
14+
if args:
15+
raise ValueError("Unexpected parameters: {0}".format(args))
16+
for opt, arg in opts:
17+
if opt == "-s" or opt == "--save":
18+
save_location = arg
19+
elif opt == "-r" or opt == "--read":
20+
read_location = arg
21+
elif opt == "--fail-under":
22+
fail_under = float(arg)/100.0
23+
elif opt == "--max-difference":
24+
max_difference = float(arg)/100.0
25+
else:
26+
raise ValueError("Unknown option: {0}".format(opt))
27+
28+
total_hits = 0
29+
total_count = 0
30+
31+
for line in sys.stdin.readlines():
32+
if not line.startswith("ecdsa"):
33+
continue
34+
35+
fields = line.split()
36+
hit, count = fields[1].split('/')
37+
total_hits += int(hit)
38+
total_count += int(count)
39+
40+
coverage = total_hits * 1.0 / total_count
41+
42+
if read_location:
43+
with open(read_location, "r") as f:
44+
old_coverage = float(f.read())
45+
print("Old coverage: {0:6.2f}%".format(old_coverage*100))
46+
47+
if save_location:
48+
with open(save_location, "w") as f:
49+
f.write("{0:1.40f}".format(coverage))
50+
51+
print("Coverage: {0:6.2f}%".format(coverage*100))
52+
53+
if read_location:
54+
print("Difference: {0:6.2f}%".format((old_coverage - coverage)*100))
55+
56+
if fail_under and coverage < fail_under:
57+
print("ERROR: Insufficient coverage.", file=sys.stderr)
58+
sys.exit(1)
59+
60+
if read_location and coverage - old_coverage < max_difference:
61+
print("ERROR: Too big decrease in coverage", file=sys.stderr)
62+
sys.exit(1)

0 commit comments

Comments
 (0)