fix create/remove env with invalid spack.yaml (#39898)

* fix create/remove env with invalid spack.yaml
* fix isort error
* fix env ident unittests
* Fix pull request points
This commit is contained in:
Luisa Burini 2023-10-31 19:39:42 -03:00 committed by GitHub
parent 4738b45fb1
commit e5cebb6b6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 27 deletions

View File

@ -380,28 +380,35 @@ def env_remove(args):
and manifests embedded in repositories should be removed manually.
"""
read_envs = []
bad_envs = []
for env_name in args.rm_env:
try:
env = ev.read(env_name)
read_envs.append(env)
except spack.config.ConfigFormatError:
bad_envs.append(env_name)
if not args.yes_to_all:
answer = tty.get_yes_or_no(
"Really remove %s %s?"
% (
string.plural(len(args.rm_env), "environment", show_n=False),
string.comma_and(args.rm_env),
),
default=False,
)
environments = string.plural(len(args.rm_env), "environment", show_n=False)
envs = string.comma_and(args.rm_env)
answer = tty.get_yes_or_no(f"Really remove {environments} {envs}?", default=False)
if not answer:
tty.die("Will not remove any environments")
for env in read_envs:
name = env.name
if env.active:
tty.die("Environment %s can't be removed while activated." % env.name)
tty.die(f"Environment {name} can't be removed while activated.")
env.destroy()
tty.msg("Successfully removed environment '%s'" % env.name)
tty.msg(f"Successfully removed environment {name}")
for bad_env_name in bad_envs:
shutil.rmtree(
spack.environment.environment.environment_dir_from_name(
bad_env_name, exists_ok=True
)
)
tty.msg(f"Successfully removed environment '{bad_env_name}'")
#

View File

@ -330,6 +330,7 @@ def create_in_dir(
if with_view is None and keep_relative:
return Environment(manifest_dir)
try:
manifest = EnvironmentManifestFile(manifest_dir)
if with_view is not None:
@ -341,6 +342,10 @@ def create_in_dir(
manifest.flush()
except spack.config.ConfigFormatError as e:
shutil.rmtree(manifest_dir)
raise e
return Environment(manifest_dir)

View File

@ -991,9 +991,27 @@ def test_bad_env_yaml_format(tmpdir):
with tmpdir.as_cwd():
with pytest.raises(spack.config.ConfigFormatError) as e:
env("create", "test", "./spack.yaml")
assert "spack.yaml:2" in str(e)
assert "'spacks' was unexpected" in str(e)
assert "test" not in env("list")
def test_bad_env_yaml_format_remove():
badenv = "badenv"
env("create", badenv)
tmpdir = spack.environment.environment.environment_dir_from_name(badenv, exists_ok=True)
filename = os.path.join(tmpdir, "spack.yaml")
with open(filename, "w") as f:
f.write(
"""\
- mpileaks
"""
)
assert badenv in env("list")
env("remove", "-y", badenv)
assert badenv not in env("list")
def test_env_loads(install_mockery, mock_fetch):
env("create", "test")