Skip to content

Commit

Permalink
Expand coverage of path globbing (#1709)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
greg-hellings and ssbarnea committed Oct 1, 2021
1 parent ef02119 commit af402d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .ansible-lint
@@ -1,4 +1,7 @@
# .ansible-lint
# exclude_paths included in this file are parsed relative to this file's location
# and not relative to the CWD of execution. CLI arguments passed to the --exclude
# option will be parsed relative to the CWD of execution.
exclude_paths:
- .cache/ # implicit unless exclude_paths is defined in config
- .github/
Expand Down
5 changes: 5 additions & 0 deletions examples/playbooks/valid_with_alt_extension.yaml
@@ -0,0 +1,5 @@
# Used to validate that we also accept .yaml extension on playbooks
- hosts: localhost
tasks:
- debug: # <-- should notify about missing 'name'
msg: "hello!"
6 changes: 5 additions & 1 deletion src/ansiblelint/runner.py
Expand Up @@ -4,6 +4,7 @@
import multiprocessing.pool
import os
from dataclasses import dataclass
from fnmatch import fnmatch
from pathlib import Path
from typing import TYPE_CHECKING, Any, FrozenSet, Generator, List, Optional, Set, Union

Expand Down Expand Up @@ -91,7 +92,10 @@ def is_excluded(self, file_path: str) -> bool:
_file_path = Path(file_path)

return any(
abs_path.startswith(path) or _file_path.match(path)
abs_path.startswith(path)
or _file_path.match(path)
or fnmatch(str(abs_path), path)
or fnmatch(str(_file_path), path)
for path in self.exclude_paths
)

Expand Down
12 changes: 8 additions & 4 deletions test/TestRunner.py
Expand Up @@ -76,16 +76,20 @@ def test_runner_exclude_paths(default_rules_collection: RulesCollection) -> None
assert len(matches) == 0


def test_runner_exclude_globs(default_rules_collection: RulesCollection) -> None:
@pytest.mark.parametrize(('exclude_path'), ('**/playbooks/*.yml',))
def test_runner_exclude_globs(
default_rules_collection: RulesCollection, exclude_path: str
) -> None:
"""Test that globs work."""
runner = Runner(
'examples/playbooks/example.yml',
'examples/playbooks',
rules=default_rules_collection,
exclude_paths=['**/example.yml'],
exclude_paths=[exclude_path],
)

matches = runner.run()
assert len(matches) == 0
# we expect to find one error from the only .yaml file we have there.
assert len(matches) == 1


@pytest.mark.parametrize(
Expand Down

0 comments on commit af402d0

Please sign in to comment.