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] spec = args.spec[0]
cmd = args.spec[1:] cmd = args.spec[1:]
if not spec:
tty.die("spack %s requires a spec." % cmd_name)
specs = spack.cmd.parse_specs(spec, concretize=False) specs = spack.cmd.parse_specs(spec, concretize=False)
if len(specs) > 1: if len(specs) > 1:
tty.die("spack %s only takes one spec." % cmd_name) tty.die("spack %s only takes one spec." % cmd_name)

View File

@ -6,9 +6,9 @@
from six.moves import cPickle from six.moves import cPickle
import pytest 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', [ @pytest.mark.parametrize('pkg', [
@ -17,17 +17,24 @@
]) ])
@pytest.mark.usefixtures('config') @pytest.mark.usefixtures('config')
def test_it_just_runs(pkg): def test_it_just_runs(pkg):
info(*pkg) build_env(*pkg)
@pytest.mark.parametrize('pkg,error_cls', [ @pytest.mark.usefixtures('config')
('zlib libszip', SpackCommandError), def test_error_when_multiple_specs_are_given():
('', IndexError) 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') @pytest.mark.usefixtures('config')
def test_it_just_fails(pkg, error_cls): def test_build_env_requires_a_spec(args):
with pytest.raises(error_cls): output = build_env(*args, fail_on_error=False)
info(pkg) assert 'requires a spec' in output
_out_file = 'env.out' _out_file = 'env.out'
@ -36,7 +43,7 @@ def test_it_just_fails(pkg, error_cls):
@pytest.mark.usefixtures('config') @pytest.mark.usefixtures('config')
def test_dump(tmpdir): def test_dump(tmpdir):
with tmpdir.as_cwd(): with tmpdir.as_cwd():
info('--dump', _out_file, 'zlib') build_env('--dump', _out_file, 'zlib')
with open(_out_file) as f: with open(_out_file) as f:
assert(any(line.startswith('PATH=') for line in f.readlines())) assert(any(line.startswith('PATH=') for line in f.readlines()))
@ -44,7 +51,7 @@ def test_dump(tmpdir):
@pytest.mark.usefixtures('config') @pytest.mark.usefixtures('config')
def test_pickle(tmpdir): def test_pickle(tmpdir):
with tmpdir.as_cwd(): with tmpdir.as_cwd():
info('--pickle', _out_file, 'zlib') build_env('--pickle', _out_file, 'zlib')
environment = cPickle.load(open(_out_file, 'rb')) environment = cPickle.load(open(_out_file, 'rb'))
assert(type(environment) == dict) assert(type(environment) == dict)
assert('PATH' in environment) assert('PATH' in environment)