Skip to content

Commit

Permalink
feat: validate files
Browse files Browse the repository at this point in the history
Break hard if the configuration files have a broken syntax.

This way, we ensure that we are not committing broken configuration.
  • Loading branch information
gforcada committed Jan 22, 2024
1 parent 4a30b19 commit d31ef21
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion config/config-package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """\
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit d31ef21

Please sign in to comment.