ansible-test - Fix PS coverage `--all` generation.

pull/77254/head
Matt Clay 3 years ago
parent 6f445ca6e5
commit 0c514bcf54

@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Fix traceback when using the ``--all`` option with PowerShell code coverage.

@ -60,6 +60,8 @@ from . import (
PathChecker, PathChecker,
) )
TValue = t.TypeVar('TValue')
def command_coverage_combine(args): # type: (CoverageCombineConfig) -> None def command_coverage_combine(args): # type: (CoverageCombineConfig) -> None
"""Patch paths in coverage files and merge into a single file.""" """Patch paths in coverage files and merge into a single file."""
@ -113,9 +115,12 @@ def _command_coverage_combine_python(args, host_state): # type: (CoverageCombin
coverage_files = get_python_coverage_files() coverage_files = get_python_coverage_files()
def _default_stub_value(source_paths: list[str]) -> dict[str, set[tuple[int, int]]]:
return {path: set() for path in source_paths}
counter = 0 counter = 0
sources = _get_coverage_targets(args, walk_compile_targets) sources = _get_coverage_targets(args, walk_compile_targets)
groups = _build_stub_groups(args, sources, lambda s: dict((name, set()) for name in s)) groups = _build_stub_groups(args, sources, _default_stub_value)
collection_search_re, collection_sub_re = get_collection_path_regexes() collection_search_re, collection_sub_re = get_collection_path_regexes()
@ -185,7 +190,7 @@ def _command_coverage_combine_powershell(args): # type: (CoverageCombineConfig)
"""Combine PowerShell coverage files and return a list of the output files.""" """Combine PowerShell coverage files and return a list of the output files."""
coverage_files = get_powershell_coverage_files() coverage_files = get_powershell_coverage_files()
def _default_stub_value(source_paths): def _default_stub_value(source_paths: list[str]) -> dict[str, dict[int, int]]:
cmd = ['pwsh', os.path.join(ANSIBLE_TEST_TOOLS_ROOT, 'coverage_stub.ps1')] cmd = ['pwsh', os.path.join(ANSIBLE_TEST_TOOLS_ROOT, 'coverage_stub.ps1')]
cmd.extend(source_paths) cmd.extend(source_paths)
@ -234,12 +239,9 @@ def _command_coverage_combine_powershell(args): # type: (CoverageCombineConfig)
coverage_data = dict((filename, data) for filename, data in groups[group].items() if path_checker.check_path(filename)) coverage_data = dict((filename, data) for filename, data in groups[group].items() if path_checker.check_path(filename))
if args.all: if args.all:
# Add 0 line entries for files not in coverage_data missing_sources = [source for source, _source_line_count in sources if source not in coverage_data]
for source, source_line_count in sources: stubs = _default_stub_value(missing_sources)
if source in coverage_data: coverage_data.update(stubs)
continue
coverage_data[source] = _default_stub_value(source_line_count)
if not args.explain: if not args.explain:
if args.export: if args.export:
@ -278,17 +280,19 @@ def _get_coverage_targets(args, walk_func): # type: (CoverageCombineConfig, t.C
return sources return sources
def _build_stub_groups(args, sources, default_stub_value): def _build_stub_groups(
args: CoverageCombineConfig,
sources: list[tuple[str, int]],
default_stub_value: t.Callable[[list[str]], dict[str, TValue]],
) -> dict[str, dict[str, TValue]]:
""" """
:type args: CoverageCombineConfig Split the given list of sources with line counts into groups, maintaining a maximum line count for each group.
:type sources: List[tuple[str, int]] Each group consists of a dictionary of sources and default coverage stubs generated by the provided default_stub_value function.
:type default_stub_value: Func[List[str]]
:rtype: dict
""" """
groups = {} groups = {}
if args.stub: if args.stub:
stub_group = [] stub_group: list[str] = []
stub_groups = [stub_group] stub_groups = [stub_group]
stub_line_limit = 500000 stub_line_limit = 500000
stub_line_count = 0 stub_line_count = 0

Loading…
Cancel
Save