Skip to content

Commit e2eace6

Browse files
committed
Making test command agnostic to execution directory, fixing style
1 parent a101d7a commit e2eace6

File tree

1 file changed

+45
-59
lines changed

1 file changed

+45
-59
lines changed

mbed/mbed.py

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,75 +1949,61 @@ def test_(toolchain=None, mcu=None, compile_list=False, run_list=False, compile_
19491949
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
19501950
orig_path = os.getcwd()
19511951

1952-
# Change directories to the program root to use mbed OS tools
1952+
target = program.get_mcu(mcu)
1953+
tchain = program.get_toolchain(toolchain)
1954+
macros = program.get_macros()
1955+
tools_dir = program.get_tools()
1956+
build_and_run_tests = not compile_list and not run_list and not compile_only and not run_only
1957+
relative_orig_path = os.path.relpath(orig_path, program.path)
1958+
1959+
env = os.environ.copy()
1960+
env['PYTHONPATH'] = os.path.abspath(program.path)
1961+
19531962
with cd(program.path):
1954-
target = program.get_mcu(mcu)
1955-
tchain = program.get_toolchain(toolchain)
1956-
macros = program.get_macros()
1957-
tools_dir = program.get_tools()
1958-
1959-
env = os.environ.copy()
1960-
env['PYTHONPATH'] = os.path.abspath(program.path)
1961-
19621963
# Setup the source path if not specified
19631964
if not source or len(source) == 0:
1964-
source = [os.path.relpath(program.path, orig_path)]
1965+
source = [program.path]
19651966

19661967
# Setup the build path if not specified
19671968
if not build:
1968-
build = os.path.join(os.path.relpath(program.path, orig_path), '.build/tests', target, tchain)
1969-
1969+
build = os.path.join(program.path, '.build/tests', target, tchain)
1970+
19701971
# Create the path to the test spec file
19711972
test_spec = os.path.join(build, 'test_spec.json')
1972-
1973-
# Determine if building and running tests
1974-
build_and_run_tests = not compile_list and not run_list and not compile_only and not run_only
1975-
1976-
if compile_only or build_and_run_tests:
1977-
cmd = ['python', '-u', os.path.join(tools_dir, 'test.py')]
1978-
cmd += list(chain.from_iterable(izip(repeat('-D'), macros)))
1979-
cmd += ['-t', tchain, '-m', target]
1980-
cmd += (['-c'] if clean else [])
1981-
cmd += list(chain.from_iterable(izip(repeat('--source'), source)))
1982-
cmd += ['--build', build]
1983-
cmd += ['--test-spec', test_spec]
1984-
cmd += (['-n', tests_by_name] if tests_by_name else [])
1985-
cmd += (['-v'] if very_verbose else [])
1986-
1987-
try:
1988-
popen(cmd + args, env=env)
1989-
except ProcessException:
1990-
error('Failed to run the test compiling script')
1991-
1992-
if run_only or build_and_run_tests:
1993-
cmd = ['mbedgt', '--test-spec', test_spec]
1994-
cmd += (['-n', tests_by_name] if tests_by_name else [])
1995-
cmd += (['-V'] if verbose else [])
1996-
1997-
try:
1998-
popen(cmd + args, env=env)
1999-
except ProcessException:
2000-
error('Failed to run test runner')
2001-
1973+
20021974
if compile_list:
2003-
cmd = ['python', '-u', os.path.join(tools_dir, 'test.py'), '--list']
2004-
cmd += (['-n', tests_by_name] if tests_by_name else [])
2005-
cmd += (['-v'] if very_verbose else [])
2006-
2007-
try:
2008-
popen(cmd + args, env=env)
2009-
except ProcessException:
2010-
error('Failed to run buildable tests listing script')
2011-
1975+
popen(['python', '-u', os.path.join(tools_dir, 'test.py'), '--list']
1976+
+ (['-n', tests_by_name] if tests_by_name else [])
1977+
+ (['-v'] if very_verbose else [])
1978+
+ args,
1979+
env=env)
1980+
1981+
if compile_only or build_and_run_tests:
1982+
popen(['python', '-u', os.path.join(tools_dir, 'test.py')]
1983+
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
1984+
+ ['-t', tchain, '-m', target]
1985+
+ (['-c'] if clean else [])
1986+
+ list(chain.from_iterable(izip(repeat('--source'), source)))
1987+
+ ['--build', build]
1988+
+ ['--test-spec', test_spec]
1989+
+ (['-n', tests_by_name] if tests_by_name else [])
1990+
+ (['-v'] if very_verbose else [])
1991+
+ args,
1992+
env=env)
1993+
20121994
if run_list:
2013-
cmd = ['mbedgt', '--test-spec', test_spec, '--list']
2014-
cmd += (['-n', tests_by_name] if tests_by_name else [])
2015-
cmd += (['-V'] if verbose else [])
2016-
2017-
try:
2018-
popen(cmd + args, env=env)
2019-
except ProcessException:
2020-
error('Failed to run test runner')
1995+
popen(['mbedgt', '--test-spec', test_spec, '--list']
1996+
+ (['-n', tests_by_name] if tests_by_name else [])
1997+
+ (['-V'] if very_verbose else [])
1998+
+ args,
1999+
env=env)
2000+
2001+
if run_only or build_and_run_tests:
2002+
popen(['mbedgt', '--test-spec', test_spec]
2003+
+ (['-n', tests_by_name] if tests_by_name else [])
2004+
+ (['-V'] if very_verbose else [])
2005+
+ args,
2006+
env=env)
20212007

20222008

20232009
# Export command

0 commit comments

Comments
 (0)