From 654501746e0a4e46451109e434671b1ce074514e Mon Sep 17 00:00:00 2001 From: ndrwnaguib <24280372+ndrwnaguib@users.noreply.github.com> Date: Sun, 15 Oct 2023 19:43:02 -0700 Subject: [PATCH] fix (#524): refactor code to the same style; add rule-scoped test --- tests/test_config.py | 25 +++++++++++++------------ yamllint/config.py | 11 +++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 1fb83c9b..13d7ab3b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -213,16 +213,12 @@ def test_invalid_rule_ignore(self): ' ignore: yes\n') def test_invalid_rule_ignore_from_file(self): - with self.assertRaisesRegex( + self.assertRaises( 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' - ) + config.YamlLintConfig, + 'rules:\n' + ' colons:\n' + ' ignore-from-file: 1337\n') def test_invalid_locale(self): with self.assertRaisesRegex( @@ -672,12 +668,19 @@ def test_run_with_ignore_list(self): def test_run_with_ignore_from_file(self): with open(os.path.join(self.wd, '.yamllint'), 'w') as f: 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: f.write('*.dont-lint-me.yaml\n' '/bin/\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() with self.assertRaises(SystemExit): cli.run(('-f', 'parsable', '.')) @@ -698,10 +701,8 @@ def test_run_with_ignore_from_file(self): './file-at-root.yaml:3:3: ' + keydup, './file-at-root.yaml:4:17: ' + trailing, './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: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:5:5: ' + hyphen, './ign-trail/file.yaml:3:3: ' + keydup, diff --git a/yamllint/config.py b/yamllint/config.py index a2a1f93f..05b43b8a 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -153,25 +153,24 @@ def validate_rule_conf(rule, conf): return False if isinstance(conf, dict): - if 'ignore-from-file' in conf and isinstance( - conf['ignore-from-file'], (str, list) + if 'ignore-from-file' in conf and not isinstance( + 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) and os.path.isfile(line) - for line in conf['ignore-from-file'] + 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 ignore_file: + with fileinput.input(conf['ignore-from-file']) as f: conf['ignore'] = pathspec.PathSpec.from_lines( - 'gitwildmatch', ignore_file + 'gitwildmatch', f ) elif 'ignore' in conf and not isinstance( conf['ignore'], pathspec.pathspec.PathSpec