Skip to content

Commit

Permalink
Fix #24 -- Add human-readable output for config file errors
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Aug 5, 2022
1 parent 468ebc5 commit 17f285e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
57 changes: 37 additions & 20 deletions relint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
r"(?:\n|^)diff --git a\/.* b\/.*(?:\n|$)")


class RelintError(Exception):
pass


class ConfigError(ValueError, RelintError):
pass


Test = namedtuple(
'Test', (
'name',
Expand Down Expand Up @@ -63,27 +71,36 @@ def parse_args(args):

def load_config(path, fail_warnings):
with open(path) as fs:
for test in yaml.safe_load(fs):
filename = test.get('filename')
if filename:
warnings.warn(
"The glob style 'filename' configuration attribute has been"
" deprecated in favor of a new RegEx based 'filePattern' attribute."
" 'filename' support will be removed in relint version 2.0.",
DeprecationWarning
try:
for test in yaml.safe_load(fs):
filename = test.get('filename')
if filename:
warnings.warn(
"The glob style 'filename' configuration attribute has been"
" deprecated in favor of a new RegEx based 'filePattern' attribute."
" 'filename' support will be removed in relint version 2.0.",
DeprecationWarning
)
if not isinstance(filename, list):
filename = list(filename)
file_pattern = test.get('filePattern', '.*')
file_pattern = re.compile(file_pattern)
yield Test(
name=test['name'],
pattern=re.compile(test['pattern'], re.MULTILINE),
hint=test.get('hint'),
file_pattern=file_pattern,
filename=filename,
error=test.get('error', True) or fail_warnings
)
if not isinstance(filename, list):
filename = list(filename)
file_pattern = test.get('filePattern', '.*')
file_pattern = re.compile(file_pattern)
yield Test(
name=test['name'],
pattern=re.compile(test['pattern'], re.MULTILINE),
hint=test.get('hint'),
file_pattern=file_pattern,
filename=filename,
error=test.get('error', True) or fail_warnings
)
except yaml.YAMLError as e:
raise ConfigError("Error parsing your relint config file.") from e
except TypeError:
warnings.warn("Your relint config is empty, no tests were executed.", UserWarning)
except (AttributeError, ValueError) as e:
raise ConfigError(
"Your relint config is not a valid YAML list of relint tests."
) from e


def lint_file(filename, tests):
Expand Down
33 changes: 32 additions & 1 deletion test_relint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import warnings

import pytest
import yaml

from relint import (main, match_with_diff_changes, parse_diff, parse_filenames,
parse_line_numbers, split_diff_content_by_filename,)
parse_line_numbers, split_diff_content_by_filename, ConfigError, )


class TestMain:
Expand Down Expand Up @@ -165,3 +166,33 @@ def test_filename_warning(self, tmpdir):
assert exc_info.value.code == 0
assert issubclass(w[-1].category, DeprecationWarning)
assert "'filename'" in str(w[-1].message)

def test_empty_config_file(self, tmpdir):
tmpdir.join('.relint.yml').write('')

with tmpdir.as_cwd():
with warnings.catch_warnings(record=True) as w:
with pytest.raises(SystemExit) as exc_info:
main(['**'])

assert exc_info.value.code == 0
assert issubclass(w[-1].category, UserWarning)
assert "Your relint config is empty, no tests were executed." in str(w[-1].message)

def test_malformed_config_file(self, tmpdir):
tmpdir.join('.relint.yml').write('test:')

with tmpdir.as_cwd():
with pytest.raises(ConfigError) as exc_info:
main(['**'])

assert "Your relint config is not a valid YAML list of relint tests." in str(exc_info.value)

def test_corrupt_config_file(self, tmpdir):
tmpdir.join('.relint.yml').write(b'\x00')

with tmpdir.as_cwd():
with pytest.raises(ConfigError) as exc_info:
main(['**'])

assert 'Error parsing your relint config file.' in str(exc_info.value)

0 comments on commit 17f285e

Please sign in to comment.