From 64804512a2cebb8e429028d11c1b962d2abfd620 Mon Sep 17 00:00:00 2001 From: Johan Holmberg Date: Wed, 8 Apr 2009 07:08:13 +0000 Subject: [PATCH] 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. --- lib/cmdtest/testcase.rb | 22 ++++++++ t/01-cmd.rb | 109 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 t/01-cmd.rb 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 +