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

Fix exclude_paths regression #1313

Merged
merged 1 commit into from Feb 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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