[Tarantool-patches] [PATCH v3 luajit 3/6] test: introduce utils.h helper for C tests

Sergey Bronnikov sergeyb at tarantool.org
Mon Jun 5 18:11:44 MSK 2023


Sergey,


thanks for patches and fixes.


LGTM with a couple of minor comments below


On 6/5/23 13:41, Sergey Kaplun wrote:
> This header contains generic init and close functions for tests and
> helpers for loading auxiliary Lua script with functions to run inside a
> C test.  See more information in the <README.md> file.
>
> It will be properly used in the next commit.
>
> Part of tarantool/tarantool#7900
> ---
>   test/tarantool-c-tests/CMakeLists.txt |  3 ++
>   test/tarantool-c-tests/README.md      | 36 +++++++++++++
>   test/tarantool-c-tests/utils.h        | 75 +++++++++++++++++++++++++++
>   3 files changed, 114 insertions(+)
>   create mode 100644 test/tarantool-c-tests/README.md
>   create mode 100644 test/tarantool-c-tests/utils.h
>
> diff --git a/test/tarantool-c-tests/CMakeLists.txt b/test/tarantool-c-tests/CMakeLists.txt
> index da128457..17255345 100644
> --- a/test/tarantool-c-tests/CMakeLists.txt
> +++ b/test/tarantool-c-tests/CMakeLists.txt
> @@ -26,6 +26,9 @@ set_target_properties(libtest PROPERTIES
>   # lj_arch.h in compiled test are consistent with the LuaJIT library
>   # to link.
>   AppendFlags(TESTS_C_FLAGS ${TARGET_C_FLAGS})
> +# Use directory for absolute path to the Lua script helper. So,
> +# test binary can be run from any directory.
> +AppendFlags(TESTS_C_FLAGS "-D__LJ_TEST_DIR__='\"${CMAKE_CURRENT_SOURCE_DIR}\"'")
>   
>   set(CTEST_SRC_SUFFIX ".test.c")
>   file(GLOB tests "${CMAKE_CURRENT_SOURCE_DIR}/*${CTEST_SRC_SUFFIX}")
> diff --git a/test/tarantool-c-tests/README.md b/test/tarantool-c-tests/README.md
> new file mode 100644
> index 00000000..102ec151
> --- /dev/null
> +++ b/test/tarantool-c-tests/README.md
> @@ -0,0 +1,36 @@
> +This directory contains C tests for Tarantool's fork of LuaJIT.
> +
> +They should test C API, some unit functionality, etc..
> +
> +The group of unit tests is declared like the following:
> +```c
> +void *t_state = NULL;
> +const struct test_unit tgroup[] = {
> +      test_unit_new(test_base),
> +      test_unit_new(test_simple),
> +};
> +return test_run_group(tgroup, t_state);
> +```
> +
> +Sometimes we need to test C API for modules, that show some Lua metrics (like
> +`luaM_metrics` or sysprof). In these cases, the required Lua script should be
> +named like the following: `<ctestname-script.lua>` and contains a table with a
> +bunch of Lua functions named the same as the unit C test, that uses this
> +function.
> +
> +```lua
> +local M = {}
> +M.base = function()
> +  -- ...
> +end
> +
> +M.test_simple = function()
> +  -- ...
> +end
> +
> +return M
> +```
> +
> +The script is loaded via `utils_load_aux_script(L, script_name)`. It loads the file
> +and place the table with functions at the top of Lua stack. Each function is get
> +from the table via `utils_get_aux_lfunc(L)` helper in the corresponding test.
> diff --git a/test/tarantool-c-tests/utils.h b/test/tarantool-c-tests/utils.h
> new file mode 100644
> index 00000000..714aa61a
> --- /dev/null
> +++ b/test/tarantool-c-tests/utils.h
> @@ -0,0 +1,75 @@
> +#ifndef TARANTOOL_LUAJIT_TEST_UTILS_H
> +#define TARANTOOL_LUAJIT_TEST_UTILS_H
> +
> +#include <limits.h>
> +#include <string.h>
> +
> +#include "lauxlib.h"
> +#include "lua.h"
> +#include "luajit.h"
> +#include "lualib.h"
> +
> +#include "test.h"
> +
> +#define UTILS_UNUSED __attribute__((unused))
> +
> +/* Generic init for our tests. */
> +static lua_State *utils_lua_init(void)
> +{
> +	lua_State *L = luaL_newstate();
> +	if (!L)
> +		bail_out("Can't init Lua state");
> +	/* Stop collector during library initialization. */

Message would be more helpful with explanation *why* GC should be stopped.


> +	lua_gc(L, LUA_GCSTOP, 0);
> +	luaL_openlibs(L);
> +	lua_gc(L, LUA_GCRESTART, -1);
> +	return L;
> +}
> +
> +/* Generic close for our tests. */
> +static void utils_lua_close(lua_State *L)
> +{
> +	lua_close(L);
> +}
> +
> +/*
> + * Load the pair to the test file <filename-script.lua>.

Nit: replace filename-script with file (same as in argument name, this 
allows easily match them

on reading

> + * Each file should return the table with functions (named the
> + * same as the corresponding unit tests) to call in unit tests.
> + * Script file name is given as the second argument.
> + * Push the table with those functions on the Lua stack.
> + */
> +UTILS_UNUSED static void utils_load_aux_script(lua_State *L, const char *file)
> +{
> +	/*
> +	 * Format script name.
> +	 * `__LJ_TEST_DIR__` is set via CMake.
> +	 */
> +	char script[PATH_MAX] = __LJ_TEST_DIR__;
> +	char *script_name = script + sizeof(__LJ_TEST_DIR__) - 1;
> +	/* Replace '\0' with '/'. */
> +	*script_name++ = '/';
> +	/* Append script filename. */
> +	strcpy(script_name, file);
> +
> +	if (luaL_dofile(L, script) != LUA_OK) {
> +		test_comment("Can't load %s: '%s'", script,
> +			     lua_tostring(L, -1));
> +		bail_out("Can't load auxiliary script");
> +	}
> +
> +	if (!lua_istable(L, -1))
> +		bail_out("Returned value from script is not a table");
> +}
> +
> +/*
> + * Accept a table on top of the Lua stack which containing the
> + * function named as the unit test we currently executing.
> + */
> +#define utils_get_aux_lfunc(L) do {					\
> +	lua_getfield((L), -1, __func__);				\
> +	if (!lua_isfunction((L), -1))					\
> +		bail_out("Can't get auxiliary test function");		\
> +} while (0)
> +
> +#endif /* TARANTOOL_LUAJIT_TEST_UTILS_H */


More information about the Tarantool-patches mailing list