@ -5,6 +5,8 @@ __metaclass__ = type
import os
import json
from . . . import types as t
from . . . target import (
walk_compile_targets ,
walk_powershell_targets ,
@ -17,7 +19,7 @@ from ...io import (
from . . . util import (
ANSIBLE_TEST_DATA_ROOT ,
display ,
find_executable ,
ApplicationError ,
)
from . . . util_common import (
@ -27,10 +29,19 @@ from ...util_common import (
write_json_test_results ,
)
from . . . executor import (
Delegate ,
)
from . . . data import (
data_context ,
)
from . import (
enumerate_python_arcs ,
enumerate_powershell_lines ,
get_collection_path_regexes ,
get_all_coverage_files ,
get_python_coverage_files ,
get_python_modules ,
get_powershell_coverage_files ,
@ -44,9 +55,28 @@ from . import (
def command_coverage_combine ( args ) :
""" Patch paths in coverage files and merge into a single file.
: type args : CoverageCo nfig
: type args : CoverageCo mbineCo nfig
: rtype : list [ str ]
"""
if args . delegate :
if args . docker or args . remote :
paths = get_all_coverage_files ( )
exported_paths = [ path for path in paths if path . endswith ( ' =coverage.combined ' ) ]
if not exported_paths :
raise ExportedCoverageDataNotFound ( )
pairs = [ ( path , os . path . relpath ( path , data_context ( ) . content . root ) ) for path in exported_paths ]
def coverage_callback ( files ) : # type: (t.List[t.Tuple[str, str]]) -> None
""" Add the coverage files to the payload file list. """
display . info ( ' Including %d exported coverage file(s) in payload. ' % len ( pairs ) , verbosity = 1 )
files . extend ( pairs )
data_context ( ) . register_payload_callback ( coverage_callback )
raise Delegate ( )
paths = _command_coverage_combine_powershell ( args ) + _command_coverage_combine_python ( args )
for path in paths :
@ -55,9 +85,18 @@ def command_coverage_combine(args):
return paths
class ExportedCoverageDataNotFound ( ApplicationError ) :
""" Exception when no combined coverage data is present yet is required. """
def __init__ ( self ) :
super ( ExportedCoverageDataNotFound , self ) . __init__ (
' Coverage data must be exported before processing with the `--docker` or `--remote` option. \n '
' Export coverage with `ansible-test coverage combine` using the `--export` option. \n '
' The exported files must be in the directory: %s / ' % ResultType . COVERAGE . relative_path )
def _command_coverage_combine_python ( args ) :
"""
: type args : CoverageConfig
: type args : CoverageCo mbineCo nfig
: rtype : list [ str ]
"""
coverage = initialize_coverage ( args )
@ -136,7 +175,7 @@ def _command_coverage_combine_python(args):
def _command_coverage_combine_powershell ( args ) :
"""
: type args : CoverageCo nfig
: type args : CoverageCo mbineCo nfig
: rtype : list [ str ]
"""
coverage_files = get_powershell_coverage_files ( )
@ -217,7 +256,7 @@ def _command_coverage_combine_powershell(args):
def _get_coverage_targets ( args , walk_func ) :
"""
: type args : CoverageCo nfig
: type args : CoverageCo mbineCo nfig
: type walk_func : Func
: rtype : list [ tuple [ str , int ] ]
"""
@ -240,7 +279,7 @@ def _get_coverage_targets(args, walk_func):
def _build_stub_groups ( args , sources , default_stub_value ) :
"""
: type args : CoverageCo nfig
: type args : CoverageCo mbineCo nfig
: type sources : List [ tuple [ str , int ] ]
: type default_stub_value : Func [ List [ str ] ]
: rtype : dict
@ -273,7 +312,7 @@ def _build_stub_groups(args, sources, default_stub_value):
def get_coverage_group ( args , coverage_file ) :
"""
: type args : CoverageCo nfig
: type args : CoverageCo mbineCo nfig
: type coverage_file : str
: rtype : str
"""
@ -306,3 +345,16 @@ def get_coverage_group(args, coverage_file):
group = group . lstrip ( ' = ' )
return group
class CoverageCombineConfig ( CoverageConfig ) :
""" Configuration for the coverage combine command. """
def __init__ ( self , args ) : # type: (t.Any) -> None
super ( CoverageCombineConfig , self ) . __init__ ( args )
self . group_by = frozenset ( args . group_by ) if args . group_by else frozenset ( ) # type: t.FrozenSet[str]
self . all = args . all # type: bool
self . stub = args . stub # type: bool
# only available to coverage combine
self . export = args . export if ' export ' in args else False # type: str