Show useful error in build-env (#23458)

Instead of an out of bounds error tell the user to provide a spec
This commit is contained in:
Harmen Stoppels 2021-05-07 14:53:08 +02:00 committed by GitHub
parent 1698be3c3c
commit 129de9083a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -53,6 +53,9 @@ def emulate_env_utility(cmd_name, context, args):
spec = args.spec[0]
cmd = args.spec[1:]
if not spec:
tty.die("spack %s requires a spec." % cmd_name)
specs = spack.cmd.parse_specs(spec, concretize=False)
if len(specs) > 1:
tty.die("spack %s only takes one spec." % cmd_name)

View File

@ -6,9 +6,9 @@
from six.moves import cPickle
import pytest
from spack.main import SpackCommand, SpackCommandError
from spack.main import SpackCommand
info = SpackCommand('build-env')
build_env = SpackCommand('build-env')
@pytest.mark.parametrize('pkg', [
@ -17,17 +17,24 @@
])
@pytest.mark.usefixtures('config')
def test_it_just_runs(pkg):
info(*pkg)
build_env(*pkg)
@pytest.mark.parametrize('pkg,error_cls', [
('zlib libszip', SpackCommandError),
('', IndexError)
@pytest.mark.usefixtures('config')
def test_error_when_multiple_specs_are_given():
output = build_env('libelf libdwarf', fail_on_error=False)
assert 'only takes one spec' in output
@pytest.mark.parametrize('args', [
('--', '/bin/bash', '-c', 'echo test'),
('--',),
(),
])
@pytest.mark.usefixtures('config')
def test_it_just_fails(pkg, error_cls):
with pytest.raises(error_cls):
info(pkg)
def test_build_env_requires_a_spec(args):
output = build_env(*args, fail_on_error=False)
assert 'requires a spec' in output
_out_file = 'env.out'
@ -36,7 +43,7 @@ def test_it_just_fails(pkg, error_cls):
@pytest.mark.usefixtures('config')
def test_dump(tmpdir):
with tmpdir.as_cwd():
info('--dump', _out_file, 'zlib')
build_env('--dump', _out_file, 'zlib')
with open(_out_file) as f:
assert(any(line.startswith('PATH=') for line in f.readlines()))
@ -44,7 +51,7 @@ def test_dump(tmpdir):
@pytest.mark.usefixtures('config')
def test_pickle(tmpdir):
with tmpdir.as_cwd():
info('--pickle', _out_file, 'zlib')
build_env('--pickle', _out_file, 'zlib')
environment = cPickle.load(open(_out_file, 'rb'))
assert(type(environment) == dict)
assert('PATH' in environment)