From e369c16e0d585cdcf483e2cc43637d312c0e6b51 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 5 Dec 2022 17:30:26 +0400 Subject: [PATCH] Fix checks in out-of-root builds (#132) * Fix Python tests * Fix cppcheck --- ChangeLog | 2 ++ make/checks.am | 4 ++-- tests/Makefile.am | 12 +++++----- tests/cmdline_gen.py | 44 +++++++++++++++++++--------------- tests/printf_fmt_gen.py | 44 +++++++++++++++++++--------------- tests/printf_gen.py | 52 ++++++++++++++++++++++++----------------- 6 files changed, 90 insertions(+), 68 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3120200d..b053ec4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ 2022-12-05 Alex Kotov * configure.ac: Fix a bug with building for a freestanding environment + * tests/Makefile.am: Fix Python tests in out-of-root builds + * make/checks.am: Fix cppcheck in out-of-root builds 2022-12-04 Alex Kotov diff --git a/make/checks.am b/make/checks.am index 0417a3b6..6268fd21 100644 --- a/make/checks.am +++ b/make/checks.am @@ -20,8 +20,8 @@ CPPCHECK_INC = \ -I$(top_srcdir)/include CPPCHECK_SUPPRESS = \ - --suppress='unusedStructMember:examples/multiboot2_header_macro.c' \ - --suppress='unusedStructMember:tests/test_multiboot2_info_*.c' + --suppress='unusedStructMember:$(top_srcdir)/examples/multiboot2_header_macro.c' \ + --suppress='unusedStructMember:$(top_srcdir)/tests/test_multiboot2_info_*.c' CPPCHECK_PATHS = \ $(top_builddir)/examples \ diff --git a/tests/Makefile.am b/tests/Makefile.am index aa54c1b5..d0272010 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -103,8 +103,8 @@ endif CLEANFILES += test_cmdline_gen.c -test_cmdline_gen.c: cmdline_gen.py cmdline_gen.jinja $(top_srcdir)/common/cmdline.yml - $(PYTHON) cmdline_gen.py +test_cmdline_gen.c: $(top_srcdir)/tests/cmdline_gen.py $(top_srcdir)/tests/cmdline_gen.jinja $(top_srcdir)/common/cmdline.yml + $(PYTHON) $+ $@ ############ # test_elf # @@ -337,8 +337,8 @@ endif CLEANFILES += test_printf_fmt_gen.c -test_printf_fmt_gen.c: printf_fmt_gen.py printf_fmt_gen.jinja $(top_srcdir)/common/printf_fmt.yml - $(PYTHON) printf_fmt_gen.py +test_printf_fmt_gen.c: $(top_srcdir)/tests/printf_fmt_gen.py $(top_srcdir)/tests/printf_fmt_gen.jinja $(top_srcdir)/common/printf_fmt.yml + $(PYTHON) $+ $@ ################### # test_printf_gen # @@ -360,8 +360,8 @@ endif CLEANFILES += test_printf_gen.c -test_printf_gen.c: printf_gen.py printf_gen.jinja $(top_srcdir)/common/printf.yml $(top_srcdir)/common/printf_orig.yml - $(PYTHON) printf_gen.py +test_printf_gen.c: $(top_srcdir)/tests/printf_gen.py $(top_srcdir)/tests/printf_gen.jinja $(top_srcdir)/common/printf.yml $(top_srcdir)/common/printf_orig.yml + $(PYTHON) $+ $@ #################### # test_units_human # diff --git a/tests/cmdline_gen.py b/tests/cmdline_gen.py index f74d104f..32883dca 100644 --- a/tests/cmdline_gen.py +++ b/tests/cmdline_gen.py @@ -1,27 +1,16 @@ from jinja2 import Environment, FileSystemLoader from os import path -from yaml import SafeLoader, safe_load +from sys import argv +from yaml import safe_load -CASES_FILENAME = 'cmdline.yml' -TEMPLATE_FILENAME = 'cmdline_gen.jinja' -TEST_FILENAME = 'test_cmdline_gen.c' - -ROOT_DIRPATH = path.dirname(path.dirname(path.join(path.abspath(__file__)))) - -COMMON_DIRPATH = path.join(ROOT_DIRPATH, 'common') -TESTS_DIRPATH = path.join(ROOT_DIRPATH, 'tests') - -CASES_FILEPATH = path.join(COMMON_DIRPATH, CASES_FILENAME) -TEST_FILEPATH = path.join(TESTS_DIRPATH, TEST_FILENAME) - -def main(): - cases = safe_load(open(CASES_FILEPATH)) +def main(test_filepath, template_filepath, cases_filepath): + cases = safe_load(open(cases_filepath)) jinja_env = Environment( keep_trailing_newline=True, - loader=FileSystemLoader(TESTS_DIRPATH), + loader=FileSystemLoader(path.dirname(template_filepath)), ) - jinja_template = jinja_env.get_template(TEMPLATE_FILENAME) + jinja_template = jinja_env.get_template(path.basename(template_filepath)) result = jinja_template.render( cases=cases, @@ -30,7 +19,7 @@ def main(): len=len, ) - with open(TEST_FILEPATH, 'w') as f: + with open(test_filepath, 'w') as f: f.write(result) def escape_int(n): @@ -40,4 +29,21 @@ def escape_str(s): return '"' + s + '"' if __name__ == '__main__': - main() + print(argv) + + template_filepath = argv[1] + cases_filepath = argv[2] + test_filepath = argv[3] + + print('test_filepath: %s' % test_filepath) + print('template_filepath: %s' % template_filepath) + print('cases_filepath: %s' % cases_filepath) + + if path.exists(test_filepath) and not path.isfile(test_filepath): + raise RuntimeError('invalid test file path') + if not path.isfile(template_filepath): + raise RuntimeError('invalid template file path') + if not path.isfile(cases_filepath): + raise RuntimeError('invalid cases file path') + + main(test_filepath, template_filepath, cases_filepath) diff --git a/tests/printf_fmt_gen.py b/tests/printf_fmt_gen.py index f6d092ce..96a0925e 100644 --- a/tests/printf_fmt_gen.py +++ b/tests/printf_fmt_gen.py @@ -1,27 +1,16 @@ from jinja2 import Environment, FileSystemLoader from os import path -from yaml import SafeLoader, safe_load +from sys import argv +from yaml import safe_load -CASES_FILENAME = 'printf_fmt.yml' -TEMPLATE_FILENAME = 'printf_fmt_gen.jinja' -TEST_FILENAME = 'test_printf_fmt_gen.c' - -ROOT_DIRPATH = path.dirname(path.dirname(path.join(path.abspath(__file__)))) - -COMMON_DIRPATH = path.join(ROOT_DIRPATH, 'common') -TESTS_DIRPATH = path.join(ROOT_DIRPATH, 'tests') - -CASES_FILEPATH = path.join(COMMON_DIRPATH, CASES_FILENAME) -TEST_FILEPATH = path.join(TESTS_DIRPATH, TEST_FILENAME) - -def main(): - cases = safe_load(open(CASES_FILEPATH)) +def main(test_filepath, template_filepath, cases_filepath): + cases = safe_load(open(cases_filepath)) jinja_env = Environment( keep_trailing_newline=True, - loader=FileSystemLoader(TESTS_DIRPATH), + loader=FileSystemLoader(path.dirname(template_filepath)), ) - jinja_template = jinja_env.get_template(TEMPLATE_FILENAME) + jinja_template = jinja_env.get_template(path.basename(template_filepath)) result = jinja_template.render( cases=cases, @@ -31,7 +20,7 @@ def main(): none_to_zero=none_to_zero, ) - with open(TEST_FILEPATH, 'w') as f: + with open(test_filepath, 'w') as f: f.write(result) def escape_flag(flag): @@ -56,4 +45,21 @@ def none_to_zero(num): return str(num) if __name__ == '__main__': - main() + print(argv) + + template_filepath = argv[1] + cases_filepath = argv[2] + test_filepath = argv[3] + + print('test_filepath: %s' % test_filepath) + print('template_filepath: %s' % template_filepath) + print('cases_filepath: %s' % cases_filepath) + + if path.exists(test_filepath) and not path.isfile(test_filepath): + raise RuntimeError('invalid test file path') + if not path.isfile(template_filepath): + raise RuntimeError('invalid template file path') + if not path.isfile(cases_filepath): + raise RuntimeError('invalid cases file path') + + main(test_filepath, template_filepath, cases_filepath) diff --git a/tests/printf_gen.py b/tests/printf_gen.py index bc322a1f..2841a14b 100644 --- a/tests/printf_gen.py +++ b/tests/printf_gen.py @@ -1,32 +1,19 @@ from jinja2 import Environment, FileSystemLoader from os import path -from yaml import SafeLoader, safe_load +from sys import argv +from yaml import safe_load -CASES_ORIG_FILENAME = 'printf_orig.yml' -CASES_REG_FILENAME = 'printf.yml' -TEMPLATE_FILENAME = 'printf_gen.jinja' -TEST_FILENAME = 'test_printf_gen.c' - -ROOT_DIRPATH = path.dirname(path.dirname(path.join(path.abspath(__file__)))) - -COMMON_DIRPATH = path.join(ROOT_DIRPATH, 'common') -TESTS_DIRPATH = path.join(ROOT_DIRPATH, 'tests') - -CASES_ORIG_FILEPATH = path.join(COMMON_DIRPATH, CASES_ORIG_FILENAME) -CASES_REG_FILEPATH = path.join(COMMON_DIRPATH, CASES_REG_FILENAME) -TEST_FILEPATH = path.join(TESTS_DIRPATH, TEST_FILENAME) - -def main(): - cases_orig = safe_load(open(CASES_ORIG_FILEPATH)) - cases_reg = safe_load(open(CASES_REG_FILEPATH)) +def main(test_filepath, template_filepath, cases_reg_filepath, cases_orig_filepath): + cases_reg = safe_load(open(cases_reg_filepath)) + cases_orig = safe_load(open(cases_orig_filepath)) cases = cases_reg + cases_orig jinja_env = Environment( keep_trailing_newline=True, - loader=FileSystemLoader(TESTS_DIRPATH), + loader=FileSystemLoader(path.dirname(template_filepath)), ) - jinja_template = jinja_env.get_template(TEMPLATE_FILENAME) + jinja_template = jinja_env.get_template(path.basename(template_filepath)) result = jinja_template.render( cases=cases, @@ -35,7 +22,7 @@ def main(): values=values, ) - with open(TEST_FILEPATH, 'w') as f: + with open(test_filepath, 'w') as f: f.write(result) def escape_char(c): @@ -77,4 +64,25 @@ def values(args): return values if __name__ == '__main__': - main() + print(argv) + + template_filepath = argv[1] + cases_reg_filepath = argv[2] + cases_orig_filepath = argv[3] + test_filepath = argv[4] + + print('test_filepath: %s' % test_filepath) + print('template_filepath: %s' % template_filepath) + print('cases_reg_filepath: %s' % cases_reg_filepath) + print('cases_orig_filepath: %s' % cases_orig_filepath) + + if path.exists(test_filepath) and not path.isfile(test_filepath): + raise RuntimeError('invalid test file path') + if not path.isfile(template_filepath): + raise RuntimeError('invalid template file path') + if not path.isfile(cases_reg_filepath): + raise RuntimeError('invalid regular cases file path') + if not path.isfile(cases_orig_filepath): + raise RuntimeError('invalid original cases file path') + + main(test_filepath, template_filepath, cases_reg_filepath, cases_orig_filepath)