summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2023-05-03 13:01:16 +0200
committerGitHub <noreply@github.com>2023-05-03 13:01:16 +0200
commit03d18413853de8e1feb3068acf7f19e54c21dd12 (patch)
treee50e2f47d290074edd3c649a4840718e8c7bd5e9 /lib
parent7c8590ee4436d3d3706d60934459ba2717e000f7 (diff)
downloadspack-03d18413853de8e1feb3068acf7f19e54c21dd12.tar.gz
spack-03d18413853de8e1feb3068acf7f19e54c21dd12.tar.bz2
spack-03d18413853de8e1feb3068acf7f19e54c21dd12.tar.xz
spack-03d18413853de8e1feb3068acf7f19e54c21dd12.zip
Allow adding specs to an environment without the 'specs' attribute (#37378)
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/environment/environment.py4
-rw-r--r--lib/spack/spack/test/env.py22
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/spack/spack/environment/environment.py b/lib/spack/spack/environment/environment.py
index eedca8daaa..9c1c2e4346 100644
--- a/lib/spack/spack/environment/environment.py
+++ b/lib/spack/spack/environment/environment.py
@@ -2606,8 +2606,8 @@ class EnvironmentManifestFile(collections.abc.Mapping):
Args:
user_spec: user spec to be appended
"""
- config_dict(self.pristine_yaml_content)["specs"].append(user_spec)
- config_dict(self.yaml_content)["specs"].append(user_spec)
+ config_dict(self.pristine_yaml_content).setdefault("specs", []).append(user_spec)
+ config_dict(self.yaml_content).setdefault("specs", []).append(user_spec)
self.changed = True
def remove_user_spec(self, user_spec: str) -> None:
diff --git a/lib/spack/spack/test/env.py b/lib/spack/spack/test/env.py
index 913eb7bf9e..16a8cb4ff8 100644
--- a/lib/spack/spack/test/env.py
+++ b/lib/spack/spack/test/env.py
@@ -363,3 +363,25 @@ def test_error_on_nonempty_view_dir(tmpdir):
with pytest.raises(SpackEnvironmentViewError):
_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"]