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

Sergey Kaplun skaplun at tarantool.org
Sat Jun 10 11:03:11 MSK 2023


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      | 26 +++++++++
 test/tarantool-c-tests/utils.h        | 79 +++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 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
index 04ddb729..462534be 100644
--- a/test/tarantool-c-tests/README.md
+++ b/test/tarantool-c-tests/README.md
@@ -44,3 +44,29 @@ earlier:
 * `skip_all("reason")` -- skip the current group of tests.
 * `todo("reason")` -- skip the current test marking as TODO.
 * `bail_out("reason")` -- exit the entire process due to some emergency.
+
+## Testing with Lua source code
+
+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..49a5832d
--- /dev/null
+++ b/test/tarantool-c-tests/utils.h
@@ -0,0 +1,79 @@
+#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");
+	/*
+	 * Don't really need to waste time on the GC during
+	 * library initialization, so stop the collector.
+	 * Same approach as in `pmain()` in <src/luajit.c>.
+	 */
+	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 Lua <file> -- the pair to the C test file.
+ * 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 */
-- 
2.34.1



More information about the Tarantool-patches mailing list