Skip to content

Commit 17f285e

Browse files
committed
Fix #24 -- Add human-readable output for config file errors
1 parent 468ebc5 commit 17f285e

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

relint.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
r"(?:\n|^)diff --git a\/.* b\/.*(?:\n|$)")
1818

1919

20+
class RelintError(Exception):
21+
pass
22+
23+
24+
class ConfigError(ValueError, RelintError):
25+
pass
26+
27+
2028
Test = namedtuple(
2129
'Test', (
2230
'name',
@@ -63,27 +71,36 @@ def parse_args(args):
6371

6472
def load_config(path, fail_warnings):
6573
with open(path) as fs:
66-
for test in yaml.safe_load(fs):
67-
filename = test.get('filename')
68-
if filename:
69-
warnings.warn(
70-
"The glob style 'filename' configuration attribute has been"
71-
" deprecated in favor of a new RegEx based 'filePattern' attribute."
72-
" 'filename' support will be removed in relint version 2.0.",
73-
DeprecationWarning
74+
try:
75+
for test in yaml.safe_load(fs):
76+
filename = test.get('filename')
77+
if filename:
78+
warnings.warn(
79+
"The glob style 'filename' configuration attribute has been"
80+
" deprecated in favor of a new RegEx based 'filePattern' attribute."
81+
" 'filename' support will be removed in relint version 2.0.",
82+
DeprecationWarning
83+
)
84+
if not isinstance(filename, list):
85+
filename = list(filename)
86+
file_pattern = test.get('filePattern', '.*')
87+
file_pattern = re.compile(file_pattern)
88+
yield Test(
89+
name=test['name'],
90+
pattern=re.compile(test['pattern'], re.MULTILINE),
91+
hint=test.get('hint'),
92+
file_pattern=file_pattern,
93+
filename=filename,
94+
error=test.get('error', True) or fail_warnings
7495
)
75-
if not isinstance(filename, list):
76-
filename = list(filename)
77-
file_pattern = test.get('filePattern', '.*')
78-
file_pattern = re.compile(file_pattern)
79-
yield Test(
80-
name=test['name'],
81-
pattern=re.compile(test['pattern'], re.MULTILINE),
82-
hint=test.get('hint'),
83-
file_pattern=file_pattern,
84-
filename=filename,
85-
error=test.get('error', True) or fail_warnings
86-
)
96+
except yaml.YAMLError as e:
97+
raise ConfigError("Error parsing your relint config file.") from e
98+
except TypeError:
99+
warnings.warn("Your relint config is empty, no tests were executed.", UserWarning)
100+
except (AttributeError, ValueError) as e:
101+
raise ConfigError(
102+
"Your relint config is not a valid YAML list of relint tests."
103+
) from e
87104

88105

89106
def lint_file(filename, tests):

test_relint.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import warnings
44

55
import pytest
6+
import yaml
67

78
from relint import (main, match_with_diff_changes, parse_diff, parse_filenames,
8-
parse_line_numbers, split_diff_content_by_filename,)
9+
parse_line_numbers, split_diff_content_by_filename, ConfigError, )
910

1011

1112
class TestMain:
@@ -165,3 +166,33 @@ def test_filename_warning(self, tmpdir):
165166
assert exc_info.value.code == 0
166167
assert issubclass(w[-1].category, DeprecationWarning)
167168
assert "'filename'" in str(w[-1].message)
169+
170+
def test_empty_config_file(self, tmpdir):
171+
tmpdir.join('.relint.yml').write('')
172+
173+
with tmpdir.as_cwd():
174+
with warnings.catch_warnings(record=True) as w:
175+
with pytest.raises(SystemExit) as exc_info:
176+
main(['**'])
177+
178+
assert exc_info.value.code == 0
179+
assert issubclass(w[-1].category, UserWarning)
180+
assert "Your relint config is empty, no tests were executed." in str(w[-1].message)
181+
182+
def test_malformed_config_file(self, tmpdir):
183+
tmpdir.join('.relint.yml').write('test:')
184+
185+
with tmpdir.as_cwd():
186+
with pytest.raises(ConfigError) as exc_info:
187+
main(['**'])
188+
189+
assert "Your relint config is not a valid YAML list of relint tests." in str(exc_info.value)
190+
191+
def test_corrupt_config_file(self, tmpdir):
192+
tmpdir.join('.relint.yml').write(b'\x00')
193+
194+
with tmpdir.as_cwd():
195+
with pytest.raises(ConfigError) as exc_info:
196+
main(['**'])
197+
198+
assert 'Error parsing your relint config file.' in str(exc_info.value)

0 commit comments

Comments
 (0)