Make it possible to give an array as argument to "cmd".
The intention is that the array should be passed "as is" to the invoked program. This should work on both UN*X and Windows so it has to deal with the quoting of both sh(1) and CMD.EXE. The Windows part has not been done yet.
This commit is contained in:

committed by
holmberg556

parent
107f6557db
commit
64804512a2
@ -497,7 +497,29 @@ module Cmdtest
|
||||
|
||||
#------------------------------
|
||||
|
||||
def _args_to_quoted_string(args)
|
||||
quoted_args = []
|
||||
for arg in args
|
||||
if arg =~ /[;&()><\\| $"]/
|
||||
quoted_arg = arg.dup
|
||||
quoted_arg.gsub!(/\\/, "\\\\")
|
||||
quoted_arg.gsub!(/"/, "\\\"")
|
||||
quoted_arg.gsub!(/\$/, "\\$")
|
||||
quoted_arg.gsub!(/`/, "\\\\`")
|
||||
quoted_args << '"' + quoted_arg + '"'
|
||||
else
|
||||
quoted_args << arg
|
||||
end
|
||||
end
|
||||
quoted_args.join(" ")
|
||||
end
|
||||
|
||||
#------------------------------
|
||||
|
||||
def cmd(cmdline)
|
||||
if Array === cmdline
|
||||
cmdline = _args_to_quoted_string(cmdline)
|
||||
end
|
||||
Util.wait_for_new_second
|
||||
_update_hardlinks
|
||||
@_cmdline = cmdline
|
||||
|
109
t/01-cmd.rb
Normal file
109
t/01-cmd.rb
Normal file
@ -0,0 +1,109 @@
|
||||
#======================================================================
|
||||
|
||||
#-----------------------------------
|
||||
# Using Array argument to cmd
|
||||
|
||||
cmd ["lines.rb", "this is an argument", "and another"] do
|
||||
stdout_equal [
|
||||
"this is an argument",
|
||||
"and another",
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb "this is an argument" "and another"
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# only some arguments need quoting
|
||||
|
||||
cmd ["lines.rb", "arg1", "a r g 2", "<arg3>"] do
|
||||
stdout_equal [
|
||||
"arg1",
|
||||
"a r g 2",
|
||||
"<arg3>",
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb arg1 "a r g 2" "<arg3>"
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# array with no arguments
|
||||
|
||||
cmd ["true.rb"] do
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### true.rb
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# array with no arguments (II)
|
||||
|
||||
cmd ["false.rb"] do
|
||||
exit_nonzero
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### false.rb
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# array with " and \ in arguments
|
||||
|
||||
cmd ["lines.rb", "emb\"edded1", "emb\\edded2", "emb\\edd\"ed3"] do
|
||||
stdout_equal [
|
||||
"emb\"edded1",
|
||||
"emb\\edded2",
|
||||
"emb\\edd\"ed3",
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb "emb\"edded1" "emb\edded2" "emb\edd\"ed3"
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# array with $ arguments
|
||||
|
||||
cmd ["lines.rb", "emb$edded1", "emb$$edded2"] do
|
||||
stdout_equal [
|
||||
"emb$edded1",
|
||||
"emb$$edded2",
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb "emb\$edded1" "emb\$\$edded2"
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# "all" characters (but not ` for now)
|
||||
|
||||
all = " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
|
||||
cmd ["lines.rb", all] do
|
||||
stdout_equal [
|
||||
" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb " !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
|
||||
# stdout end
|
||||
|
||||
#-----------------------------------
|
||||
# "`" character
|
||||
|
||||
all = " ` "
|
||||
cmd ["lines.rb", all] do
|
||||
stdout_equal [
|
||||
" ` ",
|
||||
]
|
||||
end
|
||||
|
||||
# stdout begin
|
||||
# ### lines.rb " \` "
|
||||
# stdout end
|
||||
|
Reference in New Issue
Block a user