Skip to content

Commit 27c1de5

Browse files
committed
Move unittest to test --unittests
1 parent 5085b9d commit 27c1de5

File tree

1 file changed

+101
-113
lines changed

1 file changed

+101
-113
lines changed

mbed/mbed.py

Lines changed: 101 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,9 +2604,19 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
26042604
dict(name='--test-spec', dest="test_spec", help="Path used for the test spec file used when building and running tests (the default path is the build directory)"),
26052605
dict(name='--app-config', dest="app_config", help="Path of an application configuration file. Default is to look for \"mbed_app.json\""),
26062606
dict(name='--test-config', dest="test_config", help="Path or mbed OS keyword of a test configuration file. Example: ethernet, odin_wifi, or path/to/config.json"),
2607+
dict(name='--coverage', choices=['html', 'xml', 'both'], help='Generate code coverage report for unit tetsts'),
2608+
dict(name=['--make-program'], choices=['gmake', 'make', 'mingw32-make', 'ninja'], help='Which make program to use for unit tests'),
2609+
dict(name=['--generator'], choices=['Unix Makefiles', 'MinGW Makefiles', 'Ninja'], help='Which CMake generator to use for unit tests'),
2610+
dict(name='--new', help='generate files for a new unit test', metavar="FILEPATH"),
2611+
dict(name=['-r', '--regex'], help='Run unit tests matching regular expression'),
2612+
dict(name=['--unittests'], action="store_true", help='Run only unit tests'),
26072613
help='Find, build and run tests',
26082614
description="Find, build, and run tests in a program and libraries")
2609-
def test_(toolchain=None, target=None, compile_list=False, run_list=False, compile_only=False, run_only=False, tests_by_name=None, source=False, profile=False, build=False, clean=False, test_spec=None, app_config=None, test_config=None):
2615+
def test_(toolchain=None, target=None, compile_list=False, run_list=False,
2616+
compile_only=False, run_only=False, tests_by_name=None, source=False,
2617+
profile=False, build=False, clean=False, test_spec=None,
2618+
app_config=None, test_config=None, coverage=None, make_program=None,
2619+
new=None, generator=None, regex=None, unittests=None):
26102620
# Gather remaining arguments
26112621
args = remainder
26122622
# Find the root of the program
@@ -2625,68 +2635,100 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
26252635
env = program.get_env()
26262636

26272637
with cd(program.path):
2628-
# Setup the source path if not specified
2629-
if not source or len(source) == 0:
2630-
source = [os.path.relpath(program.path, orig_path)]
2631-
2632-
# Setup the build path if not specified
2633-
build_path = build
2634-
if not build_path:
2635-
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, 'tests', target.upper(), tchain.upper())
2636-
build_path = _safe_append_profile_to_build_path(build_path, profile)
2637-
2638-
if test_spec:
2639-
# Preserve path to given test spec
2640-
test_spec = os.path.relpath(os.path.join(orig_path, test_spec), program.path)
2638+
if unittests:
2639+
mbed_os_dir = program.get_os_dir()
2640+
if mbed_os_dir is None:
2641+
error("No Mbed OS directory found.")
2642+
unittests_dir = os.path.join(mbed_os_dir, "UNITTESTS")
2643+
2644+
tool = os.path.join(unittests_dir, "mbed_unittest.py")
2645+
if os.path.exists(tool):
2646+
# Setup the build path if not specified
2647+
build_path = build
2648+
if not build_path:
2649+
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, 'unittests')
2650+
2651+
# Run unit testing tools
2652+
popen([python_cmd, tool]
2653+
+ (["--compile"] if compile_only else [])
2654+
+ (["--run"] if run_only else [])
2655+
+ (["--clean"] if clean else [])
2656+
+ (["--debug"] if profile and "debug" in profile else [])
2657+
+ (["--coverage", coverage] if coverage else [])
2658+
+ (["--make-program", make_program] if make_program else [])
2659+
+ (["--generator", generator] if generator else [])
2660+
+ (["--regex", regex] if regex else [])
2661+
+ ["--build", build_path]
2662+
+ (["--new", new] if new else [])
2663+
+ (["--verbose"] if verbose else [])
2664+
+ remainder,
2665+
env=env)
2666+
else:
2667+
warning("Unit testing is not supported with this Mbed OS version.")
26412668
else:
2642-
# Create the path to the test spec file
2643-
test_spec = os.path.join(build_path, 'test_spec.json')
2644-
2645-
if compile_list:
2646-
popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py'), '--list']
2647-
+ list(chain.from_iterable(list(zip(repeat('--profile'), profile or []))))
2648-
+ ['-t', tchain, '-m', target]
2649-
+ list(chain.from_iterable(zip(repeat('--source'), source)))
2650-
+ (['-n', tests_by_name] if tests_by_name else [])
2651-
+ (['-v'] if verbose else [])
2652-
+ (['--app-config', app_config] if app_config else [])
2653-
+ (['--test-config', test_config] if test_config else [])
2654-
+ args,
2655-
env=env)
2656-
2657-
if compile_only or build_and_run_tests:
2658-
# If the user hasn't supplied a build directory, ignore the default build directory
2659-
if not build:
2660-
program.ignore_build_dir()
2669+
# Setup the source path if not specified
2670+
if not source or len(source) == 0:
2671+
source = [os.path.relpath(program.path, orig_path)]
26612672

2662-
popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py')]
2663-
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
2664-
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
2665-
+ ['-t', tchain, '-m', target]
2666-
+ (['-c'] if clean else [])
2667-
+ list(chain.from_iterable(zip(repeat('--source'), source)))
2668-
+ ['--build', build_path]
2669-
+ ['--test-spec', test_spec]
2670-
+ (['-n', tests_by_name] if tests_by_name else [])
2671-
+ (['-v'] if verbose else [])
2672-
+ (['--app-config', app_config] if app_config else [])
2673-
+ (['--test-config', test_config] if test_config else [])
2674-
+ args,
2675-
env=env)
2673+
# Setup the build path if not specified
2674+
build_path = build
2675+
if not build_path:
2676+
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, 'tests', target.upper(), tchain.upper())
2677+
build_path = _safe_append_profile_to_build_path(build_path, profile)
26762678

2677-
if run_list:
2678-
popen(['mbedgt', '--test-spec', test_spec, '--list']
2679-
+ (['-n', tests_by_name] if tests_by_name else [])
2680-
+ (['-V'] if verbose else [])
2681-
+ args,
2682-
env=env)
2679+
if test_spec:
2680+
# Preserve path to given test spec
2681+
test_spec = os.path.relpath(os.path.join(orig_path, test_spec), program.path)
2682+
else:
2683+
# Create the path to the test spec file
2684+
test_spec = os.path.join(build_path, 'test_spec.json')
2685+
2686+
if compile_list:
2687+
popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py'), '--list']
2688+
+ list(chain.from_iterable(list(zip(repeat('--profile'), profile or []))))
2689+
+ ['-t', tchain, '-m', target]
2690+
+ list(chain.from_iterable(zip(repeat('--source'), source)))
2691+
+ (['-n', tests_by_name] if tests_by_name else [])
2692+
+ (['-v'] if verbose else [])
2693+
+ (['--app-config', app_config] if app_config else [])
2694+
+ (['--test-config', test_config] if test_config else [])
2695+
+ args,
2696+
env=env)
2697+
2698+
if compile_only or build_and_run_tests:
2699+
# If the user hasn't supplied a build directory, ignore the default build directory
2700+
if not build:
2701+
program.ignore_build_dir()
2702+
2703+
popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py')]
2704+
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
2705+
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
2706+
+ ['-t', tchain, '-m', target]
2707+
+ (['-c'] if clean else [])
2708+
+ list(chain.from_iterable(zip(repeat('--source'), source)))
2709+
+ ['--build', build_path]
2710+
+ ['--test-spec', test_spec]
2711+
+ (['-n', tests_by_name] if tests_by_name else [])
2712+
+ (['-v'] if verbose else [])
2713+
+ (['--app-config', app_config] if app_config else [])
2714+
+ (['--test-config', test_config] if test_config else [])
2715+
+ args,
2716+
env=env)
2717+
2718+
if run_list:
2719+
popen(['mbedgt', '--test-spec', test_spec, '--list']
2720+
+ (['-n', tests_by_name] if tests_by_name else [])
2721+
+ (['-V'] if verbose else [])
2722+
+ args,
2723+
env=env)
2724+
2725+
if run_only or build_and_run_tests:
2726+
popen(['mbedgt', '--test-spec', test_spec]
2727+
+ (['-n', tests_by_name] if tests_by_name else [])
2728+
+ (['-V'] if verbose else [])
2729+
+ args,
2730+
env=env)
26832731

2684-
if run_only or build_and_run_tests:
2685-
popen(['mbedgt', '--test-spec', test_spec]
2686-
+ (['-n', tests_by_name] if tests_by_name else [])
2687-
+ (['-V'] if verbose else [])
2688-
+ args,
2689-
env=env)
26902732

26912733
program.set_defaults(target=target, toolchain=tchain)
26922734

@@ -3011,60 +3053,6 @@ def help_():
30113053
return parser.print_help()
30123054

30133055

3014-
@subcommand('unittest',
3015-
dict(name='--compile', action='store_true', dest="compile_only", help='Only compile unit tests'),
3016-
dict(name='--run', action='store_true', dest="run_only", help='Only run unit tests'),
3017-
dict(name=['-c', '--clean'], action='store_true', help='Clean the build directory'),
3018-
dict(name=['-d', '--debug'], action='store_true', help='Enable debug build'),
3019-
dict(name='--coverage', choices=['html', 'xml', 'both'], help='Generate code coverage report'),
3020-
dict(name=['-m', '--make-program'], choices=['gmake', 'make', 'mingw32-make', 'ninja'], help='Which make program to use'),
3021-
dict(name=['-g', '--generator'], choices=['Unix Makefiles', 'MinGW Makefiles', 'Ninja'], help='Which CMake generator to use'),
3022-
dict(name=['-r', '--regex'], help='Run tests matching regular expression'),
3023-
dict(name='--build', help='Build directory. Default: BUILD/unittests/'),
3024-
dict(name='--new', help='generate files for a new unit test', metavar="FILEPATH"),
3025-
help='Create, build and run unit tests')
3026-
def unittest_(compile_only=False, run_only=False, clean=False, debug=False, coverage=None, make_program=None, generator=None, regex=None, build=None, new=None):
3027-
# Find the root of the program
3028-
program = Program(getcwd(), False)
3029-
program.check_requirements(True)
3030-
# Save original working directory
3031-
orig_path = getcwd()
3032-
3033-
mbed_os_dir = program.get_os_dir()
3034-
if mbed_os_dir is None:
3035-
error("No Mbed OS directory found.")
3036-
unittests_dir = os.path.join(mbed_os_dir, "UNITTESTS")
3037-
3038-
tool = os.path.join(unittests_dir, "mbed_unittest.py")
3039-
3040-
# Prepare environment variables
3041-
env = program.get_env()
3042-
3043-
if os.path.exists(tool):
3044-
with cd(program.path):
3045-
# Setup the build path if not specified
3046-
build_path = build
3047-
if not build_path:
3048-
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, 'unittests')
3049-
3050-
# Run unit testing tools
3051-
popen([python_cmd, tool]
3052-
+ (["--compile"] if compile_only else [])
3053-
+ (["--run"] if run_only else [])
3054-
+ (["--clean"] if clean else [])
3055-
+ (["--debug"] if debug else [])
3056-
+ (["--coverage", coverage] if coverage else [])
3057-
+ (["--make-program", make_program] if make_program else [])
3058-
+ (["--generator", generator] if generator else [])
3059-
+ (["--regex", regex] if regex else [])
3060-
+ ["--build", build_path]
3061-
+ (["--new", new] if new else [])
3062-
+ (["--verbose"] if verbose else [])
3063-
+ remainder,
3064-
env=env)
3065-
else:
3066-
warning("Unit testing is not supported with this Mbed OS version.")
3067-
30683056

30693057
def main():
30703058
global verbose, very_verbose, remainder, cwd_root

0 commit comments

Comments
 (0)