diff --git a/lib/cmdtest/testcase.rb b/lib/cmdtest/testcase.rb index bf36b43..db8ecea 100644 --- a/lib/cmdtest/testcase.rb +++ b/lib/cmdtest/testcase.rb @@ -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 diff --git a/t/01-cmd.rb b/t/01-cmd.rb new file mode 100644 index 0000000..90833d0 --- /dev/null +++ b/t/01-cmd.rb @@ -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", ""] do + stdout_equal [ + "arg1", + "a r g 2", + "", + ] +end + +# stdout begin +# ### lines.rb arg1 "a r g 2" "" +# 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 +