| """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 logging |
| import os |
| |
| from slave import base_recipe |
| from slave.step import precommit_step |
| from slave.step import py_env_setup_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 |
| |
| def get_steps(self): |
| return [ |
| py_env_setup_step.PyEnvSetupStep( |
| py3=True, |
| requirements_project="continuous-tests", |
| requirements_path="configs/pre-commit/requirements.txt", |
| **self._step_kwargs), |
| precommit_step.PreCommitStep( |
| config_file=self._config_file, |
| repo_root_directory=self._repo_root_directory, |
| show_output=self._show_output, |
| **self._step_kwargs), |
| ] |