Skip to content

Commit

Permalink
echo cmd | nim r - -arg1 -arg2 now works (nim-lang#14210)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored May 4, 2020
1 parent a73d403 commit f115e40
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
config.commandLine.add ':'
config.commandLine.add p.val.quoteShell

if p.key == " ":
if p.key == "": # `-` was passed to indicate main project is stdin
p.key = "-"
if processArgument(pass, p, argsCount, config): break
else:
Expand Down
32 changes: 31 additions & 1 deletion tests/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ discard """

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

const nim = getCurrentCompilerExe()

proc runCmd(file, options = ""): auto =
let mode = if existsEnv("NIM_COMPILE_TO_CPP"): "cpp" else: "c"
const nim = getCurrentCompilerExe()
const testsDir = currentSourcePath().parentDir
let fileabs = testsDir / file.unixToNativePath
doAssert fileabs.existsFile, fileabs
Expand Down Expand Up @@ -60,3 +61,32 @@ else: # don't run twice the same test
check "sizeof(Foo5) == 3"
check "sizeof(struct Foo6) == "
doAssert exitCode != 0

import streams
block: # stdin input
let nimcmd = fmt"{nim} r --hints:off - -firstparam '-second param'"
let inputcmd = "import os; echo commandLineParams()"
let expected = """@["-firstparam", "-second param"]"""
block:
let p = startProcess(nimcmd, options = {poEvalCommand})
p.inputStream.write("import os; echo commandLineParams()")
p.inputStream.close
var output = p.outputStream.readAll
let error = p.errorStream.readAll
doAssert p.waitForExit == 0
when false: # https://github.com/timotheecour/Nim/issues/152
# bug: `^[[0m` is being inserted somehow with `-` (regarless of --run)
doAssert error.len == 0, $(error,)
output.stripLineEnd
doAssert output == expected
p.errorStream.close
p.outputStream.close

block:
when defined(posix):
let cmd = fmt"echo 'import os; echo commandLineParams()' | {nimcmd}"
# avoid https://github.com/timotheecour/Nim/issues/152 by
# making sure `poStdErrToStdOut` isn't passed
var (output, exitCode) = execCmdEx(cmd, options = {poEvalCommand})
output.stripLineEnd
doAssert output == expected

0 comments on commit f115e40

Please sign in to comment.