diff --git a/.config/requirements-test.txt b/.config/requirements-test.txt index f62fa707e1..5e21fe9636 100644 --- a/.config/requirements-test.txt +++ b/.config/requirements-test.txt @@ -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 diff --git a/src/molecule/app.py b/src/molecule/app.py index 9ad032db92..5ba7fe4ab5 100644 --- a/src/molecule/app.py +++ b/src/molecule/app.py @@ -2,6 +2,8 @@ from __future__ import annotations +from functools import lru_cache + from ansible_compat.runtime import Runtime @@ -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() diff --git a/src/molecule/config.py b/src/molecule/config.py index 662b4ee107..86c3d78a81 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -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 @@ -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: @@ -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 diff --git a/src/molecule/scenario.py b/src/molecule/scenario.py index 5719c8b729..fded74ed1b 100644 --- a/src/molecule/scenario.py +++ b/src/molecule/scenario.py @@ -133,7 +133,6 @@ def ephemeral_directory(self) -> str: project_directory, self.name, ) - path = ephemeral_directory(project_scenario_directory) if isinstance(path, str): diff --git a/src/molecule/shell.py b/src/molecule/shell.py index b6648a6588..e5407b0fa4 100644 --- a/src/molecule/shell.py +++ b/src/molecule/shell.py @@ -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 @@ -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] " diff --git a/src/molecule/util.py b/src/molecule/util.py index 01adf9ed95..19a7c7a31b 100644 --- a/src/molecule/util.py +++ b/src/molecule/util.py @@ -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 @@ -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, @@ -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