config: validate ignore-from-file in validate_rule_conf

This fixes commit 2f8ad70 "config: Implement for `ignore-from-file`
option" that lacked rule config validation.

Closes https://github.com/adrienverge/yamllint/issues/542.
This commit is contained in:
ndrwnaguib
2023-08-21 12:54:19 -07:00
committed by Adrien Vergé
parent 1f79e62a98
commit ec153761dc
2 changed files with 31 additions and 5 deletions

View File

@@ -212,6 +212,14 @@ class SimpleConfigTestCase(unittest.TestCase):
' colons:\n' ' colons:\n'
' ignore: yes\n') ' ignore: yes\n')
def test_invalid_rule_ignore_from_file(self):
self.assertRaises(
config.YamlLintConfigError,
config.YamlLintConfig,
'rules:\n'
' colons:\n'
' ignore-from-file: 1337\n')
def test_invalid_locale(self): def test_invalid_locale(self):
with self.assertRaisesRegex( with self.assertRaisesRegex(
config.YamlLintConfigError, config.YamlLintConfigError,
@@ -660,12 +668,19 @@ class IgnoreConfigTestCase(unittest.TestCase):
def test_run_with_ignore_from_file(self): def test_run_with_ignore_from_file(self):
with open(os.path.join(self.wd, '.yamllint'), 'w') as f: with open(os.path.join(self.wd, '.yamllint'), 'w') as f:
f.write('extends: default\n' f.write('extends: default\n'
'ignore-from-file: .gitignore\n') 'ignore-from-file: .gitignore\n'
'rules:\n'
' key-duplicates:\n'
' ignore-from-file: .ignore-key-duplicates\n')
with open(os.path.join(self.wd, '.gitignore'), 'w') as f: with open(os.path.join(self.wd, '.gitignore'), 'w') as f:
f.write('*.dont-lint-me.yaml\n' f.write('*.dont-lint-me.yaml\n'
'/bin/\n' '/bin/\n'
'!/bin/*.lint-me-anyway.yaml\n') '!/bin/*.lint-me-anyway.yaml\n')
with open(os.path.join(self.wd, '.ignore-key-duplicates'), 'w') as f:
f.write('/ign-dup\n')
sys.stdout = StringIO() sys.stdout = StringIO()
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
cli.run(('-f', 'parsable', '.')) cli.run(('-f', 'parsable', '.'))
@@ -686,10 +701,8 @@ class IgnoreConfigTestCase(unittest.TestCase):
'./file-at-root.yaml:3:3: ' + keydup, './file-at-root.yaml:3:3: ' + keydup,
'./file-at-root.yaml:4:17: ' + trailing, './file-at-root.yaml:4:17: ' + trailing,
'./file-at-root.yaml:5:5: ' + hyphen, './file-at-root.yaml:5:5: ' + hyphen,
'./ign-dup/file.yaml:3:3: ' + keydup,
'./ign-dup/file.yaml:4:17: ' + trailing, './ign-dup/file.yaml:4:17: ' + trailing,
'./ign-dup/file.yaml:5:5: ' + hyphen, './ign-dup/file.yaml:5:5: ' + hyphen,
'./ign-dup/sub/dir/file.yaml:3:3: ' + keydup,
'./ign-dup/sub/dir/file.yaml:4:17: ' + trailing, './ign-dup/sub/dir/file.yaml:4:17: ' + trailing,
'./ign-dup/sub/dir/file.yaml:5:5: ' + hyphen, './ign-dup/sub/dir/file.yaml:5:5: ' + hyphen,
'./ign-trail/file.yaml:3:3: ' + keydup, './ign-trail/file.yaml:3:3: ' + keydup,

View File

@@ -153,8 +153,21 @@ def validate_rule_conf(rule, conf):
return False return False
if isinstance(conf, dict): if isinstance(conf, dict):
if ('ignore' in conf and if ('ignore-from-file' in conf and not isinstance(
not isinstance(conf['ignore'], pathspec.pathspec.PathSpec)): conf['ignore-from-file'], pathspec.pathspec.PathSpec)):
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)
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 f:
conf['ignore'] = pathspec.PathSpec.from_lines(
'gitwildmatch', f)
elif ('ignore' in conf and not isinstance(
conf['ignore'], pathspec.pathspec.PathSpec)):
if isinstance(conf['ignore'], str): if isinstance(conf['ignore'], str):
conf['ignore'] = pathspec.PathSpec.from_lines( conf['ignore'] = pathspec.PathSpec.from_lines(
'gitwildmatch', conf['ignore'].splitlines()) 'gitwildmatch', conf['ignore'].splitlines())