Tarantool development patches archive
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@gmail.com>
To: tml <tarantool-patches@dev.tarantool.org>
Subject: [Tarantool-patches] [PATCH 1/2] box/console: handle multireturn in lua mode
Date: Wed, 25 Dec 2019 19:08:52 +0300	[thread overview]
Message-ID: <20191225160853.1407-2-gorcunov@gmail.com> (raw)
In-Reply-To: <20191225160853.1407-1-gorcunov@gmail.com>

Currently we handle only first member of multireturn statement.
Fix it processing each element separately.

n.b.: While at this file add vim settings.

 | tarantool> \set output lua
 | true;
 | tarantool> 1,2,3,4
 | 1, 2, 3, 4;

Fixes #4604

Reported-by: Alexander Turenko <alexander.turenko@tarantool.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 src/box/lua/console.lua           | 38 ++++++++++++-------
 test/app-tap/console-lua.skipcond |  7 ++++
 test/app-tap/console-lua.test.lua | 62 +++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 13 deletions(-)
 create mode 100644 test/app-tap/console-lua.skipcond
 create mode 100755 test/app-tap/console-lua.test.lua

diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
index d4d8ec984..a4a36c625 100644
--- a/src/box/lua/console.lua
+++ b/src/box/lua/console.lua
@@ -1,4 +1,6 @@
 -- console.lua -- internal file
+--
+-- vim: ts=4 sw=4 et
 
 local serpent = require('serpent')
 local internal = require('console')
@@ -75,28 +77,38 @@ local serpent_map_symbols = function(tag, head, body, tail, level)
     return tag..head..body..tail
 end
 
-output_handlers["lua"] = function(status, opts, ...)
-    --
-    -- If no data present at least EOS should be put,
-    -- otherwise wire readers won't be able to find
-    -- where the end of string is.
-    if not ... then
-        return output_eos["lua"]
-    end
+--
+-- Format a Lua value.
+local function format_lua_value(value, opts)
     for k,v in pairs(lua_map_direct_symbols) do
-        if k == ... then
-            return v .. output_eos["lua"]
+        if k == value then
+            return v
         end
     end
     local serpent_opts = {
         custom  = serpent_map_symbols,
         comment = false,
-        nocode = true,
+        nocode  = true,
     }
     if opts == "block" then
-        return serpent.block(..., serpent_opts) .. output_eos["lua"]
+        return serpent.block(value, serpent_opts)
+    end
+    return serpent.line(value, serpent_opts)
+end
+
+output_handlers["lua"] = function(status, opts, ...)
+    local collect = {}
+    --
+    -- If no data present at least EOS should be put,
+    -- otherwise wire readers won't be able to find
+    -- where the end of string is.
+    if not ... then
+        return output_eos["lua"]
+    end
+    for i = 1, select('#', ...) do
+        collect[i] = format_lua_value(select(i, ...), opts)
     end
-    return serpent.line(..., serpent_opts) .. output_eos["lua"]
+    return table.concat(collect, ', ') .. output_eos["lua"]
 end
 
 local function output_verify_opts(fmt, opts)
diff --git a/test/app-tap/console-lua.skipcond b/test/app-tap/console-lua.skipcond
new file mode 100644
index 000000000..48e17903e
--- /dev/null
+++ b/test/app-tap/console-lua.skipcond
@@ -0,0 +1,7 @@
+import platform
+
+# Disabled on FreeBSD due to flaky fail #4271.
+if platform.system() == 'FreeBSD':
+    self.skip = 1
+
+# vim: set ft=python:
diff --git a/test/app-tap/console-lua.test.lua b/test/app-tap/console-lua.test.lua
new file mode 100755
index 000000000..d3271a369
--- /dev/null
+++ b/test/app-tap/console-lua.test.lua
@@ -0,0 +1,62 @@
+#!/usr/bin/env tarantool
+--
+-- vim: ts=4 sw=4 et
+
+local tap = require('tap')
+local console = require('console')
+local socket = require('socket')
+local yaml = require('yaml')
+local fiber = require('fiber')
+local ffi = require('ffi')
+local log = require('log')
+local fio = require('fio')
+
+-- Suppress console log messages
+log.level(4)
+local CONSOLE_SOCKET = fio.pathjoin(fio.cwd(), 'tarantool-test-console-lua.sock')
+os.remove(CONSOLE_SOCKET)
+
+local EOL = ";"
+
+test = tap.test("console-lua")
+
+test:plan(7)
+
+--
+-- Start console and connect to it
+local server = console.listen(CONSOLE_SOCKET)
+test:ok(server ~= nil, "console.listen started")
+
+local client = socket.tcp_connect("unix/", CONSOLE_SOCKET)
+local handshake = client:read{chunk = 128}
+test:ok(string.match(handshake, '^Tarantool .*console') ~= nil, 'Handshake')
+test:ok(client ~= nil, "connect to console")
+
+--
+-- Change output mode to Lua
+client:write('\\set output lua\n')
+test:is(client:read(EOL), 'true' .. EOL, "set lua output mode")
+
+--
+-- Write mulireturn statement
+client:write('a={1,2,3}\n')
+test:is(client:read(EOL), EOL, "declare array")
+
+client:write('1,2,nil,a\n')
+test:is(client:read(EOL), '1, 2, box.NULL, {1, 2, 3}' .. EOL, "multireturn numbers")
+
+--
+-- Disconect from console
+client:shutdown()
+client:close()
+
+--
+-- Stop console
+server:shutdown()
+server:close()
+fiber.sleep(0) -- workaround for gh-712: console.test.lua fails in Fedora
+-- Check that admin console has been stopped
+test:isnil(socket.tcp_connect("unix/", CONSOLE_SOCKET), "console.listen stopped")
+
+test:check()
+os.exit(0)
-- 
2.20.1

  reply	other threads:[~2019-12-25 16:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-25 16:08 [Tarantool-patches] [PATCH 0/2] box/console: Fixes for 2.3 series Cyrill Gorcunov
2019-12-25 16:08 ` Cyrill Gorcunov [this message]
2020-01-14  0:54   ` [Tarantool-patches] [PATCH 1/2] box/console: handle multireturn in lua mode Alexander Turenko
2020-01-14 20:27     ` Cyrill Gorcunov
2019-12-25 16:08 ` [Tarantool-patches] [PATCH 2/2] box/console: handle empty output format Cyrill Gorcunov
2020-01-09  9:36 ` [Tarantool-patches] [PATCH 0/2] box/console: Fixes for 2.3 series Cyrill Gorcunov

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=20191225160853.1407-2-gorcunov@gmail.com \
    --to=gorcunov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 1/2] box/console: handle multireturn in lua mode' \
    /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