Skip to content

Commit

Permalink
Catch ansible-compat initialization warnings
Browse files Browse the repository at this point in the history
Improves output by catching and displaying warnings from ansible-compat
after the logger is initialized.

Related: ansible/ansible-compat#436
  • Loading branch information
ssbarnea committed Jan 3, 2025
1 parent 37dd23d commit ab77812
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import shutil
import site
import sys
import warnings
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -269,39 +270,46 @@ def fix(runtime_options: Options, result: LintResult, rules: RulesCollection) ->
result.matches.pop(idx)


# pylint: disable=too-many-locals
# pylint: disable=too-many-locals,too-many-statements
def main(argv: list[str] | None = None) -> int:
"""Linter CLI entry point."""
# alter PATH if needed (venv support)
path_inject(argv[0] if argv and argv[0] else "")

if argv is None: # pragma: no cover
argv = sys.argv
cache_dir_lock = initialize_options(argv[1:])

reconfigure(colored=options.colored)

if options.version:
deps = get_deps_versions()
msg = f"ansible-lint [repr.number]{__version__}[/] using[dim]"
for k, v in deps.items():
msg += f" {k}:[repr.number]{v}[/]"
msg += "[/]"
console.print(msg)
msg = get_version_warning()
if msg:
console.print(msg)
support_banner()
sys.exit(0)
else:
support_banner()

initialize_logger(options.verbosity)
for level, message in log_entries:
_logger.log(level, message)
_logger.debug("Options: %s", options)
_logger.debug("CWD: %s", Path.cwd())
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter(action="ignore")

cache_dir_lock = initialize_options(argv[1:])

reconfigure(colored=options.colored)

if options.version:
deps = get_deps_versions()
msg = f"ansible-lint [repr.number]{__version__}[/] using[dim]"
for k, v in deps.items():
msg += f" {k}:[repr.number]{v}[/]"
msg += "[/]"
console.print(msg)
msg = get_version_warning()
if msg:
console.print(msg)

Check warning on line 298 in src/ansiblelint/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/__main__.py#L298

Added line #L298 was not covered by tests
support_banner()
sys.exit(0)
else:
support_banner()

initialize_logger(options.verbosity)
for level, message in log_entries:
_logger.log(level, message)
_logger.debug("Options: %s", options)
_logger.debug("CWD: %s", Path.cwd())

for warn in warns:
_logger.warning(str(warn.message))

Check warning on line 311 in src/ansiblelint/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/__main__.py#L311

Added line #L311 was not covered by tests
warnings.resetwarnings()
# checks if we have `ANSIBLE_LINT_SKIP_SCHEMA_UPDATE` set to bypass schema
# update. Also skip if in offline mode.
# env var set to skip schema refresh
Expand Down

0 comments on commit ab77812

Please sign in to comment.