From 61c64467c912bde8ff36e0bfbf65d69b18413975 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 1 Nov 2022 03:06:11 -0400 Subject: [PATCH] loader: Add `__opts__` to pack if not already present --- changelog/63013.fixed | 3 +++ doc/topics/development/modules/developing.rst | 5 +++++ salt/loader/lazy.py | 4 ++++ tests/pytests/unit/loader/test_context.py | 9 +++++++++ tests/pytests/unit/loader/test_lazy.py | 20 +++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 changelog/63013.fixed diff --git a/changelog/63013.fixed b/changelog/63013.fixed new file mode 100644 index 000000000000..28cd29c7f372 --- /dev/null +++ b/changelog/63013.fixed @@ -0,0 +1,3 @@ +The `__opts__` dunder dictionary is now added to the loader's `pack` if not +already present, which makes it accessible via the +`salt.loader.context.NamedLoaderContext` class. diff --git a/doc/topics/development/modules/developing.rst b/doc/topics/development/modules/developing.rst index 33556a15eb07..5e9a9219ac4c 100644 --- a/doc/topics/development/modules/developing.rst +++ b/doc/topics/development/modules/developing.rst @@ -155,6 +155,11 @@ The following dunder dictionaries are always defined, but may be empty __opts__ -------- +..versionchanged:: 3006.0 + + The ``__opts__`` dictionary can now be accessed via + :py:mod:`~salt.loader.context``. + Defined in: All modules The ``__opts__`` dictionary contains all of the options passed in the diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py index 8a5d0dd267f3..d319fe54b429 100644 --- a/salt/loader/lazy.py +++ b/salt/loader/lazy.py @@ -590,6 +590,10 @@ def __prep_mod_opts(self, opts): if key == "logger": continue mod_opts[key] = val + + if "__opts__" not in self.pack: + self.pack["__opts__"] = mod_opts + return mod_opts def _iter_files(self, mod_name): diff --git a/tests/pytests/unit/loader/test_context.py b/tests/pytests/unit/loader/test_context.py index 28dbc63e2061..9815f443f258 100644 --- a/tests/pytests/unit/loader/test_context.py +++ b/tests/pytests/unit/loader/test_context.py @@ -45,3 +45,12 @@ def test_named_loader_context_deepcopy(): assert coppied.name == named_context.name assert id(coppied.loader_context) == id(named_context.loader_context) assert id(coppied.default) != id(named_context.default) + + +def test_named_loader_context_opts(): + loader_context = salt.loader.context.LoaderContext() + opts = loader_context.named_context("__opts__") + loader = salt.loader.lazy.LazyLoader(["/foo"], opts={"foo": "bar"}) + with salt.loader.context.loader_context(loader): + assert "foo" in opts + assert opts["foo"] == "bar" diff --git a/tests/pytests/unit/loader/test_lazy.py b/tests/pytests/unit/loader/test_lazy.py index df5039a7193d..571f458b3604 100644 --- a/tests/pytests/unit/loader/test_lazy.py +++ b/tests/pytests/unit/loader/test_lazy.py @@ -119,3 +119,23 @@ def test_missing_loader_from_salt_internal_loaders(): salt.loader._module_dirs( {"extension_modules": "/tmp/foo"}, "missingmodules", "module" ) + + +def test_loader_pack_always_has_opts(loader_dir): + loader = salt.loader.lazy.LazyLoader([loader_dir], opts={"foo": "bar"}) + assert "__opts__" in loader.pack + assert "foo" in loader.pack["__opts__"] + assert loader.pack["__opts__"]["foo"] == "bar" + + +def test_loader_pack_opts_not_overwritten(loader_dir): + opts = {"foo": "bar"} + loader = salt.loader.lazy.LazyLoader( + [loader_dir], + opts={"foo": "bar"}, + pack={"__opts__": {"baz": "bif"}}, + ) + assert "__opts__" in loader.pack + assert "foo" not in loader.pack["__opts__"] + assert "baz" in loader.pack["__opts__"] + assert loader.pack["__opts__"]["baz"] == "bif"