environment: ignore invalid files names in var/spack/environments (#10198)

This commit is contained in:
Denis Davydov 2018-12-26 07:22:21 +01:00 committed by Todd Gamblin
parent d9da7c98a2
commit 39b23d277b
3 changed files with 20 additions and 5 deletions

View file

@ -232,10 +232,15 @@ def get_env(args, cmd_name, required=False):
' spack -e ENV %s ...' % cmd_name)
def _root(name):
"""Non-validating version of root(), to be used internally."""
return os.path.join(env_path, name)
def root(name):
"""Get the root directory for an environment by name."""
validate_env_name(name)
return os.path.join(env_path, name)
return _root(name)
def exists(name):
@ -288,7 +293,7 @@ def all_environment_names():
candidates = sorted(os.listdir(env_path))
names = []
for candidate in candidates:
yaml_path = os.path.join(root(candidate), manifest_name)
yaml_path = os.path.join(_root(candidate), manifest_name)
if valid_env_name(candidate) and os.path.exists(yaml_path):
names.append(candidate)
return names

View file

@ -37,7 +37,7 @@ def test_add():
assert Spec('mpileaks') in e.user_specs
def test_env_list():
def test_env_list(mutable_mock_env_path):
env('create', 'foo')
env('create', 'bar')
env('create', 'baz')
@ -48,6 +48,15 @@ def test_env_list():
assert 'bar' in out
assert 'baz' in out
# make sure `spack env list` skips invalid things in var/spack/env
mutable_mock_env_path.join('.DS_Store').ensure(file=True)
out = env('list')
assert 'foo' in out
assert 'bar' in out
assert 'baz' in out
assert '.DS_Store' not in out
def test_env_remove(capfd):
env('create', 'foo')

View file

@ -703,8 +703,9 @@ def get_rev():
def mutable_mock_env_path(tmpdir_factory):
"""Fixture for mocking the internal spack environments directory."""
saved_path = spack.environment.env_path
spack.environment.env_path = str(tmpdir_factory.mktemp('mock-env-path'))
yield spack.environment.env_path
mock_path = tmpdir_factory.mktemp('mock-env-path')
spack.environment.env_path = str(mock_path)
yield mock_path
spack.environment.env_path = saved_path