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

Ensure that we also lint files from git submodules #3431

Merged
merged 1 commit into from
May 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ repos:
- cryptography>=39.0.1
- filelock
- jinja2
- pytest-mock
- pytest>=7.2.2
- rich>=13.2.0
- ruamel-yaml>=0.17.26
Expand Down Expand Up @@ -180,6 +181,7 @@ repos:
- docutils
- filelock
- jsonschema>=4.9.0
- pytest-mock
- pytest>=7.2.2
- pyyaml
- rich>=13.2.0
Expand Down
78 changes: 51 additions & 27 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,36 +422,60 @@ def discover_lintables(options: Options) -> dict[str, Any]:
under current user and absolute for everything else.
"""
# git is preferred as it also considers .gitignore
git_command_present = [
*GIT_CMD,
"ls-files",
"--cached",
"--others",
"--exclude-standard",
"-z",
]
git_command_absent = [*GIT_CMD, "ls-files", "--deleted", "-z"]
out = None
# As --recurse-submodules is incompatible with --others we need to run
# twice to get combined results.
commands = {
"tracked": {
"cmd": [
*GIT_CMD,
"ls-files",
"--cached",
"--exclude-standard",
"--recurse-submodules",
"-z",
],
"remove": False,
},
"others": {
"cmd": [
*GIT_CMD,
"ls-files",
"--cached",
"--others",
"--exclude-standard",
"-z",
],
"remove": False,
},
"absent": {
"cmd": [
*GIT_CMD,
"ls-files",
"--deleted",
"-z",
],
"remove": True,
},
}

out: set[str] = set()
try:
out_present = subprocess.check_output(
git_command_present, # noqa: S603
stderr=subprocess.STDOUT,
text=True,
).split("\x00")[:-1]
_logger.info(
"Discovered files to lint using: %s",
" ".join(git_command_present),
)

out_absent = subprocess.check_output(
git_command_absent, # noqa: S603
stderr=subprocess.STDOUT,
text=True,
).split("\x00")[:-1]
_logger.info("Excluded removed files using: %s", " ".join(git_command_absent))
for k, value in commands.items():
if not isinstance(value["cmd"], list):
msg = f"Expected list but got {type(value['cmd'])}"
raise TypeError(msg)
result = subprocess.check_output(
value["cmd"], # noqa: S603
stderr=subprocess.STDOUT,
text=True,
).split("\x00")[:-1]
_logger.info(
"Discovered files to lint using git (%s): %s",
k,
" ".join(value["cmd"]),
)
out = out.union(result) if not value["remove"] else out - set(result)

out = set(out_present) - set(out_absent)
except subprocess.CalledProcessError as exc:
if not (exc.returncode == 128 and "fatal: not a git repository" in exc.output):
err = exc.output.rstrip("\n")
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
},
"meta": {
"etag": "7cbcda4e9454961d843f5b2b37349bafdf387d01e8ad6772e0a4c89868aaa55c",
"etag": "24aa044eddbf2fc92e31775bcc625fd8e7689cb14542ac59c0e3b94d9a9b163a",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
},
"meta-runtime": {
Expand Down
4 changes: 2 additions & 2 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import sys
import time
from pathlib import Path
from typing import Any

import pytest
from pytest_mock import MockerFixture

from ansiblelint.config import get_version_warning

Expand Down Expand Up @@ -58,7 +58,7 @@ def test_call_from_outside_venv(expected_warning: bool) -> None:
),
)
def test_get_version_warning(
mocker: Any,
mocker: MockerFixture,
ver_diff: str,
found: bool,
check: str,
Expand Down
7 changes: 6 additions & 1 deletion test/test_mockings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"""Test mockings module."""
from typing import Any

import pytest

from ansiblelint._mockings import _make_module_stub
from ansiblelint.config import options
from ansiblelint.constants import RC


def test_make_module_stub() -> None:
def test_make_module_stub(mocker: Any) -> None:
"""Test make module stub."""
mocker.patch("ansiblelint.config.options.cache_dir", return_value=".")
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
assert options.cache_dir is not None
with pytest.raises(SystemExit) as exc:
_make_module_stub("")
assert exc.type == SystemExit
Expand Down
3 changes: 1 addition & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ def test_cli_auto_detect(capfd: CaptureFixture[str]) -> None:
out, err = capfd.readouterr()

# Confirmation that it runs in auto-detect mode
assert "Discovered files to lint using: git" in err
assert "Excluded removed files using: git" in err
assert "Discovered files to lint using git" in err
# An expected rule match from our examples
assert (
"examples/playbooks/empty_playbook.yml:1:1: "
Expand Down