[Tarantool-patches] [PATCH 2/2] console: add line carrying backslash

Olga Arkhangelskaia arkholga at tarantool.org
Wed Jun 10 12:43:54 MSK 2020


When using interactive console(stdin) instead of \set delimiter <delimiter>
with "\", "\" in the end if line can be used.

Closes #4317
---
 src/box/lua/console.lua       | 18 ++++++++-----
 test/app-tap/gh-4317.test.lua | 51 +++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 6 deletions(-)
 create mode 100755 test/app-tap/gh-4317.test.lua

diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
index 17e2c91b2..4c2c7a132 100644
--- a/src/box/lua/console.lua
+++ b/src/box/lua/console.lua
@@ -573,14 +573,20 @@ local function local_read(self)
             break
         end
         if delim == "" then
-            local lang = box.session.language
-            if not lang or lang == 'lua' then
-                -- stop once a complete Lua statement is entered
-                if local_check_lua(buf) then
+            -- if no delim is set and line ends with the backslash
+            -- continue reading
+            if buf:sub(-1, -1) == '\\' then
+                buf = buf:sub(0, #buf - 1)
+            else
+                local lang = box.session.language
+                if not lang or lang == 'lua' then
+                    -- stop once a complete Lua statement is entered
+                    if local_check_lua(buf) then
+                        break
+                    end
+                else
                     break
                 end
-            else
-                break
             end
         elseif #buf >= #delim and buf:sub(#buf - #delim + 1) == delim then
             buf = buf:sub(0, #buf - #delim)
diff --git a/test/app-tap/gh-4317.test.lua b/test/app-tap/gh-4317.test.lua
new file mode 100755
index 000000000..d7a5063e7
--- /dev/null
+++ b/test/app-tap/gh-4317.test.lua
@@ -0,0 +1,51 @@
+#!/usr/bin/env tarantool
+
+local tap = require('tap')
+local console = require('console')
+local fio = require('fio')
+
+-- The following cases use LD_PRELOAD, so will not work under
+-- Mac OS X.
+if jit.os == 'OSX' then
+    os.exit(0)
+end
+
+test = tap.test("console")
+test:plan(1)
+
+-- Allow to run the test from the repository root without
+-- test-run:
+--
+-- $ ./src/tarantool test/app-tap/gh-4317.test.lua
+--
+-- It works at least for in-source build.
+local is_under_test_run = pcall(require, 'test_run')
+local isatty_lib_dir = is_under_test_run and '../..' or 'test'
+local isatty_lib_path = fio.pathjoin(isatty_lib_dir, 'libisatty.so')
+local saved_path = os.getenv('PATH')
+if not is_under_test_run then
+    os.setenv('PATH', './src:', saved_path)
+end
+
+local tarantool_command = "local a = 0 \\\nfor i = 1, 10 do\na = a + i\nend \\\nprint(a)"
+local result_str =  [[tarantool> local a = 0 \
+         > for i = 1, 10 do
+         > a = a + i
+         > end \
+         > print(a)
+55
+---
+...
+
+tarantool> ]]
+local cmd = ([[printf '%s\n' | LD_PRELOAD='%s' tarantool ]] ..
+[[2>/dev/null]]):format(tarantool_command, isatty_lib_path)
+local fh = io.popen(cmd, 'r')
+-- Readline on CentOS 7 produces \e[?1034h escape
+-- sequence before tarantool> prompt, remove it.
+local result = fh:read('*a'):gsub('\x1b%[%?1034h', '')
+fh:close()
+test:is(result, result_str, "Backslash")
+
+test:check()
+os.exit(0)
-- 
2.20.1 (Apple Git-117)



More information about the Tarantool-patches mailing list