Skip to content

Commit

Permalink
fix (#542): validate ignore-from-file in validate_rule_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrwnaguib committed Aug 22, 2023
1 parent a68c3aa commit f529d95
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ def test_invalid_rule_ignore(self):
' colons:\n'
' ignore: yes\n')

def test_invalid_rule_ignore_from_file(self):
with self.assertRaisesRegex(
config.YamlLintConfigError,
'invalid config: ignore-from-file should contain '
r'valid filename\(s\), either as a list or string.',
):
config.YamlLintConfig(
'rules:\n'
' colons:\n'
' ignore-from-file: /invalid_filename\n'
)

def test_invalid_locale(self):
with self.assertRaisesRegex(
config.YamlLintConfigError,
Expand Down
25 changes: 23 additions & 2 deletions yamllint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,29 @@ def validate_rule_conf(rule, conf):
return False

if isinstance(conf, dict):
if ('ignore' in conf and
not isinstance(conf['ignore'], pathspec.pathspec.PathSpec)):
if 'ignore-from-file' in conf and isinstance(
conf['ignore-from-file'], (str, list)
):
if isinstance(conf['ignore-from-file'], str):
conf['ignore-from-file'] = [conf['ignore-from-file']]
if not (
isinstance(conf['ignore-from-file'], list)
and all(
isinstance(line, str) and os.path.isfile(line)
for line in conf['ignore-from-file']
)
):
raise YamlLintConfigError(
'invalid config: ignore-from-file should contain '
'valid filename(s), either as a list or string.'
)
with fileinput.input(conf['ignore-from-file']) as ignore_file:
conf['ignore'] = pathspec.PathSpec.from_lines(
'gitwildmatch', ignore_file
)
elif 'ignore' in conf and not isinstance(
conf['ignore'], pathspec.pathspec.PathSpec
):
if isinstance(conf['ignore'], str):
conf['ignore'] = pathspec.PathSpec.from_lines(
'gitwildmatch', conf['ignore'].splitlines())
Expand Down

0 comments on commit f529d95

Please sign in to comment.