test Refactor unit test scripts. (#2473)

* Refactor unit test scripts.

Does the following:

1. Remove as many dependencies on the operating system shell as possible.
   For example, use of shutil.rmtree(...) instead of os.system('rm -r ...').
   This brings this script a bit closer to being able to run on Windows.
2. Switch from os.system() to subprocess.check_call().
   * This is a bit more secure as check_call() directly invokes the subprocess
     without evaluation the arguments on a command-line.
   * Removes the need to evaluate the return code as check_call() does this.
   * Can directly set environment variables (e.g. env=cmd_env) instead of
     including with subprocess invocation (e.g. BIN=test.bin).
3. Minor cleanup to main.py sys.argv parsing.
4. PEP8 formatting.

* Ignore FileNotFoundError for rmtree('report').

* Back to os.system for gcovr.

* Removed unused shutil import.
This commit is contained in:
Chris Mumford
2021-08-25 06:37:59 -07:00
committed by GitHub
parent 98b9ce5997
commit 53986b4b0e
3 changed files with 128 additions and 108 deletions

View File

@@ -2,86 +2,104 @@
import os
import re
import subprocess
lvgldirname = os.path.abspath('..')
lvgldirname = os.path.basename(lvgldirname)
lvgldirname = '"' + lvgldirname + '"'
test_dir = os.path.dirname(os.path.realpath(__file__))
lvgl_dir = os.path.abspath(os.path.join(test_dir, os.pardir))
lvgl_dir_name = 'lvgl' # LVGL subdirectory (base name) in lvgl_dir.
lvgl_parent_dir = os.path.abspath(os.path.join(lvgl_dir, os.pardir))
lv_conf_path = os.path.join(lvgl_dir, 'tests/src/lv_test_conf.h')
base_defines = ['-DLV_CONF_PATH="%s"' % lv_conf_path, '-DLV_BUILD_TEST']
def maybe_quote(obj):
if type(obj) == str:
return '"%s"' % obj
return obj
def zip_defines(defines):
return ['-D%s=%s' % (key, maybe_quote(defines[key])) for key in defines]
base_defines = '"-DLV_CONF_PATH=' + lvgldirname +'/tests/src/lv_test_conf.h -DLV_BUILD_TEST"'
def build(defines):
global base_defines
optimization = '"-O3 -g0"'
d_all = base_defines[:-1] + " ";
for d in defines:
d_all += " -D" + d + "=" + str(defines[d])
d_all += '"'
# -s makes it silence
cmd = "make -s -j BIN=test.bin " + "MAINSRC=src/lv_test_main.c LVGL_DIR_NAME=" + lvgldirname + " DEFINES=" + d_all + " OPTIMIZATION=" + optimization
global base_defines
optimization = ['-O3', '-g0']
d_all = base_defines + zip_defines(defines)
print("")
print("Build")
print("-----------------------", flush=True)
# print(cmd)
ret = os.system(cmd)
if(ret != 0):
print("BUILD ERROR! (error code " + str(ret) + ")")
exit(1)
cmd_env = os.environ.copy()
cmd_env['BIN'] = 'test.bin'
cmd_env['MAINSRC'] = 'src/lv_test_main.c'
cmd_env['LVGL_DIR'] = lvgl_parent_dir
cmd_env['LVGL_DIR_NAME'] = lvgl_dir_name
cmd_env['DEFINES'] = ' '.join(d_all)
cmd_env['OPTIMIZATION'] = ' '.join(optimization)
print("")
print("Run")
print("-----------------------", flush=True)
ret = os.system("./test.bin")
if(ret != 0):
print("RUN ERROR! (error code " + str(ret) + ")", flush=True)
exit(1)
print("")
print("Build")
print("-----------------------", flush=True)
# -s makes it silence
subprocess.check_call(['make', '-s', '--jobs=%d' % os.cpu_count()], env=cmd_env)
print("")
print("Run")
print("-----------------------", flush=True)
subprocess.check_call("./test.bin")
def build_test(defines, test_name):
global base_defines
optimization = '"-g0"'
global base_defines
optimization = ['-g0']
print("")
print("")
print("~~~~~~~~~~~~~~~~~~~~~~~~")
print(re.search("/[a-z_]*$", test_name).group(0)[1:])
print("~~~~~~~~~~~~~~~~~~~~~~~~", flush=True)
d_all = base_defines[:-1] + " ";
for d in defines:
d_all += " -D" + d + "=" + str(defines[d])
d_all += '"'
test_file_name = test_name + ".c"
test_file_runner_name = test_name + "_Runner.c"
test_file_runner_name = test_file_runner_name.replace("/test_cases/", "/test_runners/")
csrcs = " EXTRA_CSRCS=\"unity/unity.c unity/unity_support.c src/test_fonts/font_1.c src/test_fonts/font_2.c src/test_fonts/font_3.c \" "
# -s makes it silence
cmd = "make -s -j BIN=test.bin MAINSRC=" + test_file_name + " TEST_SRC=" + test_file_runner_name + csrcs + " LVGL_DIR_NAME=" + lvgldirname + " DEFINES=" + d_all + " OPTIMIZATION=" + optimization
print("")
print("")
print("~~~~~~~~~~~~~~~~~~~~~~~~")
print(re.search("/[a-z_]*$", test_name).group(0)[1:])
print("~~~~~~~~~~~~~~~~~~~~~~~~", flush=True)
d_all = base_defines + zip_defines(defines)
test_file_name = test_name + ".c"
test_file_runner_name = test_name + "_Runner.c"
test_file_runner_name = test_file_runner_name.replace(
"/test_cases/", "/test_runners/")
cmd_env = os.environ.copy()
cmd_env['BIN'] = 'test.bin'
cmd_env['MAINSRC'] = test_file_name
cmd_env['TEST_SRC'] = test_file_runner_name
cmd_env['LVGL_DIR'] = lvgl_parent_dir
cmd_env['LVGL_DIR_NAME'] = lvgl_dir_name
cmd_env['DEFINES'] = ' '.join(d_all)
cmd_env['OPTIMIZATION'] = ' '.join(optimization)
extra_csrcs = [
'unity/unity.c', 'unity/unity_support.c',
'src/test_fonts/font_1.c', 'src/test_fonts/font_2.c',
'src/test_fonts/font_3.c'
]
cmd_env['EXTRA_CSRCS'] = ' '.join(extra_csrcs)
print("")
print("Build")
print("-----------------------", flush=True)
# -s makes it silence
subprocess.check_call(['make', '-s', '--jobs=%d' % os.cpu_count()], env=cmd_env)
print("")
print("Run")
print("-----------------------")
subprocess.check_call('./test.bin')
print("")
print("Build")
print("-----------------------", flush=True)
# print(cmd)
ret = os.system(cmd)
if(ret != 0):
print("BUILD ERROR! (error code " + str(ret) + ")", flush=True)
exit(1)
print("")
print("Run")
print("-----------------------")
ret = os.system("./test.bin")
if(ret != 0):
print("RUN ERROR! (error code " + str(ret) + ")", flush=True)
exit(1)
def clean():
print("")
print("Clean")
print("-----------------------", flush=True)
os.system("make clean LVGL_DIR_NAME=" + lvgldirname)
os.system("rm -f ./test.bin")
print("")
print("Clean")
print("-----------------------", flush=True)
cmd_env = os.environ.copy()
cmd_env['LVGL_DIR'] = lvgl_parent_dir
cmd_env['LVGL_DIR_NAME'] = lvgl_dir_name
subprocess.check_call(['make', 'clean'], env=cmd_env)
try:
os.remove('test.bin')
except FileNotFoundError:
pass