import re


def get_opencover_args(config, testlist):
    """
    Builds an OpenCover command according to the configuration file and list of tests provided.

    :param config: path to configuration file
    :param testlist: path to file with list of tests to run
    :return: an OpenCover command and the id of the list of tests
    """
    # Load relevant data from config
    args = [
        f" -target: {config['runner']}",
        f" -targetargs:{' '.join(config['runner_args'])} {testlist}",
        f" -threshold:{config['threshold']} ",
        " -hideskipped:All ",
        " -mergebyhash ",
        #        " -skipautoprops ",
        f" -filter:{' '.join(config['filters'])} ",
        f" -coverbytest:{';'.join(config['cover_by_test'])} ",
        f" -searchdirs: {config['searchdirs_path']} ",
        " -register:user ",
    ]
    testlist_id = re.search(re.escape(config["runlists_path"]) + r"(.*).in", testlist).group(1)

    # Build OpenCover command with arguments
    command = [config["opencover_exec"]]
    command.extend(args)
    command.append(f"-output:{config['reports_path']}refactor_{testlist_id}.xml")
    return command, testlist_id