-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a coverage plugin that handles ipykernel cells #110
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1dff58c
Add a coverage plugin that handles ipykernel cells
chmp a1eee61
Fix the linter
chmp d0ece67
Fix `--cov` behavior with coverage_plugin + pytest-cov
chmp 68e2e94
Use a more specific test
chmp 6438182
Move code into function
chmp ca66f9b
Rename coverage_plugin -> cov
chmp 8944c96
Start to document the coverage plugin
chmp 9d5628f
Document how to use the coverage plugin
chmp fcc8c05
Do not include ipytest.cov members in docs
chmp 740c208
Include ipytest invocation with coverage in docs
chmp c818453
Add integration test for coverage support
chmp 6b7c0f0
Add pytest-cov to requirements and update the requirements.txt
chmp 91f9cb1
Update docs
chmp 8978f30
Update changelog
chmp ce37235
Include pytest-cov in docs
chmp e8d4c1d
Update Readme to document that pytest-cov works
chmp 334d4e1
Fix docs
chmp 29e8d1a
Include the `.coveragerc` hint in the test notebook
chmp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ __pycache__/ | |
.ipynb_checkpoints/ | ||
.pytest_cache/ | ||
.coverage | ||
coverage.json | ||
|
||
/.python-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""A coverage.py plugin to support coverage in Jupyter notebooks | ||
|
||
The plugin must be enabled in a `.coveragerc` next to the current notebook or | ||
the `pyproject.toml` file. See the [coverage.py docs][coverage-py-config-docs] | ||
for details. In case of a `.coveragerc` file, the minimal configuration reads: | ||
|
||
```ini | ||
[run] | ||
plugins = | ||
ipytest.cov | ||
``` | ||
|
||
With this config file, the coverage can be collected using | ||
[pytest-cov][ipytest-cov-pytest-cov] with | ||
|
||
```pyhton | ||
chmp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
%%ipytest --cov | ||
|
||
def test(): | ||
... | ||
``` | ||
|
||
[coverage-py-config-docs]: https://coverage.readthedocs.io/en/latest/config.html | ||
[ipytest-cov-pytest-cov]: https://pytest-cov.readthedocs.io/en/latest/config.html | ||
""" | ||
import linecache | ||
import os | ||
import os.path | ||
import re | ||
|
||
import coverage.parser | ||
import coverage.plugin | ||
import coverage.python | ||
|
||
# prevent the definitions from being documented in the readme | ||
__all__ = [] | ||
|
||
|
||
def coverage_init(reg, options): | ||
reg.add_file_tracer(IPythonPlugin()) | ||
|
||
|
||
class IPythonPlugin(coverage.plugin.CoveragePlugin): | ||
def __init__(self): | ||
self._filename_pattern = self._build_filename_pattern() | ||
|
||
@classmethod | ||
def _build_filename_pattern(self): | ||
try: | ||
import ipykernel.compiler | ||
|
||
except ImportError: | ||
return None | ||
|
||
else: | ||
return re.compile( | ||
r"^" | ||
+ re.escape(ipykernel.compiler.get_tmp_directory()) | ||
+ re.escape(os.sep) | ||
+ r"\d+.py" | ||
) | ||
|
||
def file_tracer(self, filename): | ||
if not self._is_ipython_cell_file(filename): | ||
return None | ||
|
||
return IPythonFileTracer(filename) | ||
|
||
def file_reporter(self, filename): | ||
return IPythonFileReporter(filename) | ||
|
||
def _is_ipython_cell_file(self, filename: str): | ||
if self._filename_pattern is None: | ||
return False | ||
|
||
if os.path.exists(filename): | ||
return False | ||
|
||
if self._filename_pattern.match(filename) is None: | ||
return False | ||
|
||
return filename in linecache.cache | ||
|
||
|
||
class IPythonFileTracer(coverage.plugin.FileTracer): | ||
def __init__(self, filename): | ||
self._filename = filename | ||
|
||
def source_filename(self): | ||
return self._filename | ||
|
||
|
||
class IPythonFileReporter(coverage.python.PythonFileReporter): | ||
@property | ||
def parser(self): | ||
if self._parser is None: | ||
self._parser = coverage.parser.PythonParser(text=self.source()) | ||
self._parser.parse_source() | ||
|
||
return self._parser | ||
|
||
def source(self): | ||
if self.filename not in linecache.cache: | ||
raise ValueError() | ||
|
||
return "".join(linecache.cache[self.filename][2]) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
%%file
cell magic can be used to create files like.coveragerc
from a notebook.Would an
ipytest.generate_coveragerc()
or similar be too much? or Justautoconfig(coverage=True)
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH. I would rather prefer, that there is an option to configure pytest-cov programatically and inject the plugin in that way. I will open an issue for further discussion.