Skip to content

Commit

Permalink
Ensure we catch warnings before logger is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jan 4, 2025
1 parent 3f9958b commit 4cc66f5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .config/requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pydoclint
pylint
pytest
pytest-mock >= 3.10.0
pytest-plus >= 0.4.0
pytest-plus >= 0.7.0
pytest-xdist
pytest-instafail
requests != 2.32.0 # https://github.com/docker/docker-py/issues/3256
ruff
toml-sort
Expand Down
11 changes: 10 additions & 1 deletion src/molecule/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from functools import lru_cache

from ansible_compat.runtime import Runtime


Expand All @@ -13,4 +15,11 @@ def __init__(self) -> None:
self.runtime = Runtime(isolated=False)


app = App()
@lru_cache
def get_app() -> App:
"""Return the app instance.
Returns:
App: The app instance.
"""
return App()
6 changes: 3 additions & 3 deletions src/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from packaging.version import Version

from molecule import api, interpolation, platforms, scenario, state, util
from molecule.app import app
from molecule.app import get_app
from molecule.data import __file__ as data_module
from molecule.dependency import ansible_galaxy, shell
from molecule.model import schema_v3
Expand Down Expand Up @@ -74,7 +74,7 @@ def ansible_version() -> Version:
"molecule.config.ansible_version is deprecated, will be removed in the future.",
category=DeprecationWarning,
)
return app.runtime.version
return get_app().runtime.version


class Config:
Expand Down Expand Up @@ -121,7 +121,7 @@ def __init__(
"MOLECULE_PROJECT_DIRECTORY",
os.getcwd(), # noqa: PTH109
)
self.runtime = app.runtime
self.runtime = get_app().runtime
self.scenario_path = Path(molecule_file).parent

# Former after_init() contents
Expand Down
1 change: 0 additions & 1 deletion src/molecule/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def ephemeral_directory(self) -> str:
project_directory,
self.name,
)

path = ephemeral_directory(project_scenario_directory)

if isinstance(path, str):
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from molecule import command, logger
from molecule.api import drivers
from molecule.app import app
from molecule.app import get_app
from molecule.command.base import click_group_ex
from molecule.config import MOLECULE_DEBUG, MOLECULE_VERBOSITY
from molecule.console import console
Expand Down Expand Up @@ -77,7 +77,7 @@ def print_version(
f"using python [repr.number]{sys.version_info[0]}.{sys.version_info[1]}[/] \n"
)

msg += f" [repr.attrib_name]ansible[/][dim]:[/][repr.number]{app.runtime.version}[/]"
msg += f" [repr.attrib_name]ansible[/][dim]:[/][repr.number]{get_app().runtime.version}[/]"
for driver in drivers().values():
msg += (
f"\n [repr.attrib_name]{driver!s}[/][dim]:[/][repr.number]{driver.version}[/][dim] "
Expand Down
6 changes: 3 additions & 3 deletions src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from ansible_compat.ports import cache
from rich.syntax import Syntax

from molecule.app import app
from molecule.app import get_app
from molecule.console import console
from molecule.constants import MOLECULE_HEADER

Expand Down Expand Up @@ -183,7 +183,7 @@ def run_command( # noqa: PLR0913
if debug:
print_environment_vars(env)

result = app.runtime.run(
result = get_app().runtime.run(
args=cmd,
env=env,
cwd=cwd,
Expand Down Expand Up @@ -387,7 +387,7 @@ def filter_verbose_permutation(options: Options) -> Options:
Returns:
Dictionary of options without verbose options included.
"""
return {k: options[k] for k in options if not re.match("^[v]+$", k)}
return {k: options[k] for k in options if not re.match(r"^[v]+$", k)}


@overload
Expand Down

0 comments on commit 4cc66f5

Please sign in to comment.