def collect_tox_coverage_files(targeted_packages): root_coverage_dir = os.path.join(root_dir, "_coverage/") clean_coverage(coverage_dir) # coverage report has paths starting .tox and azure # coverage combine fixes this with the help of tox.ini[coverage:paths] combine_coverage_files(targeted_packages) coverage_files = [] # generate coverage files for package_dir in [package for package in targeted_packages]: coverage_file = os.path.join(package_dir, ".coverage") if os.path.isfile(coverage_file): destination_file = os.path.join( root_coverage_dir, ".coverage_{}".format(os.path.basename(package_dir))) shutil.copyfile(coverage_file, destination_file) coverage_files.append(destination_file) logging.info( "Visible uncombined .coverage files: {}".format(coverage_files)) if len(coverage_files): cov_cmd_array = [sys.executable, "-m", "coverage", "combine"] cov_cmd_array.extend(coverage_files) # merge them with coverage combine and copy to root run_check_call(cov_cmd_array, os.path.join(root_dir, "_coverage/")) source = os.path.join(coverage_dir, "./.coverage") dest = os.path.join(root_dir, ".coverage") shutil.move(source, dest)
def collect_tox_coverage_files(targeted_packages): root_coverage_dir = os.path.join(root_dir, "_coverage/") clean_coverage(coverage_dir) # coverage combine fixes this with the help of tox.ini[coverage:paths] coverage_files = [] for package_dir in [package for package in targeted_packages]: coverage_file = os.path.join(package_dir, ".coverage") if os.path.isfile(coverage_file): destination_file = os.path.join( root_coverage_dir, ".coverage_{}".format(os.path.basename(package_dir))) shutil.copyfile(coverage_file, destination_file) coverage_files.append(destination_file) logging.info("Uploading .coverage files: {}".format(coverage_files))
def run_tests(targeted_packages, python_version, test_output_location, test_res, parsed_args): err_results = [] clean_coverage(coverage_dir) # base command array without a targeted package command_array = [python_version, "-m", "pytest"] command_array.extend(test_res) # loop through the packages logging.info("Running pytest for {}".format(targeted_packages)) for index, target_package in enumerate(targeted_packages): logging.info( "Running pytest for {}. {} of {}.".format( target_package, index, len(targeted_packages) ) ) package_name = os.path.basename(target_package) source_coverage_file = os.path.join(root_dir, ".coverage") target_coverage_file = os.path.join( coverage_dir, ".coverage_{}".format(package_name) ) target_package_options = [] allowed_return_codes = [] # Get code coverage params for current package coverage_commands = create_code_coverage_params(parsed_args, package_name) command_array.extend(coverage_commands) # if we are targeting only packages that are management plane, it is a possibility # that no tests running is an acceptable situation # we explicitly handle this here. if all( map( lambda x: any( [pkg_id in x for pkg_id in MANAGEMENT_PACKAGE_IDENTIFIERS] ), [target_package], ) ): allowed_return_codes.append(5) # format test result output location if test_output_location: target_package_options.extend( [ "--junitxml", os.path.join( "TestResults/{}/".format(package_name), test_output_location ), ] ) target_package_options.append(target_package) err_result = run_check_call( command_array + target_package_options, root_dir, allowed_return_codes, True, False, ) if err_result: logging.error("Errors present in {}".format(package_name)) err_results.append(err_result) if os.path.isfile(source_coverage_file): shutil.move(source_coverage_file, target_coverage_file) collect_pytest_coverage_files(targeted_packages) # if any of the packages failed, we should get exit with errors if err_results: exit(1)