From 2db85d240a8fe08613e135f36afaaaf3789b1d36 Mon Sep 17 00:00:00 2001 From: Peter Josef Scheibel Date: Wed, 12 Oct 2022 17:08:38 -0600 Subject: [PATCH] add test; set attributes on instance vs. on class --- lib/spack/spack/repo.py | 16 ++++++++++------ lib/spack/spack/test/concretize_preferences.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py index 8bdf1f22f71..0b511dab47c 100644 --- a/lib/spack/spack/repo.py +++ b/lib/spack/spack/repo.py @@ -1169,7 +1169,7 @@ def get(self, spec): package_class = self.get_pkg_class(spec.name) try: - return package_class(spec) + package_instance = package_class(spec) except spack.error.SpackError: # pass these through as their error messages will be fine. raise @@ -1182,6 +1182,15 @@ def get(self, spec): sys.excepthook(*sys.exc_info()) raise FailedConstructorError(spec.fullname, *sys.exc_info()) + # packages.yaml config can override package attributes. This is set + # on the instance rather than the class since the configuration can + # change between calls to repo.get for the same package. + settings = spack.config.get("packages").get(spec.name, {}).get("set", {}) + for key, val in settings.items(): + setattr(package_instance, key, val) + + return package_instance + @autospec def dump_provenance(self, spec, path): """Dump provenance information for a spec to a particular path. @@ -1362,11 +1371,6 @@ def get_pkg_class(self, pkg_name): if not inspect.isclass(cls): tty.die("%s.%s is not a class" % (pkg_name, class_name)) - # packages.yaml config can override package attributes - settings = spack.config.get("packages").get(pkg_name, {}).get("set", {}) - for key, val in settings.items(): - setattr(cls, key, val) - return cls def __str__(self): diff --git a/lib/spack/spack/test/concretize_preferences.py b/lib/spack/spack/test/concretize_preferences.py index 7bae90cb2cc..a29d3bbe4da 100644 --- a/lib/spack/spack/test/concretize_preferences.py +++ b/lib/spack/spack/test/concretize_preferences.py @@ -179,6 +179,22 @@ def test_preferred_providers(self): spec = concretize("mpileaks") assert "zmpi" in spec + def test_config_set_url_for_package(self, mutable_mock_repo): + """Test preferred providers of virtual packages are + applied correctly + """ + update_packages( + "mpileaks", + "set", + {"url": "http://www.somewhereelse.com/mpileaks-1.0.tar.gz"} + ) + spec = concretize("mpileaks") + assert spec.package.fetcher[0].url == "http://www.somewhereelse.com/mpileaks-2.3.tar.gz" + + update_packages("mpileaks", "set", {}) + spec = concretize("mpileaks") + assert spec.package.fetcher[0].url == "http://www.llnl.gov/mpileaks-2.3.tar.gz" + def test_preferred(self): """ "Test packages with some version marked as preferred=True""" spec = Spec("python")