Skip to content

Commit f115e40

Browse files
authored
echo cmd | nim r - -arg1 -arg2 now works (nim-lang#14210)
1 parent a73d403 commit f115e40

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

compiler/nim.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
5252
config.commandLine.add ':'
5353
config.commandLine.add p.val.quoteShell
5454

55-
if p.key == " ":
55+
if p.key == "": # `-` was passed to indicate main project is stdin
5656
p.key = "-"
5757
if processArgument(pass, p, argsCount, config): break
5858
else:

tests/trunner.nim

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ discard """
77

88
import std/[strformat,os,osproc,strutils]
99

10+
const nim = getCurrentCompilerExe()
11+
1012
proc runCmd(file, options = ""): auto =
1113
let mode = if existsEnv("NIM_COMPILE_TO_CPP"): "cpp" else: "c"
12-
const nim = getCurrentCompilerExe()
1314
const testsDir = currentSourcePath().parentDir
1415
let fileabs = testsDir / file.unixToNativePath
1516
doAssert fileabs.existsFile, fileabs
@@ -60,3 +61,32 @@ else: # don't run twice the same test
6061
check "sizeof(Foo5) == 3"
6162
check "sizeof(struct Foo6) == "
6263
doAssert exitCode != 0
64+
65+
import streams
66+
block: # stdin input
67+
let nimcmd = fmt"{nim} r --hints:off - -firstparam '-second param'"
68+
let inputcmd = "import os; echo commandLineParams()"
69+
let expected = """@["-firstparam", "-second param"]"""
70+
block:
71+
let p = startProcess(nimcmd, options = {poEvalCommand})
72+
p.inputStream.write("import os; echo commandLineParams()")
73+
p.inputStream.close
74+
var output = p.outputStream.readAll
75+
let error = p.errorStream.readAll
76+
doAssert p.waitForExit == 0
77+
when false: # https://github.com/timotheecour/Nim/issues/152
78+
# bug: `^[[0m` is being inserted somehow with `-` (regarless of --run)
79+
doAssert error.len == 0, $(error,)
80+
output.stripLineEnd
81+
doAssert output == expected
82+
p.errorStream.close
83+
p.outputStream.close
84+
85+
block:
86+
when defined(posix):
87+
let cmd = fmt"echo 'import os; echo commandLineParams()' | {nimcmd}"
88+
# avoid https://github.com/timotheecour/Nim/issues/152 by
89+
# making sure `poStdErrToStdOut` isn't passed
90+
var (output, exitCode) = execCmdEx(cmd, options = {poEvalCommand})
91+
output.stripLineEnd
92+
doAssert output == expected

0 commit comments

Comments
 (0)