mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
11 KiB
Python
334 lines
11 KiB
Python
8 years ago
|
"""Code coverage utilities."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
|
import os
|
||
|
import re
|
||
|
|
||
5 years ago
|
from .target import (
|
||
8 years ago
|
walk_module_targets,
|
||
8 years ago
|
walk_compile_targets,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util import (
|
||
8 years ago
|
display,
|
||
|
ApplicationError,
|
||
8 years ago
|
common_environment,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util_common import (
|
||
5 years ago
|
run_command,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .config import (
|
||
8 years ago
|
CoverageConfig,
|
||
|
CoverageReportConfig,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .executor import (
|
||
8 years ago
|
Delegate,
|
||
|
install_command_requirements,
|
||
|
)
|
||
8 years ago
|
|
||
5 years ago
|
from .data import (
|
||
5 years ago
|
data_context,
|
||
|
)
|
||
|
|
||
8 years ago
|
COVERAGE_DIR = 'test/results/coverage'
|
||
|
COVERAGE_FILE = os.path.join(COVERAGE_DIR, 'coverage')
|
||
8 years ago
|
COVERAGE_GROUPS = ('command', 'target', 'environment', 'version')
|
||
8 years ago
|
|
||
|
|
||
|
def command_coverage_combine(args):
|
||
|
"""Patch paths in coverage files and merge into a single file.
|
||
|
:type args: CoverageConfig
|
||
8 years ago
|
:rtype: list[str]
|
||
8 years ago
|
"""
|
||
|
coverage = initialize_coverage(args)
|
||
|
|
||
6 years ago
|
modules = dict((t.module, t.path) for t in list(walk_module_targets()) if t.path.endswith('.py'))
|
||
8 years ago
|
|
||
8 years ago
|
coverage_files = [os.path.join(COVERAGE_DIR, f) for f in os.listdir(COVERAGE_DIR) if '=coverage.' in f]
|
||
8 years ago
|
|
||
|
ansible_path = os.path.abspath('lib/ansible/') + '/'
|
||
5 years ago
|
root_path = data_context().content.root + '/'
|
||
8 years ago
|
|
||
8 years ago
|
counter = 0
|
||
8 years ago
|
groups = {}
|
||
8 years ago
|
|
||
8 years ago
|
if args.all or args.stub:
|
||
|
sources = sorted(os.path.abspath(target.path) for target in walk_compile_targets())
|
||
|
else:
|
||
|
sources = []
|
||
|
|
||
|
if args.stub:
|
||
6 years ago
|
stub_group = []
|
||
|
stub_groups = [stub_group]
|
||
|
stub_line_limit = 500000
|
||
|
stub_line_count = 0
|
||
|
|
||
|
for source in sources:
|
||
|
with open(source, 'r') as source_fd:
|
||
|
source_line_count = len(source_fd.read().splitlines())
|
||
|
|
||
|
stub_group.append(source)
|
||
|
stub_line_count += source_line_count
|
||
|
|
||
|
if stub_line_count > stub_line_limit:
|
||
|
stub_line_count = 0
|
||
|
stub_group = []
|
||
|
stub_groups.append(stub_group)
|
||
|
|
||
|
for stub_index, stub_group in enumerate(stub_groups):
|
||
|
if not stub_group:
|
||
|
continue
|
||
|
|
||
|
groups['=stub-%02d' % (stub_index + 1)] = dict((source, set()) for source in stub_group)
|
||
8 years ago
|
|
||
5 years ago
|
if data_context().content.collection:
|
||
|
collection_search_re = re.compile(r'/%s/' % data_context().content.collection.directory)
|
||
|
collection_sub_re = re.compile(r'^.*?/%s/' % data_context().content.collection.directory)
|
||
|
else:
|
||
|
collection_search_re = None
|
||
|
collection_sub_re = None
|
||
|
|
||
8 years ago
|
for coverage_file in coverage_files:
|
||
8 years ago
|
counter += 1
|
||
|
display.info('[%4d/%4d] %s' % (counter, len(coverage_files), coverage_file), verbosity=2)
|
||
|
|
||
8 years ago
|
original = coverage.CoverageData()
|
||
|
|
||
8 years ago
|
group = get_coverage_group(args, coverage_file)
|
||
|
|
||
|
if group is None:
|
||
|
display.warning('Unexpected name for coverage file: %s' % coverage_file)
|
||
|
continue
|
||
|
|
||
8 years ago
|
if os.path.getsize(coverage_file) == 0:
|
||
|
display.warning('Empty coverage file: %s' % coverage_file)
|
||
|
continue
|
||
|
|
||
|
try:
|
||
|
original.read_file(coverage_file)
|
||
|
except Exception as ex: # pylint: disable=locally-disabled, broad-except
|
||
6 years ago
|
display.error(u'%s' % ex)
|
||
8 years ago
|
continue
|
||
|
|
||
|
for filename in original.measured_files():
|
||
8 years ago
|
arcs = set(original.arcs(filename) or [])
|
||
|
|
||
|
if not arcs:
|
||
|
# This is most likely due to using an unsupported version of coverage.
|
||
|
display.warning('No arcs found for "%s" in coverage file: %s' % (filename, coverage_file))
|
||
|
continue
|
||
8 years ago
|
|
||
|
if '/ansible_modlib.zip/ansible/' in filename:
|
||
7 years ago
|
# Rewrite the module_utils path from the remote host to match the controller. Ansible 2.6 and earlier.
|
||
8 years ago
|
new_name = re.sub('^.*/ansible_modlib.zip/ansible/', ansible_path, filename)
|
||
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
5 years ago
|
elif collection_search_re and collection_search_re.search(filename):
|
||
|
new_name = os.path.abspath(collection_sub_re.sub('', filename))
|
||
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
7 years ago
|
elif re.search(r'/ansible_[^/]+_payload\.zip/ansible/', filename):
|
||
|
# Rewrite the module_utils path from the remote host to match the controller. Ansible 2.7 and later.
|
||
|
new_name = re.sub(r'^.*/ansible_[^/]+_payload\.zip/ansible/', ansible_path, filename)
|
||
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
8 years ago
|
elif '/ansible_module_' in filename:
|
||
7 years ago
|
# Rewrite the module path from the remote host to match the controller. Ansible 2.6 and earlier.
|
||
7 years ago
|
module_name = re.sub('^.*/ansible_module_(?P<module>.*).py$', '\\g<module>', filename)
|
||
|
if module_name not in modules:
|
||
|
display.warning('Skipping coverage of unknown module: %s' % module_name)
|
||
8 years ago
|
continue
|
||
7 years ago
|
new_name = os.path.abspath(modules[module_name])
|
||
8 years ago
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
7 years ago
|
elif re.search(r'/ansible_[^/]+_payload(_[^/]+|\.zip)/__main__\.py$', filename):
|
||
|
# Rewrite the module path from the remote host to match the controller. Ansible 2.7 and later.
|
||
|
# AnsiballZ versions using zipimporter will match the `.zip` portion of the regex.
|
||
|
# AnsiballZ versions not using zipimporter will match the `_[^/]+` portion of the regex.
|
||
|
module_name = re.sub(r'^.*/ansible_(?P<module>[^/]+)_payload(_[^/]+|\.zip)/__main__\.py$', '\\g<module>', filename).rstrip('_')
|
||
|
if module_name not in modules:
|
||
|
display.warning('Skipping coverage of unknown module: %s' % module_name)
|
||
|
continue
|
||
|
new_name = os.path.abspath(modules[module_name])
|
||
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
8 years ago
|
elif re.search('^(/.*?)?/root/ansible/', filename):
|
||
7 years ago
|
# Rewrite the path of code running on a remote host or in a docker container as root.
|
||
8 years ago
|
new_name = re.sub('^(/.*?)?/root/ansible/', root_path, filename)
|
||
8 years ago
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
6 years ago
|
elif '/.ansible/test/tmp/' in filename:
|
||
|
# Rewrite the path of code running from an integration test temporary directory.
|
||
|
new_name = re.sub(r'^.*/\.ansible/test/tmp/[^/]+/', root_path, filename)
|
||
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
||
|
filename = new_name
|
||
8 years ago
|
|
||
8 years ago
|
if group not in groups:
|
||
|
groups[group] = {}
|
||
|
|
||
|
arc_data = groups[group]
|
||
|
|
||
8 years ago
|
if filename not in arc_data:
|
||
8 years ago
|
arc_data[filename] = set()
|
||
8 years ago
|
|
||
8 years ago
|
arc_data[filename].update(arcs)
|
||
8 years ago
|
|
||
8 years ago
|
output_files = []
|
||
6 years ago
|
invalid_path_count = 0
|
||
|
invalid_path_chars = 0
|
||
8 years ago
|
|
||
8 years ago
|
for group in sorted(groups):
|
||
|
arc_data = groups[group]
|
||
8 years ago
|
|
||
8 years ago
|
updated = coverage.CoverageData()
|
||
8 years ago
|
|
||
8 years ago
|
for filename in arc_data:
|
||
|
if not os.path.isfile(filename):
|
||
5 years ago
|
if collection_search_re and collection_search_re.search(filename) and os.path.basename(filename) == '__init__.py':
|
||
|
# the collection loader uses implicit namespace packages, so __init__.py does not need to exist on disk
|
||
|
continue
|
||
|
|
||
6 years ago
|
invalid_path_count += 1
|
||
|
invalid_path_chars += len(filename)
|
||
|
|
||
|
if args.verbosity > 1:
|
||
|
display.warning('Invalid coverage path: %s' % filename)
|
||
|
|
||
8 years ago
|
continue
|
||
|
|
||
|
updated.add_arcs({filename: list(arc_data[filename])})
|
||
|
|
||
8 years ago
|
if args.all:
|
||
|
updated.add_arcs(dict((source, []) for source in sources))
|
||
|
|
||
8 years ago
|
if not args.explain:
|
||
|
output_file = COVERAGE_FILE + group
|
||
|
updated.write_file(output_file)
|
||
|
output_files.append(output_file)
|
||
|
|
||
6 years ago
|
if invalid_path_count > 0:
|
||
|
display.warning('Ignored %d characters from %d invalid coverage path(s).' % (invalid_path_chars, invalid_path_count))
|
||
|
|
||
8 years ago
|
return sorted(output_files)
|
||
8 years ago
|
|
||
|
|
||
|
def command_coverage_report(args):
|
||
|
"""
|
||
8 years ago
|
:type args: CoverageReportConfig
|
||
8 years ago
|
"""
|
||
8 years ago
|
output_files = command_coverage_combine(args)
|
||
|
|
||
|
for output_file in output_files:
|
||
8 years ago
|
if args.group_by or args.stub:
|
||
8 years ago
|
display.info('>>> Coverage Group: %s' % ' '.join(os.path.basename(output_file).split('=')[1:]))
|
||
|
|
||
8 years ago
|
options = []
|
||
|
|
||
|
if args.show_missing:
|
||
|
options.append('--show-missing')
|
||
|
|
||
7 years ago
|
if args.include:
|
||
|
options.extend(['--include', args.include])
|
||
|
|
||
|
if args.omit:
|
||
|
options.extend(['--omit', args.omit])
|
||
|
|
||
8 years ago
|
env = common_environment()
|
||
|
env.update(dict(COVERAGE_FILE=output_file))
|
||
8 years ago
|
run_command(args, env=env, cmd=['coverage', 'report'] + options)
|
||
8 years ago
|
|
||
|
|
||
|
def command_coverage_html(args):
|
||
|
"""
|
||
|
:type args: CoverageConfig
|
||
|
"""
|
||
8 years ago
|
output_files = command_coverage_combine(args)
|
||
|
|
||
|
for output_file in output_files:
|
||
|
dir_name = 'test/results/reports/%s' % os.path.basename(output_file)
|
||
|
env = common_environment()
|
||
|
env.update(dict(COVERAGE_FILE=output_file))
|
||
7 years ago
|
run_command(args, env=env, cmd=['coverage', 'html', '-i', '-d', dir_name])
|
||
8 years ago
|
|
||
|
|
||
|
def command_coverage_xml(args):
|
||
|
"""
|
||
|
:type args: CoverageConfig
|
||
|
"""
|
||
8 years ago
|
output_files = command_coverage_combine(args)
|
||
|
|
||
|
for output_file in output_files:
|
||
|
xml_name = 'test/results/reports/%s.xml' % os.path.basename(output_file)
|
||
|
env = common_environment()
|
||
|
env.update(dict(COVERAGE_FILE=output_file))
|
||
7 years ago
|
run_command(args, env=env, cmd=['coverage', 'xml', '-i', '-o', xml_name])
|
||
8 years ago
|
|
||
|
|
||
|
def command_coverage_erase(args):
|
||
|
"""
|
||
|
:type args: CoverageConfig
|
||
|
"""
|
||
|
initialize_coverage(args)
|
||
|
|
||
|
for name in os.listdir(COVERAGE_DIR):
|
||
8 years ago
|
if not name.startswith('coverage') and '=coverage.' not in name:
|
||
8 years ago
|
continue
|
||
|
|
||
|
path = os.path.join(COVERAGE_DIR, name)
|
||
|
|
||
|
if not args.explain:
|
||
|
os.remove(path)
|
||
|
|
||
|
|
||
|
def initialize_coverage(args):
|
||
|
"""
|
||
|
:type args: CoverageConfig
|
||
|
:rtype: coverage
|
||
|
"""
|
||
|
if args.delegate:
|
||
|
raise Delegate()
|
||
|
|
||
|
if args.requirements:
|
||
|
install_command_requirements(args)
|
||
|
|
||
|
try:
|
||
|
import coverage
|
||
|
except ImportError:
|
||
|
coverage = None
|
||
|
|
||
|
if not coverage:
|
||
|
raise ApplicationError('You must install the "coverage" python module to use this command.')
|
||
|
|
||
|
return coverage
|
||
|
|
||
|
|
||
8 years ago
|
def get_coverage_group(args, coverage_file):
|
||
|
"""
|
||
|
:type args: CoverageConfig
|
||
|
:type coverage_file: str
|
||
|
:rtype: str
|
||
|
"""
|
||
|
parts = os.path.basename(coverage_file).split('=', 4)
|
||
|
|
||
|
if len(parts) != 5 or not parts[4].startswith('coverage.'):
|
||
|
return None
|
||
|
|
||
|
names = dict(
|
||
|
command=parts[0],
|
||
|
target=parts[1],
|
||
|
environment=parts[2],
|
||
|
version=parts[3],
|
||
|
)
|
||
|
|
||
|
group = ''
|
||
|
|
||
|
for part in COVERAGE_GROUPS:
|
||
|
if part in args.group_by:
|
||
|
group += '=%s' % names[part]
|
||
|
|
||
|
return group
|