From ee069a155b75e845d1ee7a3930a9538aa282cd53 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Fri, 8 Dec 2023 14:58:11 -0700 Subject: [PATCH] Fix broken tests --- salt/loader/context.py | 3 +++ tests/pytests/functional/loader/test_dunder.py | 14 ++++++-------- tests/pytests/unit/states/test_pkg.py | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/salt/loader/context.py b/salt/loader/context.py index 04e3bf094eb..a94e81d00a1 100644 --- a/salt/loader/context.py +++ b/salt/loader/context.py @@ -43,6 +43,9 @@ class NamedLoaderContext(collections.abc.MutableMapping): self.loader_context = loader_context self.default = default + def with_default(self, default): + return NamedLoaderContext(self.name, self.loader_context, default=default) + def loader(self): """ The LazyLoader in the current context. This will return None if there diff --git a/tests/pytests/functional/loader/test_dunder.py b/tests/pytests/functional/loader/test_dunder.py index 76770784fbc..581d669a12f 100644 --- a/tests/pytests/functional/loader/test_dunder.py +++ b/tests/pytests/functional/loader/test_dunder.py @@ -1,12 +1,10 @@ -import pathlib - import salt.loader.context import salt.loader.lazy import salt.utils.files import tests.support.helpers -def xtest_opts_dunder_opts_without_import(tempdir): +def test_opts_dunder_opts_without_import(tmp_path): """ Test __opts__ without being imported. @@ -14,7 +12,7 @@ def xtest_opts_dunder_opts_without_import(tempdir): salt.loader.dunder the __opts__ object will be a dictionary. """ opts = {"optimization_order": [0, 1, 2]} - with salt.utils.files.fopen(pathlib.Path(tempdir.tempdir) / "mymod.py", "w") as fp: + with salt.utils.files.fopen(tmp_path / "mymod.py", "w") as fp: fp.write( tests.support.helpers.dedent( """ @@ -23,11 +21,11 @@ def xtest_opts_dunder_opts_without_import(tempdir): """ ) ) - loader = salt.loader.lazy.LazyLoader([tempdir.tempdir], opts) + loader = salt.loader.lazy.LazyLoader([tmp_path], opts) assert type(loader["mymod.mymethod"]()) == dict -def test_opts_dunder_opts_with_import(tempdir): +def test_opts_dunder_opts_with_import(tmp_path): """ Test __opts__ when imported. @@ -35,7 +33,7 @@ def test_opts_dunder_opts_with_import(tempdir): salt.loader.dunder the __opts__ object will be a NamedLoaderContext. """ opts = {"optimization_order": [0, 1, 2]} - with salt.utils.files.fopen(pathlib.Path(tempdir.tempdir) / "mymod.py", "w") as fp: + with salt.utils.files.fopen(tmp_path / "mymod.py", "w") as fp: fp.write( tests.support.helpers.dedent( """ @@ -47,6 +45,6 @@ def test_opts_dunder_opts_with_import(tempdir): """ ) ) - loader = salt.loader.lazy.LazyLoader([tempdir.tempdir], opts) + loader = salt.loader.lazy.LazyLoader([tmp_path], opts) assert loader["mymod.optstype"]() == salt.loader.context.NamedLoaderContext assert loader["mymod.opts"]() == opts diff --git a/tests/pytests/unit/states/test_pkg.py b/tests/pytests/unit/states/test_pkg.py index 0255175005a..f9c566524df 100644 --- a/tests/pytests/unit/states/test_pkg.py +++ b/tests/pytests/unit/states/test_pkg.py @@ -11,6 +11,7 @@ import salt.modules.yumpkg as yumpkg import salt.states.beacon as beaconstate import salt.states.pkg as pkg import salt.utils.state as state_utils +from salt.loader.dunder import __opts__ from salt.utils.event import SaltEvent from tests.support.mock import MagicMock, patch @@ -21,7 +22,7 @@ log = logging.getLogger(__name__) def configure_loader_modules(minion_opts): return { cp: { - "__opts__": minion_opts, + "__opts__": __opts__.with_default(minion_opts), }, pkg: { "__env__": "base",