<commit_before># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import time
import unittest

# TODO(dpranke): This code is largely cloned from, and redundant with,
# src/mojo/tools/run_mojo_python_tests.py, and also duplicates logic
# in test-webkitpy and run-webkit-tests. We should consolidate the
# python TestResult parsing/converting/uploading code as much as possible.

def AddOptions(parser):
  parser.add_option('--metadata', action='append', default=[],
                    help=('optional key=value metadata that will be stored '
                          'in the results files (can be used for revision '
                          'numbers, etc.)'))
  parser.add_option('--write-full-results-to', metavar='FILENAME',
                    action='store',
                    help='path to write the list of full results to.')


def ValidateArgs(parser, args):
  for val in args.metadata:
    if '=' not in val:
      parser.error('Error: malformed metadata "%s"' % val)


def WriteandUploadResultsIfNecessary(args, test_suite, result):
  if not args.write_full_results_to:
    return

  full_results = _FullResults(test_suite, result, args.metadata)

  with open(args.write_full_results_to, 'w') as fp:
    json.dump(full_results, fp, indent=2)
    fp.write("\n")

  # TODO(dpranke): upload to test-results.appspot.com if requested as well.

TEST_SEPARATOR = '.'


def _FullResults(suite, result, metadata):
  """Convert the unittest results to the Chromium JSON test result format.

  This matches run-webkit-tests (the layout tests) and the flakiness dashboard.
  """

  full_results = {}
  full_results['interrupted'] = False
  full_results['path_delimiter'] = TEST_SEPARATOR
  full_results['version'] = 3
  full_results['seconds_since_epoch'] = time.time()
  for md in metadata:
    key, val = md.split('=', 1)
    full_results[key] = val

  all_test_names = _AllTestNames(suite)
  failed_test_names = _FailedTestNames(result)

  full_results['num_failures_by_type'] = {
      'Failure': len(failed_test_names),
      'Pass': len(all_test_names) - len(failed_test_names),
  }

  full_results['tests'] = {}

  for test_name in all_test_names:
    value = {
        'expected': 'PASS',
        'actual': 'FAIL' if (test_name in failed_test_names) else 'PASS',
    }
    _AddPathToTrie(full_results['tests'], test_name, value)
<commit_msg>Revert 286584 "Revert 286504 "Fix typos in num_failures_by_type ..."<commit_after># Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import time
import unittest

# TODO(dpranke): This code is largely cloned from, and redundant with,
# src/mojo/tools/run_mojo_python_tests.py, and also duplicates logic
# in test-webkitpy and run-webkit-tests. We should consolidate the
# python TestResult parsing/converting/uploading code as much as possible.

def AddOptions(parser):
  parser.add_option('--metadata', action='append', default=[],
                    help=('optional key=value metadata that will be stored '
                          'in the results files (can be used for revision '
                          'numbers, etc.)'))
  parser.add_option('--write-full-results-to', metavar='FILENAME',
                    action='store',
                    help='path to write the list of full results to.')


def ValidateArgs(parser, args):
  for val in args.metadata:
    if '=' not in val:
      parser.error('Error: malformed metadata "%s"' % val)


def WriteandUploadResultsIfNecessary(args, test_suite, result):
  if not args.write_full_results_to:
    return

  full_results = _FullResults(test_suite, result, args.metadata)

  with open(args.write_full_results_to, 'w') as fp:
    json.dump(full_results, fp, indent=2)
    fp.write("\n")

  # TODO(dpranke): upload to test-results.appspot.com if requested as well.

TEST_SEPARATOR = '.'


def _FullResults(suite, result, metadata):
  """Convert the unittest results to the Chromium JSON test result format.

  This matches run-webkit-tests (the layout tests) and the flakiness dashboard.
  """

  full_results = {}
  full_results['interrupted'] = False
  full_results['path_delimiter'] = TEST_SEPARATOR
  full_results['version'] = 3
  full_results['seconds_since_epoch'] = time.time()
  for md in metadata:
    key, val = md.split('=', 1)
    full_results[key] = val

  all_test_names = _AllTestNames(suite)
  failed_test_names = _FailedTestNames(result)

  full_results['num_failures_by_type'] = {
      'FAIL': len(failed_test_names),
      'PASS': len(all_test_names) - len(failed_test_names),
  }

  full_results['tests'] = {}

  for test_name in all_test_names:
    value = {
        'expected': 'PASS',
        'actual': 'FAIL' if (test_name in failed_test_names) else 'PASS',
    }
    _AddPathToTrie(full_results['tests'], test_name, value)

{'fraction_non_alphanumeric': 0.10915633746501399, 'fraction_numerical': 0.005197920831667333, 'mean_word_length': 3.5481818181818183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239845', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1525, 'n_words': 446}