Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader: Add __opts__ to pack if not already present #63013

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"