Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Restore ignoration of files passed as command-line arguments #658

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,25 @@ def test_run_list_files(self):
os.path.join(self.wd, 'warn.yaml')]
)

config = 'ignore: ["*.yaml", "*.yml", "!a.yaml"]'
with RunContext(self) as ctx:
cli.run(('--list-files', '-d', config, self.wd))
self.assertEqual(ctx.returncode, 0)
self.assertEqual(
sorted(ctx.stdout.splitlines()),
[os.path.join(self.wd, 'a.yaml')]
)
with RunContext(self) as ctx:
cli.run(('--list-files', '-d', config,
os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'en.yaml'),
os.path.join(self.wd, 'c.yaml')))
self.assertEqual(ctx.returncode, 0)
self.assertEqual(
sorted(ctx.stdout.splitlines()),
[os.path.join(self.wd, 'a.yaml')]
)


class CommandLineConfigTestCase(unittest.TestCase):
def test_config_file(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,20 @@ def test_run_with_ignore_with_broken_symlink(self):

os.chdir(backup_wd)
shutil.rmtree(wd)

def test_run_with_ignore_on_ignored_file(self):
with open(os.path.join(self.wd, '.yamllint'), 'w') as f:
f.write('ignore: file.dont-lint-me.yaml\n'
'rules:\n'
' trailing-spaces: enable\n'
' key-duplicates:\n'
' ignore: file-at-root.yaml\n')

sys.stdout = StringIO()
with self.assertRaises(SystemExit):
cli.run(('-f', 'parsable', 'file.dont-lint-me.yaml',
'file-at-root.yaml'))
self.assertEqual(
sys.stdout.getvalue().strip(),
'file-at-root.yaml:4:17: [error] trailing spaces (trailing-spaces)'
)
3 changes: 2 additions & 1 deletion yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def run(argv=None):

if args.list_files:
for file in find_files_recursively(args.files, conf):
print(file)
if not conf.is_file_ignored(file):
print(file)
sys.exit(0)

max_level = 0
Expand Down
3 changes: 3 additions & 0 deletions yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ def run(input, conf, filepath=None):
:param input: buffer, string or stream to read from
:param conf: yamllint configuration object
"""
if filepath is not None and conf.is_file_ignored(filepath):
return ()

if isinstance(input, (bytes, str)):
return _run(input, conf, filepath)
elif isinstance(input, io.IOBase):
Expand Down
Loading