summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2024-07-18 07:18:14 +0200
committerGitHub <noreply@github.com>2024-07-18 07:18:14 +0200
commit9443e31b1e610c332370b2de09f1555b27524bfc (patch)
treee40daa2c73c8eec1f44775b0fb85dd5bd0a1c48c /lib
parent2d8ca8af6932dfd50204d1e4f6fe587dec7beef5 (diff)
downloadspack-9443e31b1e610c332370b2de09f1555b27524bfc.tar.gz
spack-9443e31b1e610c332370b2de09f1555b27524bfc.tar.bz2
spack-9443e31b1e610c332370b2de09f1555b27524bfc.tar.xz
spack-9443e31b1e610c332370b2de09f1555b27524bfc.zip
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/store.py1
-rw-r--r--lib/spack/spack/test/bootstrap.py22
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/spack/spack/store.py b/lib/spack/spack/store.py
index dc2d5de54b..8509d728aa 100644
--- a/lib/spack/spack/store.py
+++ b/lib/spack/spack/store.py
@@ -371,7 +371,6 @@ def use_store(
data.update(extra_data)
# Swap the store with the one just constructed and return it
- ensure_singleton_created()
spack.config.CONFIG.push_scope(
spack.config.InternalConfigScope(name=scope_name, data={"config": {"install_tree": data}})
)
diff --git a/lib/spack/spack/test/bootstrap.py b/lib/spack/spack/test/bootstrap.py
index 5f11e2d381..f0d11d4124 100644
--- a/lib/spack/spack/test/bootstrap.py
+++ b/lib/spack/spack/test/bootstrap.py
@@ -228,3 +228,25 @@ def test_source_is_disabled(mutable_config):
spack.config.add("bootstrap:trusted:{0}:{1}".format(conf["name"], False))
with pytest.raises(ValueError):
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