tools/code_coverage/coverage.py
def _GetTargetProfDataPathsByExecutingCommands(targets, commands):
  """Runs commands and returns the relative paths to the profraw data files.

  Args:
    targets: A list of targets built with coverage instrumentation.
    commands: A list of commands used to run the targets.

  Returns:
    A list of relative paths to the generated profraw data files.
  """
  logging.debug('Executing the test commands.')

  # Remove existing profraw data files.
  report_root_dir = coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR)
  for file_or_dir in os.listdir(report_root_dir):
    if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
      os.remove(os.path.join(report_root_dir, file_or_dir))

  # Ensure that logs directory exists.
  if not os.path.exists(_GetLogsDirectoryPath()):
    os.makedirs(_GetLogsDirectoryPath())

  profdata_file_paths = []

  # Run all test targets to generate profraw data files.
  for target, command in zip(targets, commands):
    output_file_name = os.extsep.join([target + '_output', 'log'])
    output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name)

    profdata_file_path = None
    for _ in range(MERGE_RETRIES):
      logging.info('Running command: "%s", the output is redirected to "%s".',
                   command, output_file_path)

      if _IsIOSCommand(command):
        # On iOS platform, due to lack of write permissions, profraw files are
        # generated outside of the OUTPUT_DIR, and the exact paths are contained
        # in the output of the command execution.
        output = _ExecuteIOSCommand(command, output_file_path)
      else:
        # On other platforms, profraw files are generated inside the OUTPUT_DIR.
        output = _ExecuteCommand(target, command, output_file_path)

      profraw_file_paths = []
      if _IsIOS():
        profraw_file_paths = [_GetProfrawDataFileByParsingOutput(output)]
      elif _IsAndroid():
        android_coverage_dir = os.path.join(BUILD_DIR, 'coverage')
        for r, _, files in os.walk(android_coverage_dir):
          for f in files:
            if f.endswith(PROFRAW_FILE_EXTENSION):
              profraw_file_paths.append(os.path.join(r, f))
      else:
        for file_or_dir in os.listdir(report_root_dir):
          if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
            profraw_file_paths.append(
                os.path.join(report_root_dir, file_or_dir))

      assert profraw_file_paths, (
          'Running target "%s" failed to generate any profraw data file, '
          'please make sure the binary exists, is properly instrumented and '
          'does not crash. %s' % (target, FILE_BUG_MESSAGE))

      assert isinstance(profraw_file_paths, list), (
          'Variable \'profraw_file_paths\' is expected to be of type \'list\', '
          'but it is a %s. %s' % (type(profraw_file_paths), FILE_BUG_MESSAGE))

      try:
        profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles(
            target, profraw_file_paths)
        break
      except Exception:
        logging.info('Retrying...')
      finally:
        # Remove profraw files now so that they are not used in next iteration.
        for profraw_file_path in profraw_file_paths:
          os.remove(profraw_file_path)

    assert profdata_file_path, (
        'Failed to merge target "%s" profraw files after %d retries. %s' %
        (target, MERGE_RETRIES, FILE_BUG_MESSAGE))
    profdata_file_paths.append(profdata_file_path)

  logging.debug('Finished executing the test commands.')

  return profdata_file_paths
Runs commands and returns the relative paths to the profraw data files.

  Args:
    targets: A list of targets built with coverage instrumentation.
    commands: A list of commands used to run the targets.

  Returns:
    A list of relative paths to the generated profraw data files.
  
Runs commands and returns the relative paths to the profraw data files.
Runs commands and returns the relative paths to the profraw data files .
def _GetTargetProfDataPathsByExecutingCommands(targets, commands):
  logging.debug('Executing the test commands.')
  report_root_dir = coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR)
  for file_or_dir in os.listdir(report_root_dir):
    if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
      os.remove(os.path.join(report_root_dir, file_or_dir))
  if not os.path.exists(_GetLogsDirectoryPath()):
    os.makedirs(_GetLogsDirectoryPath())
  profdata_file_paths = []
  for target, command in zip(targets, commands):
    output_file_name = os.extsep.join([target + '_output', 'log'])
    output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name)
    profdata_file_path = None
    for _ in range(MERGE_RETRIES):
      logging.info('Running command: "%s", the output is redirected to "%s".',
                   command, output_file_path)
      if _IsIOSCommand(command):
        output = _ExecuteIOSCommand(command, output_file_path)
      else:
        output = _ExecuteCommand(target, command, output_file_path)
      profraw_file_paths = []
      if _IsIOS():
        profraw_file_paths = [_GetProfrawDataFileByParsingOutput(output)]
      elif _IsAndroid():
        android_coverage_dir = os.path.join(BUILD_DIR, 'coverage')
        for r, _, files in os.walk(android_coverage_dir):
          for f in files:
            if f.endswith(PROFRAW_FILE_EXTENSION):
              profraw_file_paths.append(os.path.join(r, f))
      else:
        for file_or_dir in os.listdir(report_root_dir):
          if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
            profraw_file_paths.append(
                os.path.join(report_root_dir, file_or_dir))
      assert profraw_file_paths, (
          'Running target "%s" failed to generate any profraw data file, '
          'please make sure the binary exists, is properly instrumented and '
          'does not crash. %s' % (target, FILE_BUG_MESSAGE))
      assert isinstance(profraw_file_paths, list), (
          'Variable \'profraw_file_paths\' is expected to be of type \'list\', '
          'but it is a %s. %s' % (type(profraw_file_paths), FILE_BUG_MESSAGE))
      try:
        profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles(
            target, profraw_file_paths)
        break
      except Exception:
        logging.info('Retrying...')
      finally:
        for profraw_file_path in profraw_file_paths:
          os.remove(profraw_file_path)
    assert profdata_file_path, (
        'Failed to merge target "%s" profraw files after %d retries. %s' %
        (target, MERGE_RETRIES, FILE_BUG_MESSAGE))
    profdata_file_paths.append(profdata_file_path)
  logging.debug('Finished executing the test commands.')
  return profdata_file_paths
def _GetTargetProfDataPathsByExecutingCommands ( targets , commands ) : logging . debug ( 'Executing the test commands.' ) report_root_dir = coverage_utils . GetCoverageReportRootDirPath ( OUTPUT_DIR ) for file_or_dir in os . listdir ( report_root_dir ) : if file_or_dir . endswith ( PROFRAW_FILE_EXTENSION ) : os . remove ( os . path . join ( report_root_dir , file_or_dir ) ) if not os . path . exists ( _GetLogsDirectoryPath ( ) ) : os . makedirs ( _GetLogsDirectoryPath ( ) ) profdata_file_paths = [ ] for target , command in zip ( targets , commands ) : output_file_name = os . extsep . join ( [ target + '_output' , 'log' ] ) output_file_path = os . path . join ( _GetLogsDirectoryPath ( ) , output_file_name ) profdata_file_path = None for _ in range ( MERGE_RETRIES ) : logging . info ( 'Running command: "%s", the output is redirected to "%s".' , command , output_file_path ) if _IsIOSCommand ( command ) : output = _ExecuteIOSCommand ( command , output_file_path ) else : output = _ExecuteCommand ( target , command , output_file_path ) profraw_file_paths = [ ] if _IsIOS ( ) : profraw_file_paths = [ _GetProfrawDataFileByParsingOutput ( output ) ] elif _IsAndroid ( ) : android_coverage_dir = os . path . join ( BUILD_DIR , 'coverage' ) for r , _ , files in os . walk ( android_coverage_dir ) : for f in files : if f . endswith ( PROFRAW_FILE_EXTENSION ) : profraw_file_paths . append ( os . path . join ( r , f ) ) else : for file_or_dir in os . listdir ( report_root_dir ) : if file_or_dir . endswith ( PROFRAW_FILE_EXTENSION ) : profraw_file_paths . append ( os . path . join ( report_root_dir , file_or_dir ) ) assert profraw_file_paths , ( 'Running target "%s" failed to generate any profraw data file, ' 'please make sure the binary exists, is properly instrumented and ' 'does not crash. %s' % ( target , FILE_BUG_MESSAGE ) ) assert isinstance ( profraw_file_paths , list ) , ( 'Variable \'profraw_file_paths\' is expected to be of type \'list\', ' 'but it is a %s. %s' % ( type ( profraw_file_paths ) , FILE_BUG_MESSAGE ) ) try : profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles ( target , profraw_file_paths ) break except Exception : logging . info ( 'Retrying...' ) finally : for profraw_file_path in profraw_file_paths : os . remove ( profraw_file_path ) assert profdata_file_path , ( 'Failed to merge target "%s" profraw files after %d retries. %s' % ( target , MERGE_RETRIES , FILE_BUG_MESSAGE ) ) profdata_file_paths . append ( profdata_file_path ) logging . debug ( 'Finished executing the test commands.' ) return profdata_file_paths
Runs commands and returns the relative paths to the profraw data files.
Runs commands and returns the relative paths to the profraw data files .
"""Runs commands and returns the relative paths to the profraw data files.

  Args:
    targets: A list of targets built with coverage instrumentation.
    commands: A list of commands used to run the targets.

  Returns:
    A list of relative paths to the generated profraw data files.
  """ # Remove existing profraw data files. # Ensure that logs directory exists. # Run all test targets to generate profraw data files. # On iOS platform, due to lack of write permissions, profraw files are # generated outside of the OUTPUT_DIR, and the exact paths are contained # in the output of the command execution. # On other platforms, profraw files are generated inside the OUTPUT_DIR. # Remove profraw files now so that they are not used in next iteration.
A list of relative paths to the generated profraw data files.
A list of relative paths to the generated profraw data files .
targets
A list of targets built with coverage instrumentation.
A list of targets built with coverage instrumentation .
commands
A list of commands used to run the targets.
A list of commands used to run the targets .