Skip to content

Commit

Permalink
Merge pull request #57 from Bizordec/main
Browse files Browse the repository at this point in the history
Add flag for xml report
  • Loading branch information
jaltmayerpizzorno authored Nov 5, 2024
2 parents ffade9d + 245818d commit 22a6420
Show file tree
Hide file tree
Showing 8 changed files with 789 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/slipcover/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .version import __version__
from .slipcover import Slipcover, merge_coverage, print_coverage
from .slipcover import Slipcover, merge_coverage, print_coverage, print_xml
from .importer import FileMatcher, ImportManager, wrap_pytest
from .fuzz import wrap_function
8 changes: 8 additions & 0 deletions src/slipcover/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def main():
ap.add_argument('--branch', action='store_true', help="measure both branch and line coverage")
ap.add_argument('--json', action='store_true', help="select JSON output")
ap.add_argument('--pretty-print', action='store_true', help="pretty-print JSON output")
ap.add_argument('--xml', action='store_true', help="select XML output")
ap.add_argument('--xml-package-depth', type=int, default=99, help=(
"Controls which directories are identified as packages in the report. "
"Directories deeper than this depth are not reported as packages. "
"The default is that all directories are reported as packages."))
ap.add_argument('--out', type=Path, help="specify output file name")
ap.add_argument('--source', help="specify directories to cover")
ap.add_argument('--omit', help="specify file(s) to omit")
Expand Down Expand Up @@ -205,6 +210,9 @@ def sci_atexit():
def printit(coverage, outfile):
if args.json:
print(json.dumps(coverage, indent=(4 if args.pretty_print else None)), file=outfile)
elif args.xml:
sc.print_xml(coverage, source_paths=[str(base_path)], with_branches=args.branch,
xml_package_depth=args.xml_package_depth, outfile=outfile)
else:
sc.print_coverage(coverage, outfile=outfile, skip_covered=args.skip_covered,
missing_width=args.missing_width)
Expand Down
30 changes: 30 additions & 0 deletions src/slipcover/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Dict, List, NotRequired, Tuple, TypedDict

class CoverageMeta(TypedDict):
software: str
version: str
timestamp: str
branch_coverage: bool
show_contexts: bool

class CoverageSummary(TypedDict):
covered_lines: int
missing_lines: int
covered_branches: NotRequired[int]
missing_branches: NotRequired[int]
percent_covered: float

class CoverageFile(TypedDict):
executed_lines: List[int]
missing_lines: List[int]
executed_branches: NotRequired[List[Tuple[int, int]]]
missing_branches: NotRequired[List[Tuple[int, int]]]
summary: CoverageSummary

class Coverage(TypedDict):
meta: CoverageMeta
files: Dict[str, CoverageFile]
summary: CoverageSummary
32 changes: 27 additions & 5 deletions src/slipcover/slipcover.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from __future__ import annotations
import sys

import dis
import types
from typing import Dict, Set, List, Tuple, Optional, Iterator, cast
from collections import defaultdict, Counter
import sys
import threading
import types
from collections import Counter, defaultdict
from typing import TYPE_CHECKING

if sys.version_info < (3,12):
from . import probe # type: ignore[attr-defined]
from . import bytecode as bc
from . import probe # type: ignore[attr-defined]

from pathlib import Path

from . import branch as br
from .version import __version__
from .xmlreport import XmlReporter

# FIXME provide __all__

Expand All @@ -38,6 +41,10 @@ def findlinestarts(co: types.CodeType):
else:
findlinestarts = dis.findlinestarts

if TYPE_CHECKING:
from typing import Dict, Iterable, Iterator, List, Optional, Tuple

from .schemas import Coverage

class SlipcoverError(Exception):
pass
Expand Down Expand Up @@ -92,6 +99,21 @@ def find_ranges():

return ", ".join(find_ranges())

def print_xml(
coverage: Coverage,
source_paths: Iterable[str],
*,
with_branches: bool = False,
xml_package_depth: int = 99,
outfile=sys.stdout
) -> None:
XmlReporter(
coverage=coverage,
source=source_paths,
with_branches=with_branches,
xml_package_depth=xml_package_depth,
).report(outfile=outfile)


def print_coverage(coverage, *, outfile=sys.stdout, missing_width=None, skip_covered=False) -> None:
"""Prints coverage information for human consumption."""
Expand Down
1 change: 1 addition & 0 deletions src/slipcover/version.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__version__ = "1.0.15"
__url__ = f"https://github.com/plasma-umass/slipcover/tree/v{__version__}"
Loading

0 comments on commit 22a6420

Please sign in to comment.