Skip to content

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
Turns out I wasn't at 100% actually, I was at 99.71% which coveralls.io
hlepfully rounded up to 100% in the badge.
  • Loading branch information
mgedmin committed Aug 24, 2020
1 parent 5cff0bf commit 835ee66
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[run]
source = profilehooks
plugins =
coverage_python_version

[report]
exclude_lines =
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tmp
__pycache__/
.coverage
build/
.coverage.*
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ jobs:
install: pip install flake8
script: flake8 *.py
after_success:
- name: coverage
python: 2.7
script:
- coverage run test_profilehooks.py
- coverage report -m --fail-under=100
install:
- pip install coverage coveralls
- pip install coverage coverage-python-version coveralls
script:
- coverage run --source=profilehooks test_profilehooks.py
- coverage run test_profilehooks.py
- coverage report -m
after_success:
- coveralls
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ test check:

.PHONY: coverage
coverage:
coverage run --source=profilehooks test_profilehooks.py
coverage report -m --fail-under=100
tox -e coverage,coverage3

.PHONY: test-all-pythons
test-all-pythons:
Expand Down
33 changes: 21 additions & 12 deletions profilehooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def fn(n):
import timeit

# For hotshot profiling (inaccurate!)
try:
try: # pragma: PY2
import hotshot
import hotshot.stats
except ImportError:
Expand All @@ -124,7 +124,7 @@ def fn(n):
import tokenize

# For hotshot coverage (inaccurate!; uses undocumented APIs; might break)
if hotshot is not None:
if hotshot is not None: # pragma: PY2
import _hotshot
import hotshot.log

Expand All @@ -144,14 +144,20 @@ def fn(n):
tokenize_open = getattr(tokenize, 'open', open)


def _unwrap(fn):
try:
from inspect import unwrap as _unwrap
except ImportError: # pragma: PY2
# inspect.unwrap() doesn't exist on Python 2
if not hasattr(fn, '__wrapped__'):
return fn
else:
# intentionally using recursion here instead of a while loop to
# make cycles fail with a recursion error instead of looping forever.
return _unwrap(fn.__wrapped__)
def _unwrap(fn):
if not hasattr(fn, '__wrapped__'):
return fn
else: # pragma: nocover
# functools.wraps() doesn't set __wrapped__ on Python 2 either,
# so this branch will only get reached if somebody
# manually sets __wrapped__, hence the pragma: nocover.
# NB: intentionally using recursion here instead of a while loop to
# make cycles fail with a recursion error instead of looping forever.
return _unwrap(fn.__wrapped__)


def _identify(fn):
Expand Down Expand Up @@ -289,7 +295,7 @@ def new_fn(*args, **kw):
return new_fn


def coverage_with_hotshot(fn):
def coverage_with_hotshot(fn): # pragma: PY2
"""Mark `fn` for line coverage analysis.
Uses the 'hotshot' module for fast coverage analysis.
Expand Down Expand Up @@ -424,7 +430,7 @@ class CProfileFuncProfile(FuncProfile):
AVAILABLE_PROFILERS['cProfile'] = CProfileFuncProfile


if hotshot is not None:
if hotshot is not None: # pragma: PY2

class HotShotFuncProfile(FuncProfile):
"""Profiler for a function (uses hotshot)."""
Expand Down Expand Up @@ -646,7 +652,10 @@ def __init__(self, fn):

def find_source_lines(self):
"""Mark all executable source lines in fn as executed 0 times."""
if self.filename is None:
if self.filename is None: # pragma: nocover
# I don't know how to make inspect.getsourcefile() return None in
# our test suite, but I've looked at its source and I know that it
# can do so.
return
strs = self._find_docstrings(self.filename)
lines = {
Expand Down
13 changes: 10 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ commands =
[testenv:py]
commands =
python --version
python test_profilehooks.py
{[testenv]commands}

[testenv:coverage]
basepython = python2
deps = coverage
deps =
coverage
coverage-python-version
commands =
coverage run --source=profilehooks test_profilehooks.py
coverage run {posargs} test_profilehooks.py
coverage report -m --fail-under=100

[testenv:coverage3]
basepython = python3
deps = {[testenv:coverage]deps}
commands = {[testenv:coverage]commands}

0 comments on commit 835ee66

Please sign in to comment.