From 0c514bcf54ee1c20841b905bd9290071bfc9de3a Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 9 Mar 2022 10:28:39 -0800 Subject: [PATCH] ansible-test - Fix PS coverage `--all` generation. --- .../ansible-test-powershell-coverage-all.yaml | 2 ++ .../_internal/commands/coverage/combine.py | 32 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/ansible-test-powershell-coverage-all.yaml diff --git a/changelogs/fragments/ansible-test-powershell-coverage-all.yaml b/changelogs/fragments/ansible-test-powershell-coverage-all.yaml new file mode 100644 index 00000000000..8f23fceea5e --- /dev/null +++ b/changelogs/fragments/ansible-test-powershell-coverage-all.yaml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test - Fix traceback when using the ``--all`` option with PowerShell code coverage. diff --git a/test/lib/ansible_test/_internal/commands/coverage/combine.py b/test/lib/ansible_test/_internal/commands/coverage/combine.py index 96643f7af12..8cf4c1054b9 100644 --- a/test/lib/ansible_test/_internal/commands/coverage/combine.py +++ b/test/lib/ansible_test/_internal/commands/coverage/combine.py @@ -60,6 +60,8 @@ from . import ( PathChecker, ) +TValue = t.TypeVar('TValue') + def command_coverage_combine(args): # type: (CoverageCombineConfig) -> None """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() + def _default_stub_value(source_paths: list[str]) -> dict[str, set[tuple[int, int]]]: + return {path: set() for path in source_paths} + counter = 0 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() @@ -185,7 +190,7 @@ def _command_coverage_combine_powershell(args): # type: (CoverageCombineConfig) """Combine PowerShell coverage files and return a list of the output 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.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)) if args.all: - # Add 0 line entries for files not in coverage_data - for source, source_line_count in sources: - if source in coverage_data: - continue - - coverage_data[source] = _default_stub_value(source_line_count) + missing_sources = [source for source, _source_line_count in sources if source not in coverage_data] + stubs = _default_stub_value(missing_sources) + coverage_data.update(stubs) if not args.explain: if args.export: @@ -278,17 +280,19 @@ def _get_coverage_targets(args, walk_func): # type: (CoverageCombineConfig, t.C 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 - :type sources: List[tuple[str, int]] - :type default_stub_value: Func[List[str]] - :rtype: dict + Split the given list of sources with line counts into groups, maintaining a maximum line count for each group. + Each group consists of a dictionary of sources and default coverage stubs generated by the provided default_stub_value function. """ groups = {} if args.stub: - stub_group = [] + stub_group: list[str] = [] stub_groups = [stub_group] stub_line_limit = 500000 stub_line_count = 0