Skip to content

Commit

Permalink
Avoid stacktrace with broken ansible.cfg files (#3916)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 29, 2023
1 parent 58e4ba5 commit b0b1e69
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 847
PYTEST_REQPASS: 848
steps:
- uses: actions/checkout@v4
with:
Expand Down
12 changes: 11 additions & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
from filelock import FileLock, Timeout
from rich.markup import escape

from ansiblelint.constants import RC, SKIP_SCHEMA_UPDATE

# safety check for broken ansible core, needs to happen first
try:
# pylint: disable=unused-import
from ansible.parsing.dataloader import DataLoader # noqa: F401

except Exception as _exc: # pylint: disable=broad-exception-caught # noqa: BLE001
logging.fatal(_exc)
sys.exit(RC.INVALID_CONFIG)
# pylint: disable=ungrouped-imports
from ansiblelint import cli
from ansiblelint._mockings import _perform_mockings_cleanup
from ansiblelint.app import get_app
Expand All @@ -53,7 +64,6 @@
log_entries,
options,
)
from ansiblelint.constants import RC, SKIP_SCHEMA_UPDATE
from ansiblelint.loaders import load_ignore_txt
from ansiblelint.runner import get_matches
from ansiblelint.skip_utils import normalize_tag
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/molecule.json"
},
"playbook": {
"etag": "8ae42e48318ff6da41f6d383dc19b643221258ea6521886f834e31cb8d3a7322",
"etag": "b86e7f78281e33eb16de9c5c066da0f88798243b647cc195573441d92e1e78a5",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/playbook.json"
},
"requirements": {
Expand All @@ -52,7 +52,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-rulebook/main/ansible_rulebook/schema/ruleset_schema.json"
},
"tasks": {
"etag": "f9fbc0855680d1321fa3902181131d73838d922362d8dfb85a4f59402240cc07",
"etag": "495ec0c6cb49d60feed7e2bc7c0ef0135bab473a28c3c8d4a7be4f210e8c53fc",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/tasks.json"
},
"vars": {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/broken-ansible.cfg/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[defaults]

# This breaks ansible-core, because it loads the value as null and only
# integers are accepted.
gather_timeout=
18 changes: 18 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pytest_mock import MockerFixture

from ansiblelint.config import get_version_warning
from ansiblelint.constants import RC


@pytest.mark.parametrize(
Expand Down Expand Up @@ -104,3 +105,20 @@ def test_nodeps(lintable: str) -> None:
env=env,
)
assert proc.returncode == 0, proc


def test_broken_ansible_cfg() -> None:
"""Asserts behavior when encountering broken ansible.cfg files."""
py_path = Path(sys.executable).parent
proc = subprocess.run(
[str(py_path / "ansible-lint"), "--version"],
check=False,
capture_output=True,
text=True,
cwd="test/fixtures/broken-ansible.cfg",
)
assert proc.returncode == RC.INVALID_CONFIG, proc
assert (
"Invalid type for configuration option setting: DEFAULT_GATHER_TIMEOUT"
in proc.stderr
)

0 comments on commit b0b1e69

Please sign in to comment.