Skip to content

Commit

Permalink
Fix exclude_paths regression
Browse files Browse the repository at this point in the history
Fixes: #1311
  • Loading branch information
ssbarnea committed Feb 7, 2021
1 parent bc8988e commit 8d53f16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ansiblelint/runner.py
Expand Up @@ -80,7 +80,12 @@ def is_excluded(self, file_path: str) -> bool:
# Any will short-circuit as soon as something returns True, but will
# be poor performance for the case where the path under question is
# not excluded.
return any(file_path.startswith(path) for path in self.exclude_paths)

# Exclusions should be evaluated only using absolute paths in order
# to work correctly.
abs_path = os.path.abspath(file_path)

return any(abs_path.startswith(path) for path in self.exclude_paths)

def run(self) -> List[MatchError]:
"""Execute the linting process."""
Expand Down Expand Up @@ -137,6 +142,11 @@ def worker(lintable: Lintable) -> List[MatchError]:
# update list of checked files
self.checked_files.update([str(x.path) for x in self.lintables])

# remove any matches made inside excluded files
matches = list(
filter(lambda match: not self.is_excluded(match.filename), matches)
)

return sorted(set(matches))

def _emit_matches(self, files: List[Lintable]) -> Generator[MatchError, None, None]:
Expand Down
12 changes: 12 additions & 0 deletions test/TestRunner.py
Expand Up @@ -57,6 +57,18 @@ def test_runner(default_rules_collection, playbook, exclude, length) -> None:
assert len(matches) == length


def test_runner_exclude_paths(default_rules_collection) -> None:
"""Test that exclude paths do work."""
runner = Runner(
'examples/playbooks/example.yml',
rules=default_rules_collection,
exclude_paths=['examples/'],
)

matches = runner.run()
assert len(matches) == 0


@pytest.mark.parametrize(
('formatter_cls'),
(
Expand Down

0 comments on commit 8d53f16

Please sign in to comment.