From 8630a263b6c5f86d57eb0f3a49ddce163c8378c1 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Fri, 28 Oct 2016 14:23:04 +0300 Subject: Add a few tests for fs.Q --- spec/fs_spec.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/fs_spec.lua diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua new file mode 100644 index 00000000..c76f47a4 --- /dev/null +++ b/spec/fs_spec.lua @@ -0,0 +1,21 @@ +local test_env = require("test/test_environment") + +test_env.unload_luarocks() +local fs = require("luarocks.fs") +local is_win = test_env.TEST_TARGET_OS == "windows" + +describe("Luarocks fs test #whitebox #w_fs", function() + describe("fs.Q", function() + it("simple argument", function() + assert.are.same(is_win and '"foo"' or "'foo'", fs.Q("foo")) + end) + + it("argument with quotes", function() + assert.are.same(is_win and [["it's \"quoting\""]] or [['it'\''s "quoting"']], fs.Q([[it's "quoting"]])) + end) + + it("argument with special characters", function() + assert.are.same(is_win and [["\\"%" \\\\" \\\\\\"]] or [['\% \\" \\\']], fs.Q([[\% \\" \\\]])) + end) + end) +end) -- cgit v1.2.3-55-g6feb From 7f67011db75e671068b7f02d5489491f512bf816 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Fri, 28 Oct 2016 14:27:51 +0300 Subject: Move Unix-specific fs.Q implementation into luarocks.fs.unix --- src/luarocks/fs/lua.lua | 11 ----------- src/luarocks/fs/unix.lua | 9 +++++++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index a31cbb4e..41711eab 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -30,17 +30,6 @@ local dir_stack = {} local dir_separator = "/" ---- Quote argument for shell processing. --- Adds single quotes and escapes. --- @param arg string: Unquoted argument. --- @return string: Quoted argument. -function fs_lua.Q(arg) - assert(type(arg) == "string") - - -- FIXME Unix-specific - return "'" .. arg:gsub("'", "'\\''") .. "'" -end - --- Test is file/dir is writable. -- Warning: testing if a file/dir is writable does not guarantee -- that it will remain writable and therefore it is no replacement diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 5c6b542c..e2bdc7b8 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -22,6 +22,15 @@ function unix.quiet_stderr(cmd) return cmd.." 2> /dev/null" end +--- Quote argument for shell processing. +-- Adds single quotes and escapes. +-- @param arg string: Unquoted argument. +-- @return string: Quoted argument. +function unix.Q(arg) + assert(type(arg) == "string") + return "'" .. arg:gsub("'", "'\\''") .. "'" +end + --- Return an absolute pathname from a potentially relative one. -- @param pathname string: pathname to convert. -- @param relative_to string or nil: path to prepend when making -- cgit v1.2.3-55-g6feb From afd9b2a4f2187b802f1febcff9b54073c6d8943d Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Fri, 28 Oct 2016 14:09:20 +0300 Subject: Refactor windows argument quoting functions Use string replacements instead of functions in calls to gsub. --- src/luarocks/fs/win32.lua | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c99cc895..14804713 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -35,19 +35,6 @@ end local drive_letter = "[%.a-zA-Z]?:?[\\/]" -local win_escape_chars = { - ["%"] = "%%", - ['"'] = '\\"', -} - -local function q_escaper(bs, q) - return ("\\"):rep(2*#bs-1) .. (q or "\\") -end - -local function p_escaper(bs) - return bs .. bs .. '"%"' -end - --- Quote argument for shell processing. Fixes paths on Windows. -- Adds double quotes and escapes. -- @param arg string: Unquoted argument. @@ -61,11 +48,11 @@ function win32.Q(arg) if arg == "\\" then return '\\' -- CHDIR needs special handling for root dir end - -- URLs and anything else - arg = arg:gsub('(\\+)(")', q_escaper) - arg = arg:gsub('(\\+)$', q_escaper) - arg = arg:gsub('"', win_escape_chars) - arg = arg:gsub('(\\*)%%', p_escaper) + -- URLs and anything else + arg = arg:gsub('\\(\\*)"', '\\%1%1"') + arg = arg:gsub('\\+$', '%0%0') + arg = arg:gsub('"', '\\"') + arg = arg:gsub('(\\*)%%', '%1%1"%%"') return '"' .. arg .. '"' end @@ -83,9 +70,10 @@ function win32.Qb(arg) return '\\' -- CHDIR needs special handling for root dir end -- URLs and anything else - arg = arg:gsub('(\\+)(")', q_escaper) - arg = arg:gsub('(\\+)$', q_escaper) - arg = arg:gsub('[%%"]', win_escape_chars) + arg = arg:gsub('\\(\\*)"', '\\%1%1"') + arg = arg:gsub('\\+$', '%0%0') + arg = arg:gsub('"', '\\"') + arg = arg:gsub('%%', '%%%%') return '"' .. arg .. '"' end -- cgit v1.2.3-55-g6feb