Skip to content

Commit

Permalink
Merge pull request #5 from TheCleric/pylint_support
Browse files Browse the repository at this point in the history
Add pylint support
  • Loading branch information
TheCleric authored Aug 22, 2021
2 parents 63cf654 + 39582ad commit 9f48527
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 34 deletions.
24 changes: 21 additions & 3 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,28 @@ jobs:
shell: bash
run: poetry install

- name: PyTest
- name: Mypy
shell: bash
run: poetry run pytest
run: poetry run mypy .

- name: Mypy
shell: bash
run: poetry run mypy .
run: poetry run pylint coverage_lcov tests

- name: PyTest
shell: bash
run: poetry run coverage run --source=coverage_lcov -m pytest tests && poetry run coverage report

- name: Eat our own dog food
shell: bash
run: poetry run coverage-lcov

- name: Report code coverage
if: ${{ matrix.platform == 'ubuntu-latest' && matrix.python-version == '3.9' }}
uses: zgosalvez/github-actions-report-lcov@v1
with:
coverage-files: lcov.info
minimum-coverage: 0
artifact-name: code-coverage-report
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: coverage_lcov
3 changes: 1 addition & 2 deletions coverage_lcov/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .cli import main


if __name__ == "__main__":
main()
main() # pylint: disable=no-value-for-parameter
8 changes: 7 additions & 1 deletion coverage_lcov/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
@click.option("--config_file", default=True, help="Path to .coveragerc file")
@click.option("--relative_path", is_flag=True, help="Use relative path in LCOV output")
@click.option("--preview", is_flag=True, help="Preview LCOV output")
def main(data_file_path: str, output_file_path: str, config_file: str, relative_path: bool, preview: bool) -> None:
def main(
data_file_path: str,
output_file_path: str,
config_file: str,
relative_path: bool,
preview: bool,
) -> None:
converter = Converter(
data_file_path=data_file_path,
config_file=config_file,
Expand Down
63 changes: 42 additions & 21 deletions coverage_lcov/converter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
from typing import Optional, Union
from typing import Any, List, Optional, Union

import coverage
from coverage.files import FnmatchMatcher, prep_patterns
from coverage.misc import CoverageException, NoSource, NotPython
from coverage.python import PythonFileReporter
from coverage.results import Analysis

log = logging.getLogger("coverage_lcov.converter")

Expand All @@ -23,40 +25,55 @@ def __init__(
self.cov_obj.load()
self.cov_obj.get_data()

def get_lcov(self) -> str:
"""Get LCOV output
This is shamelessly adapted from https://github.com/nedbat/coveragepy/blob/master/coverage/report.py
"""
output = ""

file_reporters = self.cov_obj._get_file_reporters(None)
def get_file_reporters(self) -> List[Union[PythonFileReporter, Any]]:
file_reporters: List[
Union[PythonFileReporter, Any]
] = self.cov_obj._get_file_reporters( # pylint: disable=protected-access
None
)
config = self.cov_obj.config

if config.report_include:
matcher = FnmatchMatcher(
matcher = FnmatchMatcher( # pylint: disable=too-many-function-args
prep_patterns(config.report_include), "report_include"
)
file_reporters = [fr for fr in file_reporters if matcher.match(fr.filename)]

if config.report_omit:
matcher = FnmatchMatcher(prep_patterns(config.report_omit), "report_omit")
matcher = FnmatchMatcher( # pylint: disable=too-many-function-args
prep_patterns(config.report_omit), "report_omit"
)
file_reporters = [
fr for fr in file_reporters if not matcher.match(fr.filename)
]

if not file_reporters:
raise CoverageException("No data to report.")

for fr in sorted(file_reporters):
return file_reporters

def get_lcov(self) -> str:
"""Get LCOV output
This is shamelessly adapted from https://github.com/nedbat/coveragepy/blob/master/coverage/report.py
"""
output = ""

file_reporters = self.get_file_reporters()

config = self.cov_obj.config

for file_reporter in sorted(file_reporters):
try:
analysis = self.cov_obj._analyze(fr)
analysis = self.cov_obj._analyze( # pylint: disable=protected-access
file_reporter
)
token_lines = analysis.file_reporter.source_token_lines()
if self.relative_path:
filename = fr.relative_filename()
filename = file_reporter.relative_filename()
else:
filename = fr.filename
filename = file_reporter.filename
output += "TN:\n"
output += f"SF:{filename}\n"

Expand All @@ -77,10 +94,14 @@ def get_lcov(self) -> str:
raise

except NotPython:
if fr.should_be_python():
if file_reporter.should_be_python():
if config.ignore_errors:
msg = "Couldn't parse Python file '{}'".format(fr.filename)
self.cov_obj._warn(msg, slug="couldnt-parse")
msg = "Couldn't parse Python file '{}'".format(
file_reporter.filename
)
self.cov_obj._warn( # pylint: disable=protected-access
msg, slug="couldnt-parse"
)

else:
raise
Expand All @@ -99,11 +120,11 @@ def print_lcov(self) -> None:
def create_lcov(self, output_file_path: str) -> None:
lcov_str = self.get_lcov()

with open(output_file_path, "w") as output_file:
with open(output_file_path, "w", encoding="ascii") as output_file:
output_file.write(lcov_str)


def get_hits(line_num: int, analysis: coverage.Analysis) -> Optional[int]:
def get_hits(line_num: int, analysis: Analysis) -> Optional[int]:
if line_num in analysis.missing:
return 0

Expand Down
116 changes: 114 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f48527

Please sign in to comment.