summary refs log tree commit diff stats
path: root/tests/misc
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2019-01-12 15:26:22 -0800
committerTimothee Cour <timothee.cour2@gmail.com>2019-01-14 17:00:54 -0800
commit0a8762eb7b4b3c00cb78a40430f51080913a9ee6 (patch)
tree850fbc31138d8e93dd8af3474e7781b481a435eb /tests/misc
parente17321aa2429c6bed97bef28a149fd21166b90a2 (diff)
downloadNim-0a8762eb7b4b3c00cb78a40430f51080913a9ee6.tar.gz
fix #9842 #9951: `nim -r` and parseopt.cmdLineRest are now correct
Diffstat (limited to 'tests/misc')
-rw-r--r--tests/misc/tparseopt.nim136
1 files changed, 92 insertions, 44 deletions
diff --git a/tests/misc/tparseopt.nim b/tests/misc/tparseopt.nim
index cbed5d476..39c17869c 100644
--- a/tests/misc/tparseopt.nim
+++ b/tests/misc/tparseopt.nim
@@ -30,54 +30,102 @@ kind: cmdLongOption	key:val  --  debug:3
 kind: cmdShortOption	key:val  --  l:4
 kind: cmdShortOption	key:val  --  r:2'''
 """
-from parseopt import nil
-from parseopt2 import nil
 
+when defined(testament_tparseopt):
+  import os
+  proc main() =
+    let args = commandLineParams()
+    echo args
+    for i, ai in args:
+      echo "arg ", i, " ai.len:", ai.len, " :{", ai, "}"
+  main()
+else:
+  from parseopt import nil
+  from parseopt2 import nil
 
-block:
-  echo "parseopt"
-  for kind, key, val in parseopt.getopt():
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+  block:
+    echo "parseopt"
+    for kind, key, val in parseopt.getopt():
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
 
-  # pass custom cmdline arguments
-  echo "first round"
-  var argv = "--left --debug:3 -l=4 -r:2"
-  var p = parseopt.initOptParser(argv)
-  for kind, key, val in parseopt.getopt(p):
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
-    break
-  # reset getopt iterator and check arguments are returned correctly.
-  echo "second round"
-  for kind, key, val in parseopt.getopt(p):
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+    # pass custom cmdline arguments
+    echo "first round"
+    var argv = "--left --debug:3 -l=4 -r:2"
+    var p = parseopt.initOptParser(argv)
+    for kind, key, val in parseopt.getopt(p):
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+      break
+    # reset getopt iterator and check arguments are returned correctly.
+    echo "second round"
+    for kind, key, val in parseopt.getopt(p):
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
 
-  # bug #9619
-  var x = parseopt.initOptParser(@["--foo:", "--path"], allowWhitespaceAfterColon = false)
-  for kind, key, val in parseopt.getopt(x):
-    echo kind, " ", key
+    # bug #9619
+    var x = parseopt.initOptParser(@["--foo:", "--path"],
+        allowWhitespaceAfterColon = false)
+    for kind, key, val in parseopt.getopt(x):
+      echo kind, " ", key
 
-block:
-  echo "parseoptNoVal"
-  # test NoVal mode with custom cmdline arguments
-  var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4"
-  var p = parseopt.initOptParser(argv,
-                                  shortNoVal = {'l'}, longNoVal = @["left"])
-  for kind, key, val in parseopt.getopt(p):
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+  block:
+    echo "parseoptNoVal"
+    # test NoVal mode with custom cmdline arguments
+    var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4"
+    var p = parseopt.initOptParser(argv,
+                                    shortNoVal = {'l'}, longNoVal = @["left"])
+    for kind, key, val in parseopt.getopt(p):
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
 
-block:
-  echo "parseopt2"
-  for kind, key, val in parseopt2.getopt():
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+  block:
+    echo "parseopt2"
+    for kind, key, val in parseopt2.getopt():
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
 
-  # pass custom cmdline arguments
-  echo "first round"
-  var argv: seq[string] = @["--left", "--debug:3", "-l=4", "-r:2"]
-  var p = parseopt2.initOptParser(argv)
-  for kind, key, val in parseopt2.getopt(p):
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
-    break
-  # reset getopt iterator and check arguments are returned correctly.
-  echo "second round"
-  for kind, key, val in parseopt2.getopt(p):
-    echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+    # pass custom cmdline arguments
+    echo "first round"
+    var argv: seq[string] = @["--left", "--debug:3", "-l=4", "-r:2"]
+    var p = parseopt2.initOptParser(argv)
+    for kind, key, val in parseopt2.getopt(p):
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+      break
+    # reset getopt iterator and check arguments are returned correctly.
+    echo "second round"
+    for kind, key, val in parseopt2.getopt(p):
+      echo "kind: ", kind, "\tkey:val  --  ", key, ":", val
+
+  import osproc, os, strutils
+  from stdtest/specialpaths import buildDir
+  import "../.." / compiler/unittest_light
+
+  block: # fix #9951
+    var p = parseopt.initOptParser(@["echo \"quoted\""])
+    assertEquals parseopt.cmdLineRest(p), """'echo "quoted"'"""
+    let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
+    var p2 = parseopt.initOptParser(args)
+    assertEquals parseopt.cmdLineRest(p2),
+      """a1b 'a2 b' '' 'a4"b' 'a5'"'"'b' 'a6\b' 'a7'"'"'b'"""
+    doAssert "a5'b" == "a5\'b"
+
+  block: # fix #9842
+    let exe = buildDir / "D20190112T145450".addFileExt(ExeExt)
+    defer: removeFile exe
+    let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
+    let cmd = "$# c -r --verbosity:0 -o:$# -d:testament_tparseopt $# $#" %
+      [getCurrentCompilerExe(), exe, currentSourcePath(),
+          args.quoteShellCommand]
+    var ret = execCmdEx(cmd, options = {})
+    if ret.exitCode != 0:
+      # before bug fix, running cmd would show:
+      # sh: -c: line 0: unexpected EOF while looking for matching `"'\n
+      echo "exitCode: ", ret.exitCode, " cmd:", cmd
+      doAssert false
+    stripLineEnd(ret.output)
+    assertEquals ret.output,
+      """
+@["a1b", "a2 b", "", "a4\"b", "a5\'b", "a6\\b", "a7\'b"]
+arg 0 ai.len:3 :{a1b}
+arg 1 ai.len:4 :{a2 b}
+arg 2 ai.len:0 :{}
+arg 3 ai.len:4 :{a4"b}
+arg 4 ai.len:4 :{a5'b}
+arg 5 ai.len:4 :{a6\b}
+arg 6 ai.len:4 :{a7'b}"""