Skip to content

Commit 014b56d

Browse files
committed
Unifying testing under test command
1 parent 7bee05e commit 014b56d

File tree

1 file changed

+73
-29
lines changed

1 file changed

+73
-29
lines changed

mbed/mbed.py

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ def compile(toolchain=None, mcu=None, source=False, build=False, compile_library
18801880
macros = program.get_macros()
18811881

18821882
if compile_tests:
1883-
return test_(toolchain=toolchain, mcu=mcu, source=source, build=build, compile_tests=True)
1883+
return test_(toolchain=toolchain, mcu=mcu, source=source, build=build, compile_only=True)
18841884

18851885
if compile_config:
18861886
# Compile configuration
@@ -1923,51 +1923,95 @@ def compile(toolchain=None, mcu=None, source=False, build=False, compile_library
19231923

19241924
# Test command
19251925
@subcommand('test',
1926-
dict(name=['-L', '--list'], dest='list_tests', action='store_true', help='List all of the available tests'),
1927-
dict(name=['-C', '--compile'], dest='compile_tests', action='store_true', help='List all of the available tests'),
19281926
dict(name=['-t', '--toolchain'], help='Compile toolchain. Example: ARM, uARM, GCC_ARM, IAR'),
19291927
dict(name=['-m', '--mcu'], help='Compile target. Example: K64F, NUCLEO_F401RE, NRF51822...'),
1928+
dict(name='--list-compile', dest='list_compile', action='store_true', help='List all tests that can be built'),
1929+
dict(name='--list-run', dest='list_run', action='store_true', help='List all built tests that can be ran'),
1930+
dict(name='--compile', dest='compile_only', action='store_true', help='Only compile tests'),
1931+
dict(name='--run', dest='run_only', action='store_true', help='Only run tests'),
1932+
dict(name=['-n', '--tests-by-name'], dest='tests_by_name', help='Limit the tests to a list (ex. test1,test2,test3)'),
19301933
dict(name='--source', action='append', help='Source directory. Default: . (current dir)'),
19311934
dict(name='--build', help='Build directory. Default: .build/'),
1932-
dict(name='--test_spec', dest="test_spec", help="Destination path for a test spec file that can be used by the Greentea automated test tool. (Default is 'test_spec.json')"),
1933-
description='Find and build tests in a program and its libraries.',
1934-
help='List, build and run tests')
1935-
def test_(list_tests=False, compile_tests=False, test_spec="test_spec.json"):
1935+
dict(name=['-c', '--clean'], action='store_true', help='Clean the build directory before compiling'),
1936+
dict(name='--test-spec', dest="test_spec", help="Destination path for the test spec file used when running tests (only override if a test spec file that can be used by the Greentea automated test tool. The default is placed in the build directory"),
1937+
help='Find, build, and run tests in a program and its libraries')
1938+
def test_(toolchain=None, mcu=None, list_compile=False, list_run=False, compile_only=False, run_only=False, tests_by_name=None, source=False, build=False, clean=False, test_spec=None):
19361939
# Gather remaining arguments
19371940
args = remainder
19381941
# Find the root of the program
19391942
program = Program(os.getcwd(), True)
19401943
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
19411944
orig_path = os.getcwd()
1942-
1945+
1946+
# Change directories to the program root to use mbed OS tools
19431947
with cd(program.path):
1948+
target = program.get_mcu(mcu)
1949+
tchain = program.get_toolchain(toolchain)
1950+
macros = program.get_macros()
19441951
tools_dir = program.get_tools()
19451952

19461953
env = os.environ.copy()
19471954
env['PYTHONPATH'] = os.path.abspath(program.path)
1948-
1949-
if list_tests:
1950-
# List all available tests (by default in a human-readable format)
1951-
try:
1952-
popen(['python', '-u', os.path.join(tools_dir, 'test.py'), '-l'] + args, env=env)
1953-
except ProcessException:
1954-
error('Failed to run test script')
1955-
1956-
if compile_tests:
1957-
# Compile tests
1955+
1956+
# Setup the source path if not specified
1957+
if not source or len(source) == 0:
1958+
source = [os.path.relpath(program.path, orig_path)]
1959+
1960+
# Setup the build path if not specified
19581961
if not build:
19591962
build = os.path.join(os.path.relpath(program.path, orig_path), '.build/tests', target, tchain)
1960-
1961-
popen(['python', '-u', os.path.join(tools_dir, 'test.py')]
1962-
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
1963-
+ ['-t', tchain, '-m', target]
1964-
+ (['-c'] if clean else [])
1965-
+ list(chain.from_iterable(izip(repeat('--source'), source)))
1966-
+ ['--build', build]
1967-
+ ['--test-spec', test_spec]
1968-
+ (['-v'] if verbose else [])
1969-
+ args,
1970-
env=env)
1963+
1964+
# Create the path to the test spec file
1965+
test_spec = os.path.join(build, 'test_spec.json')
1966+
1967+
# Determine if building and running tests
1968+
build_and_run_tests = not list_compile and not list_run and not compile_only and not run_only
1969+
1970+
if compile_only or build_and_run_tests:
1971+
cmd = ['python', '-u', os.path.join(tools_dir, 'test.py')]
1972+
cmd += list(chain.from_iterable(izip(repeat('-D'), macros)))
1973+
cmd += ['-t', tchain, '-m', target]
1974+
cmd += (['-c'] if clean else [])
1975+
cmd += list(chain.from_iterable(izip(repeat('--source'), source)))
1976+
cmd += ['--build', build]
1977+
cmd += ['--test-spec', test_spec]
1978+
cmd += (['-n', tests_by_name] if tests_by_name else [])
1979+
cmd += (['-v'] if verbose else [])
1980+
1981+
try:
1982+
popen(cmd + args, env=env)
1983+
except ProcessException:
1984+
error('Failed to run the test compiling script')
1985+
1986+
if run_only or build_and_run_tests:
1987+
cmd = ['mbedgt', '--test-spec', test_spec]
1988+
cmd += (['-n', tests_by_name] if tests_by_name else [])
1989+
cmd += (['-V'] if verbose else [])
1990+
1991+
try:
1992+
popen(cmd + args, env=env)
1993+
except ProcessException:
1994+
error('Failed to run test runner')
1995+
1996+
if list_compile:
1997+
cmd = ['python', '-u', os.path.join(tools_dir, 'test.py'), '--list']
1998+
cmd += (['-n', tests_by_name] if tests_by_name else [])
1999+
cmd += (['-v'] if verbose else [])
2000+
2001+
try:
2002+
popen(cmd + args, env=env)
2003+
except ProcessException:
2004+
error('Failed to run buildable tests listing script')
2005+
2006+
if list_run:
2007+
cmd = ['mbedgt', '--test-spec', test_spec, '--list']
2008+
cmd += (['-n', tests_by_name] if tests_by_name else [])
2009+
cmd += (['-V'] if verbose else [])
2010+
2011+
try:
2012+
popen(cmd + args, env=env)
2013+
except ProcessException:
2014+
error('Failed to run test runner')
19712015

19722016

19732017
# Export command

0 commit comments

Comments
 (0)