def test_generates_mappings(self, generate_module_test_mappings_mock,
                             generate_project_test_mappings_mock):
     mock_evg_api = MagicMock()
     generate_project_test_mappings_mock.return_value = (
         ["mock-project-mappings"],
         "last-project-sha-analyzed",
     )
     generate_module_test_mappings_mock.return_value = (
         ["mock-module-mappings"],
         "last-module-sha-analyzed",
     )
     test_mappings_result = under_test.generate_test_mappings(
         mock_evg_api,
         "mongodb-mongo-master",
         CommitLimit(stop_at_commit_sha="some-project-commit-sha"),
         ".*src",
         ".*test",
         "my-module",
         CommitLimit(stop_at_commit_sha="some-module-commit-sha"),
         ".*src",
         ".*test",
     )
     assert test_mappings_result.test_mappings_list == [
         "mock-project-mappings",
         "mock-module-mappings",
     ]
     assert (test_mappings_result.most_recent_project_commit_analyzed ==
             "last-project-sha-analyzed")
     assert test_mappings_result.most_recent_module_commit_analyzed == "last-module-sha-analyzed"
def create(
    ctx: Context,
    evergreen_project: str,
    after: str,
    source_file_regex: str,
    test_file_regex: str,
    module_name: str,
    module_source_file_regex: str,
    module_test_file_regex: str,
    output_file: str,
) -> None:
    """Create the test mappings for a given evergreen project."""
    evg_api = ctx.obj["evg_api"]

    try:
        after_date = datetime.fromisoformat(after).replace(tzinfo=pytz.UTC)
    except ValueError:
        raise click.ClickException(
            "The after date could not be parsed - make sure it's an iso date")
        return

    if module_name:
        if not module_source_file_regex:
            raise click.ClickException(
                "A module source file regex is required when a module is being analyzed"
            )
            return
        if not module_test_file_regex:
            raise click.ClickException(
                "A module test file regex is required when a module is being analyzed"
            )
            return

    LOGGER.info(f"Creating test mappings for {evergreen_project}")

    test_mappings_result = generate_test_mappings(
        evg_api,
        evergreen_project,
        CommitLimit(stop_at_date=after_date),
        source_file_regex,
        test_file_regex,
        module_name=module_name,
        module_commit_limit=CommitLimit(stop_at_date=after_date),
        module_source_file_pattern=module_source_file_regex,
        module_test_file_pattern=module_test_file_regex,
    )

    json_dump = json.dumps(test_mappings_result.test_mappings_list, indent=4)

    if output_file:
        with open(output_file, "a") as f:
            f.write(json_dump)
    else:
        print(json_dump)

    LOGGER.info("Finished processing test mappings")
示例#3
0
def _seed_test_mappings_for_project(
    evg_api: EvergreenApi,
    mongo: MongoWrapper,
    work_item: ProjectTestMappingWorkItem,
    after_date: datetime,
    log: Any,
) -> bool:
    """
    Generate test mappings for a given work item.

    :param evg_api: An instance of the evg_api client
    :param mongo: An instance of MongoWrapper.
    :param work_item: An instance of ProjectTestMappingWorkItem.
    :param after_date: The date at which to start analyzing commits of the project.
    """
    test_mappings_result = generate_test_mappings(
        evg_api,
        work_item.project,
        CommitLimit(stop_at_date=after_date),
        work_item.source_file_regex,
        work_item.test_file_regex,
        module_name=work_item.module,
        module_commit_limit=CommitLimit(stop_at_date=after_date),
        module_source_file_pattern=work_item.module_source_file_regex,
        module_test_file_pattern=work_item.module_test_file_regex,
    )

    project_config = ProjectConfig.get(mongo.project_config(),
                                       work_item.project)
    project_config.test_config.update(
        test_mappings_result.most_recent_project_commit_analyzed,
        work_item.source_file_regex,
        work_item.test_file_regex,
        work_item.module,
        test_mappings_result.most_recent_module_commit_analyzed,
        work_item.module_source_file_regex,
        work_item.module_test_file_regex,
    )
    project_config.save(mongo.project_config())

    if test_mappings_result.test_mappings_list:
        mongo.test_mappings().insert_many(
            test_mappings_result.test_mappings_list)
    else:
        log.info("No test mappings generated")
    log.info("Finished test mapping work item processing")

    return True
def update_test_mappings_since_last_commit(evg_api: EvergreenApi,
                                           mongo: MongoWrapper) -> None:
    """
    Update test mappings that are being tracked in the test mappings project config collection.

    :param evg_api: An instance of the evg_api client
    :param mongo: An instance of MongoWrapper.
    """
    LOGGER.info("Updating test mappings")
    project_cursor = mongo.project_config().find({})
    for project_config in project_cursor:
        LOGGER.info("Updating test mappings for project",
                    project_config=project_config)
        test_config = project_config["test_config"]

        test_mappings_result = generate_test_mappings(
            evg_api,
            project_config["project"],
            CommitLimit(stop_at_commit_sha=test_config[
                "most_recent_project_commit_analyzed"]),
            test_config["source_file_regex"],
            test_config["test_file_regex"],
            module_name=test_config["module"],
            module_commit_limit=CommitLimit(stop_at_commit_sha=test_config[
                "most_recent_module_commit_analyzed"]),
            module_source_file_pattern=test_config["module_source_file_regex"],
            module_test_file_pattern=test_config["module_source_file_regex"],
        )

        project_config = ProjectConfig.get(mongo.project_config(),
                                           project_config["project"])
        project_config.test_config.update_most_recent_commits_analyzed(
            test_mappings_result.most_recent_project_commit_analyzed,
            test_mappings_result.most_recent_module_commit_analyzed,
        )
        project_config.save(mongo.project_config())

        if test_mappings_result.test_mappings_list:
            update_test_mappings(test_mappings_result.test_mappings_list,
                                 mongo)
        else:
            LOGGER.info("No test mappings generated")
    LOGGER.info("Finished test mapping updating")