chromium/tools/android/test_health/list_disabled_tests.py

#!/usr/bin/env python3
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import csv
import json
import pathlib


def main():
    parser = argparse.ArgumentParser(
        description=
        'Lists annotated disabled tests from the output of get_test_health.py,'
        ' outputs a .csv.')
    parser.add_argument('get_test_health_jsonl_path',
                        type=pathlib.Path,
                        help='input jsonl generated by get_test_health.py')
    parser.add_argument('-o',
                        '--output-file',
                        type=pathlib.Path,
                        required=True,
                        help='output CSV file path')
    args = parser.parse_args()

    lines = args.get_test_health_jsonl_path.read_text().splitlines()
    java_file_dicts = [json.loads(line) for line in lines]

    all_disabled_tests = set()
    all_disable_if_tests = set()
    for java_file_dict in java_file_dicts:
        for disabled_test in java_file_dict['disabled_tests']:
            if disabled_test in all_disabled_tests:
                raise ValueError(
                    f'Multiple @DisabledTest entries for {disabled_test}')
            all_disabled_tests.add(disabled_test)
        for disabled_test in java_file_dict['disable_if_tests']:
            if disabled_test in all_disable_if_tests:
                raise ValueError(
                    f'Multiple @DisableIf entries for {disabled_test}')
            all_disable_if_tests.add(disabled_test)

    all_disabled_tests_list = sorted(all_disabled_tests)
    all_disable_if_tests_list = sorted(all_disable_if_tests)

    with open(args.output_file, 'w') as csv_output_file:
        csv_writer = csv.writer(csv_output_file)

        for disabled_test in all_disabled_tests_list:
            csv_writer.writerow(['@DisabledTest', disabled_test])
        for disabled_test in all_disable_if_tests_list:
            csv_writer.writerow(['@DisableIf', disabled_test])


if __name__ == '__main__':
    main()