Skip to content

Commit fa0bb03

Browse files
authored
cli: Add --list-files command line option
This option lists the files to lint by yamllint, taking into account `ignore` and `yaml-files` configuration options.
1 parent 2a904f8 commit fa0bb03

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

docs/configuration.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ directories, set ``yaml-files`` configuration option. The default is:
136136
The same rules as for ignoring paths apply (``.gitignore``-style path pattern,
137137
see below).
138138

139+
If you need to know the exact list of files that yamllint would process,
140+
without really linting them, you can use ``--list-files``:
141+
142+
.. code:: bash
143+
144+
yamllint --list-files .
145+
139146
Ignoring paths
140147
--------------
141148

@@ -220,6 +227,13 @@ or:
220227
221228
.. note:: However, this is mutually exclusive with the ``ignore`` key.
222229

230+
If you need to know the exact list of files that yamllint would process,
231+
without really linting them, you can use ``--list-files``:
232+
233+
.. code:: bash
234+
235+
yamllint --list-files .
236+
223237
Setting the locale
224238
------------------
225239

tests/test_cli.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,39 @@ def test_run_non_universal_newline(self):
678678
self.assertEqual(
679679
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
680680

681+
def test_run_list_files(self):
682+
with RunContext(self) as ctx:
683+
cli.run(('--list-files', self.wd))
684+
self.assertEqual(ctx.returncode, 0)
685+
self.assertEqual(
686+
sorted(ctx.stdout.splitlines()),
687+
[os.path.join(self.wd, 'a.yaml'),
688+
os.path.join(self.wd, 'c.yaml'),
689+
os.path.join(self.wd, 'dos.yml'),
690+
os.path.join(self.wd, 'empty.yml'),
691+
os.path.join(self.wd, 'en.yaml'),
692+
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
693+
os.path.join(self.wd, 'sub/directory.yaml/empty.yml'),
694+
os.path.join(self.wd, 'sub/ok.yaml'),
695+
os.path.join(self.wd, 'warn.yaml')]
696+
)
697+
698+
config = '{ignore: "*.yml", yaml-files: ["*.*"]}'
699+
with RunContext(self) as ctx:
700+
cli.run(('--list-files', '-d', config, self.wd))
701+
self.assertEqual(ctx.returncode, 0)
702+
self.assertEqual(
703+
sorted(ctx.stdout.splitlines()),
704+
[os.path.join(self.wd, 'a.yaml'),
705+
os.path.join(self.wd, 'c.yaml'),
706+
os.path.join(self.wd, 'en.yaml'),
707+
os.path.join(self.wd, 'no-yaml.json'),
708+
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
709+
os.path.join(self.wd, 'sub/directory.yaml/not-yaml.txt'),
710+
os.path.join(self.wd, 'sub/ok.yaml'),
711+
os.path.join(self.wd, 'warn.yaml')]
712+
)
713+
681714

682715
class CommandLineConfigTestCase(unittest.TestCase):
683716
def test_config_file(self):

yamllint/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def run(argv=None):
157157
config_group.add_argument('-d', '--config-data', dest='config_data',
158158
action='store',
159159
help='custom configuration (as YAML source)')
160+
parser.add_argument('--list-files', action='store_true', dest='list_files',
161+
help='list files to lint and exit')
160162
parser.add_argument('-f', '--format',
161163
choices=('parsable', 'standard', 'colored', 'github',
162164
'auto'),
@@ -207,6 +209,12 @@ def run(argv=None):
207209
if conf.locale is not None:
208210
locale.setlocale(locale.LC_ALL, conf.locale)
209211

212+
if args.list_files:
213+
for file in find_files_recursively(args.files, conf):
214+
if not conf.is_file_ignored(file):
215+
print(file)
216+
sys.exit(0)
217+
210218
max_level = 0
211219

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

0 commit comments

Comments
 (0)