Tarantool development patches archive
 help / color / mirror / Atom feed
From: Evgeniy Temirgaleev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Kaplun <skaplun@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v4 1/2] test: refactor utils.exec.makecmd
Date: Tue, 24 Nov 2026 17:22:25 +0300	[thread overview]
Message-ID: <48dc0df395bb6c6b22924cf95bbf45dc70a4a09c.1795529449.git.e.temirgaleev@tarantool.org> (raw)
In-Reply-To: <cover.1795529449.git.e.temirgaleev@tarantool.org>

From: Temir Galeev <temir.galeev@bk.ru>

The 'utils.exec.makecmd()' was refactored. The common
'utils.exec.makecmd_exec()' added to allow executing arbitrary
commands and the 'makecmd()' is implemented on top of it.
The command object's 'LUABIN' field renamed to 'BIN', the
'SCRIPT' field with script filename replaced with 'PERMARGS'
field, containing all command arguments, which are passed on
every call, and the 'trace' callback option is added for
additional processing of the generated command and it
output.

Also added the 'makecmd_tap_trace()' and 'test_data()' helpers.

Test [1] was refactored to use 'makecmd()' as usual.
Test [2] was refactored to use common 'makecmd_exec()'.

1. test/tarantool-tests/lj-962-stack-overflow-report.test.lua
2. test/tarantool-tests/lj-586-debug-non-string-error.test.lua

Initially the patch for exec utils was prepared for tests in
the follow up 'Improve CLI signal handling on POSIX' patch.
After it's test simplification, the shell script helper was
wiped out, but the refactoring was already done.
---
 .../lj-586-debug-non-string-error.test.lua    | 34 ++++-----
 .../commands.lua                              |  2 +
 .../lj-962-stack-overflow-report.test.lua     |  6 +-
 test/tarantool-tests/utils/exec.lua           | 72 +++++++++++++++----
 4 files changed, 81 insertions(+), 33 deletions(-)
 create mode 100644 test/tarantool-tests/lj-586-debug-non-string-error/commands.lua

diff --git a/test/tarantool-tests/lj-586-debug-non-string-error.test.lua b/test/tarantool-tests/lj-586-debug-non-string-error.test.lua
index 67869708..fe2b4b87 100644
--- a/test/tarantool-tests/lj-586-debug-non-string-error.test.lua
+++ b/test/tarantool-tests/lj-586-debug-non-string-error.test.lua
@@ -6,20 +6,18 @@ test:plan(1)
 
 -- XXX: This is quite an exotic test case in that point of view
 -- that testing the debug interactive interface always ends with
--- sending commands to another instance via stdin. However, the
--- module with test helpers lacks the suitable routine.
--- `utils.makecmd()` doesn't fit for this, since `debug.debug()`
--- captures `io.stdin` and waits at `fgets()` in debug busy loop.
--- As it's already mentioned, such tests are not usual, so there
--- is no need to introduce a new helper to utils module (at least
--- for now). Hence, we use `utils.luacmd()` function to obtain the
--- interpreter command, run `debug.debug()` with it, send two
--- commands via pipeline to the launched process, capture its
--- stdout and stderr and test the resulting output against the
--- expected one.
+-- sending commands to another instance via stdin.
+-- Since `debug.debug()` captures `io.stdin` and waits at
+-- `fgets()` in debug busy loop, we use `utils.exec.luacmd()`
+-- function to obtain the interpreter command, run
+-- `debug.debug()` with it, send two commands from the
+-- `utils.exec.test_data('commands.lua')` file redirecting it to
+-- the process stdin, capture its stdout and stderr and test the
+-- resulting output against the expected one.
 --
 -- Debugger prompt.
 local ldb = 'lua_debug> '
+-- The magic number is passed via MAGIC environment variable.
 local magic = 42
 local luabin = utils.exec.luacmd(arg)
 local stderr = {
@@ -37,11 +35,13 @@ local stderr = {
 -- errors are yielded before the magic given to `print()` command
 local expected = table.concat(stderr)..magic
 
-local cmd = ([[cat <<EOF | %s -e 'debug.debug()' 2>&1
-error({})
-print(%d)
-EOF]]):format(luabin, magic)
-
-local res = io.popen(cmd):read('*all'):gsub('%s+$', '')
+local dbg_commands_file = utils.exec.test_data(arg, 'commands.lua')
+local command = utils.exec.makecmd_exec(luabin, {
+  env = {MAGIC = magic},
+  redirect = ('2>&1 <%s'):format(dbg_commands_file),
+  args = {'-e', "'debug.debug()'"},
+  trace = utils.exec.makecmd_tap_trace(test),
+})
+local res = command()
 test:ok(res == expected, 'handle non-string error in debug.debug()')
 test:done(true)
diff --git a/test/tarantool-tests/lj-586-debug-non-string-error/commands.lua b/test/tarantool-tests/lj-586-debug-non-string-error/commands.lua
new file mode 100644
index 00000000..a27aaee7
--- /dev/null
+++ b/test/tarantool-tests/lj-586-debug-non-string-error/commands.lua
@@ -0,0 +1,2 @@
+error({})
+print(tonumber(os.getenv('MAGIC')))
diff --git a/test/tarantool-tests/lj-962-stack-overflow-report.test.lua b/test/tarantool-tests/lj-962-stack-overflow-report.test.lua
index 848f4ead..14ffea10 100644
--- a/test/tarantool-tests/lj-962-stack-overflow-report.test.lua
+++ b/test/tarantool-tests/lj-962-stack-overflow-report.test.lua
@@ -4,8 +4,8 @@ local tap = require('tap')
 local test = tap.test('lj-962-stack-overflow-report')
 test:plan(1)
 
-local LUABIN = require('utils').exec.luabin(arg)
-local SCRIPT = arg[0]:gsub('%.test%.lua$', '/script.lua')
-local output = io.popen(LUABIN .. ' 2>&1 ' .. SCRIPT, 'r'):read('*all')
+local utils = require('utils')
+local script = utils.exec.makecmd(arg, {redirect = '2>&1'})
+local output = script()
 test:like(output, 'stack overflow', 'stack overflow reported correctly')
 test:done(true)
diff --git a/test/tarantool-tests/utils/exec.lua b/test/tarantool-tests/utils/exec.lua
index 49f8ef06..1b79bd63 100644
--- a/test/tarantool-tests/utils/exec.lua
+++ b/test/tarantool-tests/utils/exec.lua
@@ -22,6 +22,7 @@ end
 
 local function makeenv(tabenv)
   if tabenv == nil then return '' end
+  assert(type(tabenv) == 'table')
   local flatenv = {}
   for var, value in pairs(tabenv) do
     if var == 'LD_PRELOAD' then
@@ -43,33 +44,78 @@ local function makeenv(tabenv)
   return table.concat(flatenv, ' ')
 end
 
--- <makecmd> creates a command that runs %testname%/script.lua by
--- <LUAJIT_TEST_BINARY> with the given environment, launch options
--- and CLI arguments. The function yields an object (i.e. table)
--- with the aforementioned parameters. To launch the command just
--- call the object.
-function M.makecmd(arg, opts)
+-- <makecmd_exec> creates a command that runs given binary
+-- with the given environment, launch options and zero or
+-- more permanent arguments.
+-- The function yields an object (i.e. table) with the
+-- aforementioned parameters. To launch the command just call
+-- the object. Additional arguments which may vary from call
+-- to call are the `__call` args.
+-- Pass opts.trace function(event, value) for additional
+-- command/output processing.
+function M.makecmd_exec(bin, opts)
+  assert(type(bin) == 'string')
+  opts = opts or {}
+  assert(type(opts) == 'table')
+  local redirect = opts.redirect or ''
+  assert(type(redirect) == 'string')
+  local perm_args = opts.args or {}
+  assert(type(perm_args) == 'table')
+  assert(opts.trace == nil or type(opts.trace) == 'function')
   return setmetatable({
-    LUABIN = M.luacmd(arg),
-    SCRIPT = opts and opts.script or arg[0]:gsub('%.test%.lua$', '/script.lua'),
-    ENV = opts and makeenv(opts.env) or '',
-    REDIRECT = opts and opts.redirect or '',
+    BIN = bin,
+    ENV = makeenv(opts.env),
+    REDIRECT = redirect,
+    PERMARGS = table.concat(perm_args, ' '),
+    trace = opts.trace,
   }, {
     __call = function(self, ...)
       -- This line just makes the command for <io.popen> by the
       -- following steps:
       -- 1. Replace the placeholders with the corresponding values
-      --    given to the command constructor (e.g. script, env).
+      --    given to the command constructor (e.g. bin, env).
       -- 2. Join all CLI arguments given to the __call metamethod.
       -- 3. Concatenate the results of step 1 and step 2 to obtain
       --    the resulting command.
-      local cmd = ('<ENV> <LUABIN> <REDIRECT> <SCRIPT>'):gsub('%<(%w+)>', self)
+      local cmd = ('<ENV> <BIN> <REDIRECT> <PERMARGS>'):gsub('%<(%w+)>', self)
                   .. (' %s'):rep(select('#', ...)):format(...)
+      if self.trace ~= nil then self.trace('cmd', cmd) end
       -- Trim both leading and trailing whitespace from the output
       -- produced by the child process.
-      return io.popen(cmd):read('*all'):gsub('^%s+', ''):gsub('%s+$', '')
+      local output = io.popen(cmd):
+        read('*all'):gsub('^%s+', ''):gsub('%s+$', '')
+      if self.trace ~= nil then self.trace('output', output) end
+      return output
     end
   })
 end
 
+-- Make a trace function for `makecmd_exec` script, which produces
+-- tap test diagnostic messages with the command and it's output.
+function M.makecmd_tap_trace(tap)
+  return function(ev, val) tap:diag('%s: %s', ev, val) end
+end
+
+-- Makes a file name for running test's data file in
+-- '%testname%' directory.
+function M.test_data(arg, name)
+  local fname, matches = arg[0]:gsub('%.test%.lua$', '/'..name)
+  assert(matches == 1, 'unexpected test filename')
+  return fname
+end
+
+-- <makecmd> creates a command that runs %testname%/script.lua by
+-- <LUAJIT_TEST_BINARY> with the given environment, launch options
+-- and CLI arguments.
+function M.makecmd(arg, opts)
+  local luabin = M.luacmd(arg)
+  local script = opts and opts.script or M.test_data(arg, 'script.lua')
+  return M.makecmd_exec(luabin, {
+    env = opts and opts.env or nil,
+    redirect = opts and opts.redirect or nil,
+    args = {script},
+    trace = opts and opts.trace or nil,
+  })
+end
+
 return M
-- 
2.49.0


  reply	other threads:[~2026-11-24 14:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:22 [Tarantool-patches] [PATCH luajit v4 0/2] Improve CLI signal handling on POSIX Evgeniy Temirgaleev via Tarantool-patches
2026-11-24 14:22 ` Evgeniy Temirgaleev via Tarantool-patches [this message]
2026-11-25 10:41   ` [Tarantool-patches] [PATCH luajit v4 1/2] test: refactor utils.exec.makecmd Sergey Bronnikov via Tarantool-patches
2026-11-24 14:22 ` [Tarantool-patches] [PATCH luajit v4 2/2] Improve CLI signal handling on POSIX Evgeniy Temirgaleev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48dc0df395bb6c6b22924cf95bbf45dc70a4a09c.1795529449.git.e.temirgaleev@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=e.temirgaleev@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v4 1/2] test: refactor utils.exec.makecmd' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox