From 98ef1ca2f2589c01cb411469b8d3887a3c1b3fe6 Mon Sep 17 00:00:00 2001 From: Gregory Hellings Date: Thu, 4 Mar 2021 00:12:44 -0600 Subject: [PATCH] Add support for excluding file globs Addresses #1424 --- src/ansiblelint/runner.py | 4 +++- test/TestRunner.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ansiblelint/runner.py b/src/ansiblelint/runner.py index 4fbcf7d2014..506d65d2215 100644 --- a/src/ansiblelint/runner.py +++ b/src/ansiblelint/runner.py @@ -3,6 +3,7 @@ import multiprocessing import multiprocessing.pool import os +from pathlib import Path from dataclasses import dataclass from typing import TYPE_CHECKING, Any, FrozenSet, Generator, List, Optional, Set, Union @@ -84,8 +85,9 @@ def is_excluded(self, file_path: str) -> bool: # Exclusions should be evaluated only using absolute paths in order # to work correctly. abs_path = os.path.abspath(file_path) + _file_path = Path(file_path) - return any(abs_path.startswith(path) for path in self.exclude_paths) + return any(abs_path.startswith(path) or _file_path.match(path) for path in self.exclude_paths) def run(self) -> List[MatchError]: """Execute the linting process.""" diff --git a/test/TestRunner.py b/test/TestRunner.py index e4e028034b1..ce69ae51bc3 100644 --- a/test/TestRunner.py +++ b/test/TestRunner.py @@ -69,6 +69,18 @@ def test_runner_exclude_paths(default_rules_collection) -> None: assert len(matches) == 0 +def test_runner_exclude_globs(default_rules_collection) -> None: + """Test that globs work.""" + runner = Runner( + 'examples/playbooks/example.yml', + rules=default_rules_collection, + exclude_paths=['**/example.yml'], + ) + + matches = runner.run() + assert len(matches) == 0 + + @pytest.mark.parametrize( ('formatter_cls'), (