| """Recipe for running pre-commit tests. |
| |
| pre-commit is a framework written in python that allows each |
| repository to define its own set of checks that will be run |
| before code is committed. It uses a git hook and a config file |
| at the root of the repository. |
| |
| This recipe looks for the config file and runs pre-commit. |
| """ |
| import copy |
| import logging |
| import os |
| |
| from collections import Counter |
| from slave import base_recipe |
| from slave.step import precommit_step |
| |
| CONFIG_FILE_DEFAULT = ".pre-commit-config.yaml" |
| CONFIG_FILE_BETA = ".pre-commit-config.beta.yaml" |
| |
| def GetValidBuildNames(): |
| return ["precommit", "precommit-beta"] |
| |
| def CreateRecipe(build_name: str, **kwargs): |
| """Defines how this recipe gets built.""" |
| if build_name.endswith("-beta"): |
| config_file = CONFIG_FILE_BETA |
| show_output = False |
| else: |
| config_file = CONFIG_FILE_DEFAULT |
| show_output = True |
| return PreCommitRecipe( |
| config_file=config_file, |
| show_output=show_output, |
| **kwargs) |
| |
| class PreCommitRecipe(base_recipe.BaseRecipe): |
| """Recipe to execute pre-commit hooks.""" |
| |
| def __init__(self, config_file, show_output, **kwargs): |
| base_recipe.BaseRecipe.__init__(self, **kwargs) |
| self._config_file = config_file |
| self._repo_root_directory = kwargs["eureka_root"] |
| self._show_output = show_output |
| |
| properties = copy.deepcopy(kwargs["properties"]) |
| self._patch_project_counter = Counter() |
| if "patch_project" not in properties: |
| raise ValueError('patch_project is a required properties attribute.') |
| self._patch_project_counter[properties["patch_project"]] += 1 |
| if "depends_on" not in properties: |
| properties["depends_on"] = [] |
| for depend in properties["depends_on"]: |
| if "project" not in depend: |
| raise ValueError("project is a required attribute from objects in depends_on list.") |
| self._patch_project_counter[depend["project"]] += 1 |
| |
| def get_steps(self): |
| result = [] |
| for patch_project in self._patch_project_counter: |
| result.append(precommit_step.PreCommitStep( |
| config_file=self._config_file, |
| repo_root_directory=self._repo_root_directory, |
| show_output=self._show_output, |
| patch_project=patch_project, |
| commit_counts=self._patch_project_counter[patch_project], |
| **self._step_kwargs)) |
| return result |