Skip to content

Commit

Permalink
loader: Add __opts__ to pack if not already present
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen authored and Megan Wilhite committed Nov 7, 2022
1 parent 29812c3 commit d7168f8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/63013.fixed
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions doc/topics/development/modules/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions salt/loader/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/pytests/unit/loader/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
20 changes: 20 additions & 0 deletions tests/pytests/unit/loader/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit d7168f8

Please sign in to comment.