Skip to content

Commit

Permalink
Improve spack test command a bit.
Browse files Browse the repository at this point in the history
- Now supports an approximation of the old simple interface
- Also supports full pytest options if you want them.
  • Loading branch information
tgamblin committed Dec 29, 2016
1 parent d471346 commit a58867b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
9 changes: 1 addition & 8 deletions lib/spack/spack/cmd/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import spack.cmd

description = "Get help on spack and its commands"


Expand All @@ -34,11 +32,6 @@ def setup_parser(subparser):

def help(parser, args):
if args.help_command:
if args.help_command == 'test':
# spack test is a bit special.
test = spack.cmd.get_command('test')
return test(parser, [], ['-h'])
else:
parser.parse_args([args.help_command, '-h'])
parser.parse_args([args.help_command, '-h'])
else:
parser.print_help()
71 changes: 68 additions & 3 deletions lib/spack/spack/cmd/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,86 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import sys
import os
import re
import argparse
import pytest
from StringIO import StringIO

from llnl.util.filesystem import *
from llnl.util.tty.colify import colify

import spack
from spack.cmd import remove_options

description = "A thin wrapper around the pytest command."


def setup_parser(subparser):
remove_options(subparser, '-h', '--help')
subparser.add_argument(
'-H', '--pytest-help', action='store_true', default=False,
help="print full pytest help message, showing advanced options.")

list_group = subparser.add_mutually_exclusive_group()
list_group.add_argument(
'-l', '--list', action='store_true', default=False,
help="list basic test names.")
list_group.add_argument(
'-L', '--long-list', action='store_true', default=False,
help="list the entire hierarchy of tests.")
subparser.add_argument(
'tests', nargs=argparse.REMAINDER,
help="list of tests to run (will be passed to pytest -k).")


def do_list(args, unknown_args):
"""Print a lists of tests than what pytest offers."""
# Run test collection and get the tree out.
old_output = sys.stdout
try:
sys.stdout = output = StringIO()
pytest.main(['--collect-only'])
finally:
sys.stdout = old_output

# put the output in a more readable tree format.
lines = output.getvalue().split('\n')
output_lines = []
for line in lines:
match = re.match(r"(\s*)<([^ ]*) '([^']*)'", line)
if not match:
continue
indent, nodetype, name = match.groups()

# only print top-level for short list
if args.list:
if not indent:
output_lines.append(
os.path.basename(name).replace('.py', ''))
else:
print indent + name

if args.list:
colify(output_lines)


def test(parser, args, unknown_args):
if args.pytest_help:
# make the pytest.main help output more accurate
sys.argv[0] = 'spack test'
pytest.main(['-h'])
return

# pytest.ini lives in the root of the sapck repository.
with working_dir(spack.prefix):
return pytest.main(unknown_args)
# --list and --long-list print the test output better.
if args.list or args.long_list:
do_list(args, unknown_args)
return

if args.tests and not any(arg.startswith('-') for arg in args.tests):
# Allow keyword search without -k if no options are specified
return pytest.main(['-k'] + args.tests)
else:
# Just run the pytest command.
return pytest.main(unknown_args + args.tests)
2 changes: 2 additions & 0 deletions lib/spack/spack/test/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_import_package_as(builtin_mock):
import spack.pkg.builtin.mock as m # noqa
from spack.pkg.builtin import mock # noqa


def test_inheritance_of_diretives():
p = spack.repo.get('simple_inheritance')

Expand All @@ -106,6 +107,7 @@ def test_inheritance_of_diretives():
assert '~openblas' in s
assert 'mpi' in s


def test_import_class_from_package(builtin_mock):
from spack.pkg.builtin.mock.mpich import Mpich # noqa

Expand Down

0 comments on commit a58867b

Please sign in to comment.