Skip to content

Commit

Permalink
cli: Add --list-files command line option
Browse files Browse the repository at this point in the history
This option lists the files to lint by yamllint, taking into account `ignore`
and `yaml-files` configuration options.
  • Loading branch information
splattael committed Jan 10, 2023
1 parent 2a904f8 commit fa0bb03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ directories, set ``yaml-files`` configuration option. The default is:
The same rules as for ignoring paths apply (``.gitignore``-style path pattern,
see below).

If you need to know the exact list of files that yamllint would process,
without really linting them, you can use ``--list-files``:

.. code:: bash
yamllint --list-files .
Ignoring paths
--------------

Expand Down Expand Up @@ -220,6 +227,13 @@ or:
.. note:: However, this is mutually exclusive with the ``ignore`` key.

If you need to know the exact list of files that yamllint would process,
without really linting them, you can use ``--list-files``:

.. code:: bash
yamllint --list-files .
Setting the locale
------------------

Expand Down
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,39 @@ def test_run_non_universal_newline(self):
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

def test_run_list_files(self):
with RunContext(self) as ctx:
cli.run(('--list-files', self.wd))
self.assertEqual(ctx.returncode, 0)
self.assertEqual(
sorted(ctx.stdout.splitlines()),
[os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'dos.yml'),
os.path.join(self.wd, 'empty.yml'),
os.path.join(self.wd, 'en.yaml'),
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
os.path.join(self.wd, 'sub/directory.yaml/empty.yml'),
os.path.join(self.wd, 'sub/ok.yaml'),
os.path.join(self.wd, 'warn.yaml')]
)

config = '{ignore: "*.yml", yaml-files: ["*.*"]}'
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'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'en.yaml'),
os.path.join(self.wd, 'no-yaml.json'),
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
os.path.join(self.wd, 'sub/directory.yaml/not-yaml.txt'),
os.path.join(self.wd, 'sub/ok.yaml'),
os.path.join(self.wd, 'warn.yaml')]
)


class CommandLineConfigTestCase(unittest.TestCase):
def test_config_file(self):
Expand Down
8 changes: 8 additions & 0 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def run(argv=None):
config_group.add_argument('-d', '--config-data', dest='config_data',
action='store',
help='custom configuration (as YAML source)')
parser.add_argument('--list-files', action='store_true', dest='list_files',
help='list files to lint and exit')
parser.add_argument('-f', '--format',
choices=('parsable', 'standard', 'colored', 'github',
'auto'),
Expand Down Expand Up @@ -207,6 +209,12 @@ def run(argv=None):
if conf.locale is not None:
locale.setlocale(locale.LC_ALL, conf.locale)

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

max_level = 0

for file in find_files_recursively(args.files, conf):
Expand Down

0 comments on commit fa0bb03

Please sign in to comment.