#!/usr/bin/env python3
############################################################################
# Copyright (C) SchedMD LLC.
############################################################################
import argparse
import importlib.util
import os
import pathlib
import sys

parser = argparse.ArgumentParser(description="Performs an ATF test run")
group = parser.add_mutually_exclusive_group()
group.add_argument(
    "--auto-config",
    action="store_true",
    help="the slurm configuration will be altered as needed by the test",
)
group.add_argument(
    "--local-config",
    action="store_false",
    dest="auto_config",
    help="the slurm configuration will not be altered. This is the default",
)
group.add_argument(
    "--pdb",
    action="store_true",
    dest="pdb",
    help="Use pdb to debug failures",
)
parser.add_argument(
    "--no-color", action="store_true", help="the logs won't include color"
)
parser.add_argument(
    "--allow-slurmdbd-modify",
    action="store_true",
    help="allow running in local-config even if require_accounting(modify=True)",
)
parser.add_argument("PYTEST_OPTION ...", nargs="?", help="additional pytest options")
parser.add_argument("TEST_FILE_OR_DIR ...", nargs="?", help="test files or directories")
args, unknown = parser.parse_known_args()

# Verify that pytest is installed
if importlib.util.find_spec("pytest") is None:
    sys.exit("pytest must be installed in order to run the python testsuite")

# Only highlight skips in auto-config mode
if args.auto_config:
    report_options = "A"
else:
    report_options = "fE"

atf_base_dir = str(pathlib.Path(__file__).resolve().parent)

# Change directory to the base of the ATF repo
os.chdir(atf_base_dir)

command = "pytest"
# --disable-warnings is very useful for tests using pydantic,
# logging.warning in tests are still shown
args = [
    command,
    "-s",
    f"-r{report_options}",
    "-v",
    "-p",
    "no:cacheprovider",
    "--disable-warnings",
]

if "pdb" in args:
    args.append("--pdb")

args.extend(sys.argv[1:])
os.execvp(command, args)
