<HTML><BODY><div>Hi, Sergey!</div><div>Thanks for the patch!</div><div>Please consider a few comments below.</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div> <blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div id=""><div class="js-helper js-readmsg-msg"><div><div id="style_16844429431933668568_BODY">This header contains generic init and close for tests and helpers for<br>loading auxiliary Lua script with functions to run inside a C test.<br><br>It will be properly used in the next commit.<br><br>Part of tarantool/tarantool#7900<br>---<br> test/tarantool-c-tests/utils.h | 63 ++++++++++++++++++++++++++++++++++<br> 1 file changed, 63 insertions(+)<br> create mode 100644 test/tarantool-c-tests/utils.h<br><br>diff --git a/test/tarantool-c-tests/utils.h b/test/tarantool-c-tests/utils.h<br>new file mode 100644<br>index 00000000..cf668006<br>--- /dev/null<br>+++ b/test/tarantool-c-tests/utils.h<br>@@ -0,0 +1,63 @@<br>+#include <limits.h><br>+#include <string.h><br>+<br>+#include "lauxlib.h"<br>+#include "lua.h"<br>+#include "luajit.h"<br>+#include "lualib.h"<br>+<br>+#include "test.h"<br>+<br>+/* Generic init for our tests. */<br>+static lua_State *utils_lua_init(void)<br>+{<br>+ lua_State *L = luaL_newstate();<br>+ if (!L)<br>+ bail_out("Can't init Lua state");<br>+ /* Stop collector during library initialization. */<br>+ lua_gc(L, LUA_GCSTOP, 0);<br>+ luaL_openlibs(L);<br>+ lua_gc(L, LUA_GCRESTART, -1);<br>+ return L;<br>+}<br>+<br>+/* Generic close for our tests. */<br>+static void utils_lua_close(lua_State *L)<br>+{<br>+ lua_close(L);<br>+}</div></div></div></div></blockquote><div>Do we __really__ need this function?)</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>+<br>+/*<br>+ * Load the pair to the test file <filename-script.lua>.<br>+ * Each file should return the table with functions (named the<br>+ * same as the corresponding unit tests) to call in unit tests.<br>+ * Push the table with those functions on the Lua stack.<br>+ */<br>+#define utils_load_aux_script(L) do { \<br>+ /* \<br>+ * Format script name. \<br>+ * `__ABS_FILENAME__` is set via CMake. \<br>+ */ \<br>+ char script[PATH_MAX] = __ABS_FILENAME__; \<br>+ char *file_suffix = strstr(script, ".test.c"); \</div></div></div></div></blockquote><div>The `strstr` function is not safe, use `strnstr` instead.</div><div>Also, maybe it is better to use the `strrstr` variation,</div><div>because it searches from the end. It eleminates issues</div><div>in case of a file with .test.c somewhere else in the name.</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>+ strcpy(file_suffix, "-script.lua"); \</div></div></div></div></blockquote><div>Same applies here.</div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div><div class="js-helper js-readmsg-msg"><div><div>+ \<br>+ if (luaL_dofile((L), script) != LUA_OK) { \<br>+ test_comment("Can't load %s: '%s'", script, \<br>+ lua_tostring((L), -1)); \<br>+ bail_out("Can't load auxiliary script"); \<br>+ } \<br>+ \<br>+ if (!lua_istable((L), -1)) \<br>+ bail_out("Returned value from script is not a table"); \<br>+} while (0)<br>+<br>+/*<br>+ * Accept a table on top of the Lua stack which containing the<br>+ * function named as the unit test we currently executing.<br>+ */<br>+#define utils_get_aux_lfunc(L) do { \<br>+ lua_getfield((L), -1, __func__); \<br>+ if (!lua_isfunction((L), -1)) \<br>+ bail_out("Can't get auxiliary test function"); \<br>+} while (0)<br>--<br>2.34.1</div></div></div></div></blockquote><div> </div><div><div>--<br>Best regards,</div><div>Maxim Kokryashkin</div></div><div> </div></div></blockquote></BODY></HTML>