Do not initialize previous store state in "use_store" (#45268)

The "use_store" context manager is used to swap the value
of a global variable (spack.store.STORE), while keeping
another global variable consistent (spack.config.CONFIG).

When doing that it tries to evaluate the previous value
of the store, if that was not done already. This is wrong,
since the configuration might be in an "intermediate" state
that was never meant to trigger side effects.

Remove that operation, and add a unit test to
prevent regressions.
This commit is contained in:
Massimiliano Culpo 2024-07-18 07:18:14 +02:00 committed by Harmen Stoppels
parent 85e67d60a0
commit ea2d43b4a6
2 changed files with 22 additions and 1 deletions

View file

@ -371,7 +371,6 @@ def use_store(
data.update(extra_data) data.update(extra_data)
# Swap the store with the one just constructed and return it # Swap the store with the one just constructed and return it
ensure_singleton_created()
spack.config.CONFIG.push_scope( spack.config.CONFIG.push_scope(
spack.config.InternalConfigScope(name=scope_name, data={"config": {"install_tree": data}}) spack.config.InternalConfigScope(name=scope_name, data={"config": {"install_tree": data}})
) )

View file

@ -228,3 +228,25 @@ def test_source_is_disabled(mutable_config):
spack.config.add("bootstrap:trusted:{0}:{1}".format(conf["name"], False)) spack.config.add("bootstrap:trusted:{0}:{1}".format(conf["name"], False))
with pytest.raises(ValueError): with pytest.raises(ValueError):
spack.bootstrap.core.source_is_enabled_or_raise(conf) spack.bootstrap.core.source_is_enabled_or_raise(conf)
@pytest.mark.regression("45247")
def test_use_store_does_not_try_writing_outside_root(tmp_path, monkeypatch, mutable_config):
"""Tests that when we use the 'use_store' context manager, there is no attempt at creating
a Store outside the given root.
"""
initial_store = mutable_config.get("config:install_tree:root")
user_store = tmp_path / "store"
fn = spack.store.Store.__init__
def _checked_init(self, root, *args, **kwargs):
fn(self, root, *args, **kwargs)
assert self.root == str(user_store)
monkeypatch.setattr(spack.store.Store, "__init__", _checked_init)
spack.store.reinitialize()
with spack.store.use_store(user_store):
assert spack.config.CONFIG.get("config:install_tree:root") == str(user_store)
assert spack.config.CONFIG.get("config:install_tree:root") == initial_store