#!/usr/bin/env python
import os
import os.path

import click

from sentry.utils import json


def find_test_cases_matching(model_name: str):
    manifest = json.loads(open(os.environ["SENTRY_MODEL_MANIFEST_FILE_PATH"]).read())
    for test_node_id, hits in manifest.items():
        if model_name in hits:
            yield test_node_id.split("::")[1]


@click.command()
@click.argument("target_model", required=True)
@click.argument("pytest_options", nargs=-1)
def main(target_model: str, pytest_options):
    """
    Script that uses the SENTRY_MODEL_MANIFEST_FILE_PATH path to execute tests affected by a specific model.
    """

    os.execvp(
        "pytest",
        ["pytest", "-k", " or ".join(find_test_cases_matching(target_model))]
        + list(pytest_options),
    )


if __name__ == "__main__":
    main()