#!/usr/bin/env python

"""
List e3sm test suites. Can be used to show what's being tested. Can just
list tested grids, compsets, etc.
"""

from standard_script_setup import *
from CIME.utils import expect
import get_tests

import sys, argparse, os

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
usage="""\n{0} <thing-to-list> [<test category> <test category> ...] [--verbose]
OR
{0} --help

\033[1mEXAMPLES:\033[0m
    \033[1;32m# List all tested compsets \033[0m
    > {0} compsets
    \033[1;32m# List all compsets tested by e3sm_developer \033[0m
    > {0} compsets e3sm_developer
    \033[1;32m# List all grids tested by e3sm_developer \033[0m
    > {0} grid e3sm_developer
""".format(os.path.basename(args[0])),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("suites", nargs="+",
                        help="The tests suites to list. Test suites: {}".format(", ".join(get_tests.get_test_suites())))

    parser.add_argument("-t", "--thing-to-list", choices=("compsets", "grids", "testcases", "tests"), default="tests",
                        help="The thing you want to list")

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

    if (not args.suites):
        args.suites = get_tests.get_test_suites()

    return args.thing_to_list, args.suites

###############################################################################
def list_tests(thing_to_list, suites):
###############################################################################
    things = set()
    for suite in suites:
        tests = get_tests.get_test_suite(suite)
        for test in tests:
            testcase, _, grid, compset = CIME.utils.parse_test_name(test)[:4]
            if (thing_to_list == "compsets"):
                things.add(compset)
            elif (thing_to_list == "grids"):
                things.add(grid)
            elif (thing_to_list == "testcases"):
                things.add(testcase)
            elif (thing_to_list == "tests"):
                things.add(test)
            else:
                expect(False, "Unrecognized thing to list '{}'".format(thing_to_list))

    print("Tested {} for test suites: {}".format(thing_to_list, ", ".join(suites)))
    for item in sorted(things):
        print("  {}".format(item))

###############################################################################
def _main_func(description):
###############################################################################
    thing_to_list, suites = parse_command_line(sys.argv, description)

    list_tests(thing_to_list, suites)

if __name__ == "__main__":
    _main_func(__doc__)
