From d31ef2125d745b8a157c9b0a8ba4d80361c68916 Mon Sep 17 00:00:00 2001 From: Gil Forcada Codinachs Date: Fri, 15 Dec 2023 21:54:44 +0000 Subject: [PATCH] feat: validate files Break hard if the configuration files have a broken syntax. This way, we ensure that we are not committing broken configuration. --- config/config-package.py | 44 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/config/config-package.py b/config/config-package.py index 8951bd9b..369c337d 100755 --- a/config/config-package.py +++ b/config/config-package.py @@ -10,10 +10,14 @@ import argparse import collections +import configparser +import editorconfig import jinja2 import pathlib import shutil import toml +import validate_pyproject +import yaml META_HINT = """\ @@ -504,6 +508,44 @@ def run_tox(self): tox_path = shutil.which('tox') or (pathlib.Path(cwd) / 'bin' / 'tox') call(tox_path, '-e', 'format,lint') + def validate_files(self, files_changed): + """Ensure that files are not broken""" + for file_obj in files_changed: + if file_obj.suffix == '.toml': + self._validate_toml(file_obj) + elif file_obj.suffix in ('.yaml', '.yml'): + self._validate_yaml(file_obj) + elif file_obj.suffix == '.ini' or file_obj.stem == '.flake8': + self._validate_ini(file_obj) + elif file_obj.stem == '.editorconfig': + self._validate_editorconfig(file_obj) + + def _validate_toml(self, file_obj): + """Validate files that are in TOML format""" + with change_dir(self.path): + data = toml.load(file_obj) + + if self.path.stem == 'pyproject': + validator = validate_pyproject.api.Validator() + validator(data) + + def _validate_yaml(self, file_obj): + """Validate files that are in YAML format""" + with change_dir(self.path): + data = file_obj.read_text() + _ = yaml.safe_load(data) + + def _validate_ini(self, file_obj): + """Validate files that are in INI format""" + config = configparser.ConfigParser() + with change_dir(self.path): + _ = config.read(file_obj) + + def _validate_editorconfig(self, file_obj): + """Validate .editorconfig file""" + with change_dir(self.path): + editorconfig.get_properties(file_obj.resolve()) + @property def _commit_msg(self): return self.args.commit_msg or 'Configuring with plone/meta' @@ -561,11 +603,11 @@ def configure(self): with change_dir(self.path): updating = git_branch(self.branch_name) + self.validate_files(files_changed) self.commit_and_push(files_changed) self.warn_on_setup_cfg() self.final_help_tips(updating) - def main(): args = handle_command_line_arguments()