test_health_exporter.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Lint as: python3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import json
  6. import logging
  7. import pathlib
  8. import sys
  9. from typing import Dict, List, Union
  10. from java_test_utils import JavaTestHealth
  11. from test_health_extractor import TestHealthInfo, GitRepoInfo
  12. JsonSafeDict = Dict[str, Union[str, int]]
  13. def to_json_file(test_health_list: List[TestHealthInfo],
  14. output_path: pathlib.Path) -> None:
  15. """Exports test health information to a newline-delimited JSON file.
  16. Each line of the output file is an independent JSON object. This format is
  17. suitable for importing into BigQuery and is also known as JSON Lines
  18. (http://jsonlines.org/).
  19. Args:
  20. test_health_list:
  21. The list of `TestHealthInfo` objects to write as JSON.
  22. output_path:
  23. The path at which to create or overwrite the JSON output.
  24. """
  25. test_health_dicts = _to_test_health_dicts(test_health_list)
  26. with open(output_path, 'w') as json_file:
  27. for test_health in test_health_dicts:
  28. json.dump(test_health, json_file, allow_nan=False)
  29. json_file.write('\n')
  30. def _to_test_health_dicts(test_health_list: List[TestHealthInfo]
  31. ) -> List[JsonSafeDict]:
  32. """Transforms a list of `TestHealthInfo` into dicts of JSON-safe data."""
  33. java_test_health_list = []
  34. for test_health in test_health_list:
  35. if test_health.java_test_health:
  36. java_test_health_list.append(_to_test_health_dict(test_health))
  37. else:
  38. logging.warning(
  39. f'Skipped non-Java test "{test_health.test_name}"; currently'
  40. 'only Java tests are supported.')
  41. return java_test_health_list
  42. def _to_test_health_dict(test_health_info: TestHealthInfo) -> JsonSafeDict:
  43. """Transforms a `TestHealthInfo` into a dict of JSON-safe data."""
  44. test_health_dict: JsonSafeDict = dict(
  45. test_name=test_health_info.test_name,
  46. test_path=str(test_health_info.test_dir),
  47. test_filename=test_health_info.test_filename,
  48. )
  49. if test_health_info.java_test_health:
  50. test_health_dict.update(
  51. _to_java_test_health_dict(test_health_info.java_test_health))
  52. else:
  53. test_health_dict.update(test_type='UNKNOWN')
  54. test_health_dict.update(
  55. _to_git_repo_info_dict(test_health_info.git_repo_info))
  56. return test_health_dict
  57. def _to_java_test_health_dict(java_test_health: JavaTestHealth
  58. ) -> JsonSafeDict:
  59. """Transforms a `JavaTestHealth` into a dict of JSON-safe data."""
  60. test_dict: JsonSafeDict = dict(test_type='JAVA')
  61. if java_test_health.java_package:
  62. test_dict.update(dict(java_package=java_test_health.java_package))
  63. test_dict.update(
  64. dict(disabled_tests_count=java_test_health.disabled_tests_count,
  65. disable_if_tests_count=java_test_health.disable_if_tests_count,
  66. flaky_tests_count=java_test_health.flaky_tests_count))
  67. return test_dict
  68. def _to_git_repo_info_dict(git_repo_info: GitRepoInfo) -> JsonSafeDict:
  69. """Transforms a `GitRepoInfo` into a dict of JSON-safe data."""
  70. return dict(git_head_hash=git_repo_info.git_head,
  71. git_head_timestamp=git_repo_info.git_head_time.isoformat(
  72. timespec='microseconds'))