From ddbd7d0e5e8afcce7d691ca44b09a5978e35ca8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Avil=C3=A9s?= Date: Fri, 22 Sep 2023 12:27:52 +0200 Subject: [PATCH] Add support for .header.yml config files --- CHANGELOG.md | 4 ++++ src/unbeheader/config.py | 11 +++++++++-- tests/test_config.py | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eceb8d..1b746dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.3.0 (unreleased) + +- Added support for `.header.yml` config files. + ## v1.2.0 - Renamed CLI `--ci` flag to `--check`. diff --git a/src/unbeheader/config.py b/src/unbeheader/config.py index cd9c1d9..1927c8f 100644 --- a/src/unbeheader/config.py +++ b/src/unbeheader/config.py @@ -15,6 +15,7 @@ # The name of the files containing header configuration CONFIG_FILE_NAME = '.header.yaml' +CONFIG_FILE_NAME_YML = '.header.yml' def get_config(path: Path, end_year: int) -> ConfigDict: @@ -30,14 +31,20 @@ def _load_config(path: Path) -> ConfigDict: config: ConfigDict = {} found = False for dir_path in _walk_to_root(path): - check_path = dir_path / CONFIG_FILE_NAME + check_path_yaml = dir_path / CONFIG_FILE_NAME + check_path_yml = dir_path / CONFIG_FILE_NAME_YML + if check_path_yaml.exists() and check_path_yml.exists(): + click.secho(f'Both {CONFIG_FILE_NAME} and {CONFIG_FILE_NAME_YML} files found in {dir_path}', + fg='red', err=True) + sys.exit(1) + check_path = check_path_yaml or check_path_yml if check_path.is_file(): found = True config.update((k, v) for k, v in yaml.safe_load(check_path.read_text()).items() if k not in config) if config.pop('root', False): break if not found: - click.secho(f'No {CONFIG_FILE_NAME} file found', fg='red', err=True) + click.secho(f'No valid {CONFIG_FILE_NAME} file found in {path}', fg='red', err=True) sys.exit(1) return config diff --git a/tests/test_config.py b/tests/test_config.py index d9b161f..d90977e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -10,6 +10,7 @@ import yaml from unbeheader.config import CONFIG_FILE_NAME +from unbeheader.config import CONFIG_FILE_NAME_YML from unbeheader.config import DEFAULT_SUBSTRING from unbeheader.config import _load_config from unbeheader.config import _validate_config @@ -19,8 +20,8 @@ @pytest.fixture def create_headers_file(): - def create_headers_file(data, dir_path): - file_path = os.path.join(dir_path, CONFIG_FILE_NAME) + def create_headers_file(data, dir_path, config_file_name=CONFIG_FILE_NAME): + file_path = os.path.join(dir_path, config_file_name) with open(file_path, 'w') as yaml_file: yaml.dump(data, yaml_file) @@ -80,6 +81,14 @@ def test_load_config_for_stop_on_root(create_headers_file, tmp_path): assert config == {} +def test_load_config_for_both_yaml_files(create_headers_file, tmp_path): + create_headers_file({}, tmp_path, CONFIG_FILE_NAME) + create_headers_file({}, tmp_path, CONFIG_FILE_NAME_YML) + with pytest.raises(SystemExit) as exc: + _load_config(tmp_path) + assert exc.value.code == 1 + + def test_load_config_for_file_not_found(tmp_path): with pytest.raises(SystemExit) as exc: _load_config(tmp_path)