Skip to content

Commit 9235c68

Browse files
committed
cli: Restore ignoration of files passed as command-line arguments
Commit 2344380 "Cleanly skip broken symlinks that are ignored" fixed a problem on symbolic links but also introduced a change on how ignored files should be treated, depending on whether they're explicitely passed as command-line arguments or not [^1]. This change is annoying for users that dynamically build the list of files to pass as arguments, e.g. [^2]: find -name '*\.yaml' | xargs yamllint The present commit adds unit tests for `yamllint [FILES]...` and `yamllint --list-files [FILES]...`, that passed with previous version 1.34.0, and restore the behavior of this version. As a result it also reverts the API change of commit 2344380 on `yamllint.linter.run(stream, config)`. [^1]: #657 (comment) [^2]: #657 (comment)
1 parent 3a13803 commit 9235c68

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

tests/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,25 @@ def test_run_list_files(self):
714714
os.path.join(self.wd, 'warn.yaml')]
715715
)
716716

717+
config = 'ignore: ["*.yaml", "*.yml", "!a.yaml"]'
718+
with RunContext(self) as ctx:
719+
cli.run(('--list-files', '-d', config, self.wd))
720+
self.assertEqual(ctx.returncode, 0)
721+
self.assertEqual(
722+
sorted(ctx.stdout.splitlines()),
723+
[os.path.join(self.wd, 'a.yaml')]
724+
)
725+
with RunContext(self) as ctx:
726+
cli.run(('--list-files', '-d', config,
727+
os.path.join(self.wd, 'a.yaml'),
728+
os.path.join(self.wd, 'en.yaml'),
729+
os.path.join(self.wd, 'c.yaml')))
730+
self.assertEqual(ctx.returncode, 0)
731+
self.assertEqual(
732+
sorted(ctx.stdout.splitlines()),
733+
[os.path.join(self.wd, 'a.yaml')]
734+
)
735+
717736

718737
class CommandLineConfigTestCase(unittest.TestCase):
719738
def test_config_file(self):

tests/test_config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,20 @@ def test_run_with_ignore_with_broken_symlink(self):
803803

804804
os.chdir(backup_wd)
805805
shutil.rmtree(wd)
806+
807+
def test_run_with_ignore_on_ignored_file(self):
808+
with open(os.path.join(self.wd, '.yamllint'), 'w') as f:
809+
f.write('ignore: file.dont-lint-me.yaml\n'
810+
'rules:\n'
811+
' trailing-spaces: enable\n'
812+
' key-duplicates:\n'
813+
' ignore: file-at-root.yaml\n')
814+
815+
sys.stdout = StringIO()
816+
with self.assertRaises(SystemExit):
817+
cli.run(('-f', 'parsable', 'file.dont-lint-me.yaml',
818+
'file-at-root.yaml'))
819+
self.assertEqual(
820+
sys.stdout.getvalue().strip(),
821+
'file-at-root.yaml:4:17: [error] trailing spaces (trailing-spaces)'
822+
)

yamllint/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ def run(argv=None):
210210

211211
if args.list_files:
212212
for file in find_files_recursively(args.files, conf):
213-
print(file)
213+
if not conf.is_file_ignored(file):
214+
print(file)
214215
sys.exit(0)
215216

216217
max_level = 0

yamllint/linter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ def run(input, conf, filepath=None):
222222
:param input: buffer, string or stream to read from
223223
:param conf: yamllint configuration object
224224
"""
225+
if filepath is not None and conf.is_file_ignored(filepath):
226+
return ()
227+
225228
if isinstance(input, (bytes, str)):
226229
return _run(input, conf, filepath)
227230
elif isinstance(input, io.IOBase):

0 commit comments

Comments
 (0)