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

Handling of exclude paths with trailing slashs #3527

Merged
merged 3 commits into from
Jun 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ black>=22.8.0 # MIT
filelock>=3.3.0 # The Unlicense
jsonschema>=4.10.0 # MIT, version needed for improved errors
packaging>=21.3 # Apache-2.0,BSD-2-Clause
pathspec>=0.9.0 # Mozilla Public License 2.0 (MPL 2.0)
pathspec>=0.10.3 # Mozilla Public License 2.0 (MPL 2.0)
pyyaml>=5.4.1 # MIT (centos 9 has 5.3.1)
rich>=12.0.0 # MIT
ruamel.yaml>=0.17.0,<0.18,!=0.17.29,!=0.17.30 # MIT, next version is planned to have breaking changes
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 803
PYTEST_REQPASS: 804
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
5 changes: 4 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,10 @@ def get_all_files(

def is_excluded(path_to_check: Path) -> bool:
"""Check if a file is exclude by current specs."""
return any(spec.match_file(str(path_to_check)) for spec in pathspecs)
return any(
spec.match_file(pathspec.util.append_dir_sep(path_to_check))
for spec in pathspecs
)

for path in paths:
pathspecs = [
Expand Down
15 changes: 13 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ def test_is_playbook() -> None:
assert utils.is_playbook("examples/playbooks/always-run-success.yml")


def test_auto_detect_exclude(tmp_path: Path) -> None:
@pytest.mark.parametrize(
"exclude",
(pytest.param("foo", id="1"), pytest.param("foo/", id="2")),
)
def test_auto_detect_exclude(tmp_path: Path, exclude: str) -> None:
"""Verify that exclude option can be used to narrow down detection."""
with cwd(tmp_path):
subprocess.check_output(
Expand All @@ -334,9 +338,16 @@ def test_auto_detect_exclude(tmp_path: Path) -> None:
(tmp_path / "bar").mkdir()
(tmp_path / "foo" / "playbook.yml").touch()
(tmp_path / "bar" / "playbook.yml").touch()
options = cli.get_config(["--exclude", "foo"])

options = cli.get_config(["--exclude", exclude])
options.cwd = tmp_path
result = utils.get_lintables(options)
assert result == [Lintable("bar/playbook.yml", kind="playbook")]

# now we also test with .gitignore exclude approach
(tmp_path / ".gitignore").write_text(f".gitignore\n{exclude}\n")
options = cli.get_config([])
options.cwd = tmp_path
result = utils.get_lintables(options)
assert result == [Lintable("bar/playbook.yml", kind="playbook")]

Expand Down