From da9a56218273dd6d778b341e685520e03edc4e58 Mon Sep 17 00:00:00 2001 From: Greg Becker Date: Mon, 9 Dec 2019 12:23:03 -0800 Subject: [PATCH] environments: allow 'add' command to add virtuals (#13787) This PR allows virtual packages to be added to the specs list using the add command. Virtual packages are already allowed in named lists in spack environments/stacks, and they are already allowed in the specs list when added using the yaml directly. --- lib/spack/spack/environment.py | 5 ++++- lib/spack/spack/test/cmd/env.py | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/spack/spack/environment.py b/lib/spack/spack/environment.py index 2c7a0cf098..2be355df2d 100644 --- a/lib/spack/spack/environment.py +++ b/lib/spack/spack/environment.py @@ -813,7 +813,10 @@ def add(self, user_spec, list_name=user_speclist_name): raise SpackEnvironmentError( 'cannot add anonymous specs to an environment!') elif not spack.repo.path.exists(spec.name): - raise SpackEnvironmentError('no such package: %s' % spec.name) + virtuals = spack.repo.path.provider_index.providers.keys() + if spec.name not in virtuals: + msg = 'no such package: %s' % spec.name + raise SpackEnvironmentError(msg) list_to_change = self.spec_lists[list_name] existing = str(spec) in list_to_change.yaml_list diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 9b3ca6e66e..85413a7939 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -63,6 +63,27 @@ def test_add(): assert Spec('mpileaks') in e.user_specs +def test_env_add_virtual(): + env('create', 'test') + + e = ev.read('test') + e.add('mpi') + e.concretize() + + hashes = e.concretized_order + assert len(hashes) == 1 + spec = e.specs_by_hash[hashes[0]] + assert spec.satisfies('mpi') + + +def test_env_add_nonexistant_fails(): + env('create', 'test') + + e = ev.read('test') + with pytest.raises(ev.SpackEnvironmentError, match=r'no such package'): + e.add('thispackagedoesnotexist') + + def test_env_list(mutable_mock_env_path): env('create', 'foo') env('create', 'bar') @@ -1777,7 +1798,7 @@ def test_duplicate_packages_raise_when_concretizing_together(): def test_env_write_only_non_default(): - print(env('create', 'test')) + env('create', 'test') e = ev.read('test') with open(e.manifest_path, 'r') as f: