Skip to content

Commit

Permalink
Add support for .header.yml config files
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeGak committed Sep 27, 2023
1 parent 2857241 commit ddbd7d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
11 changes: 9 additions & 2 deletions src/unbeheader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
13 changes: 11 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ddbd7d0

Please sign in to comment.