Allow adding specs to an environment without the 'specs' attribute (#37378)

This commit is contained in:
Massimiliano Culpo 2023-05-03 13:01:16 +02:00 committed by GitHub
parent 7c8590ee44
commit 03d1841385
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -2606,8 +2606,8 @@ def add_user_spec(self, user_spec: str) -> None:
Args: Args:
user_spec: user spec to be appended user_spec: user spec to be appended
""" """
config_dict(self.pristine_yaml_content)["specs"].append(user_spec) config_dict(self.pristine_yaml_content).setdefault("specs", []).append(user_spec)
config_dict(self.yaml_content)["specs"].append(user_spec) config_dict(self.yaml_content).setdefault("specs", []).append(user_spec)
self.changed = True self.changed = True
def remove_user_spec(self, user_spec: str) -> None: def remove_user_spec(self, user_spec: str) -> None:

View file

@ -363,3 +363,25 @@ def test_error_on_nonempty_view_dir(tmpdir):
with pytest.raises(SpackEnvironmentViewError): with pytest.raises(SpackEnvironmentViewError):
_error_on_nonempty_view_dir("file") _error_on_nonempty_view_dir("file")
def test_can_add_specs_to_environment_without_specs_attribute(tmp_path, mock_packages, config):
"""Sometimes users have template manifest files, and save one line in the YAML file by
removing the empty 'specs: []' attribute. This test ensures that adding a spec to an
environment without the 'specs' attribute, creates the attribute first instead of returning
an error.
"""
spack_yaml = tmp_path / "spack.yaml"
spack_yaml.write_text(
"""
spack:
view: true
concretizer:
unify: true
"""
)
env = ev.Environment(tmp_path)
env.add("a")
assert len(env.user_specs) == 1
assert env.manifest.pristine_yaml_content["spack"]["specs"] == ["a"]