summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_internal/coverage/analyze/targets/combine.py
blob: 33526379baaecd198f95399e980776ec381016af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Combine integration test target code coverage reports."""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from .... import types as t

from . import (
    CoverageAnalyzeTargetsConfig,
    get_target_index,
    make_report,
    read_report,
    write_report,
)

if t.TYPE_CHECKING:
    from . import (
        Arcs,
        IndexedPoints,
        Lines,
        TargetIndexes,
    )


def command_coverage_analyze_targets_combine(args):  # type: (CoverageAnalyzeTargetsCombineConfig) -> None
    """Combine integration test target code coverage reports."""
    combined_target_indexes = {}  # type: TargetIndexes
    combined_path_arcs = {}  # type: Arcs
    combined_path_lines = {}  # type: Lines

    for report_path in args.input_files:
        covered_targets, covered_path_arcs, covered_path_lines = read_report(report_path)

        merge_indexes(covered_path_arcs, covered_targets, combined_path_arcs, combined_target_indexes)
        merge_indexes(covered_path_lines, covered_targets, combined_path_lines, combined_target_indexes)

    report = make_report(combined_target_indexes, combined_path_arcs, combined_path_lines)

    write_report(args, report, args.output_file)


def merge_indexes(
        source_data,  # type: IndexedPoints
        source_index,  # type: t.List[str]
        combined_data,  # type: IndexedPoints
        combined_index,  # type: TargetIndexes
):  # type: (...) -> None
    """Merge indexes from the source into the combined data set (arcs or lines)."""
    for covered_path, covered_points in source_data.items():
        combined_points = combined_data.setdefault(covered_path, {})

        for covered_point, covered_target_indexes in covered_points.items():
            combined_point = combined_points.setdefault(covered_point, set())

            for covered_target_index in covered_target_indexes:
                combined_point.add(get_target_index(source_index[covered_target_index], combined_index))


class CoverageAnalyzeTargetsCombineConfig(CoverageAnalyzeTargetsConfig):
    """Configuration for the `coverage analyze targets combine` command."""
    def __init__(self, args):  # type: (t.Any) -> None
        super(CoverageAnalyzeTargetsCombineConfig, self).__init__(args)

        self.input_files = args.input_file  # type: t.List[str]
        self.output_file = args.output_file  # type: str