Skip to content

Commit

Permalink
execCmdEx now also takes an optional `workingDir and env
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed May 13, 2020
1 parent 6cdda9b commit 22864f4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@


- `osproc.execCmdEx` now takes an optional `input` for stdin.
- `osproc.execCmdEx` now takes an optional `input` for stdin, `workingDir` and `env`
parameters.

## Language changes
- In the newruntime it is now allowed to assign discriminator field without restrictions as long as case object doesn't have custom destructor. Discriminator value doesn't have to be a constant either. If you have custom destructor for case object and you do want to freely assign discriminator fields, it is recommended to refactor object into 2 objects like this:
Expand Down
24 changes: 17 additions & 7 deletions lib/pure/osproc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1438,12 +1438,14 @@ elif not defined(useNimRtl):


proc execCmdEx*(command: string, options: set[ProcessOption] = {
poStdErrToStdOut, poUsePath}, input = ""): tuple[
poStdErrToStdOut, poUsePath}, env: StringTableRef = nil,
workingDir = "", input = ""): tuple[
output: TaintedString,
exitCode: int] {.tags:
[ExecIOEffect, ReadIOEffect, RootEffect], gcsafe.} =
## A convenience proc that runs the `command`, grabs all its output and
## exit code and returns both. If `input.len > 0`, it is passed as stdin.
## A convenience proc that runs the `command`, and returns its `output` and
## `exitCode`. `env` and `workingDir` params behave as for `startProcess`.
## If `input.len > 0`, it is passed as stdin.
## Note: this could block if `input.len` is greater than your OS's maximum
## pipe buffer size.
##
Expand All @@ -1456,13 +1458,21 @@ proc execCmdEx*(command: string, options: set[ProcessOption] = {
##
runnableExamples:
var result = execCmdEx("nim r --hints:off -", options = {}, input = "echo 3*4")
import strutils
stripLineEnd(result[0])
import strutils, strtabs
stripLineEnd(result[0]) ## portable way to remove trailing newline, if any
doAssert result == ("12", 0)
doAssert execCmdEx("ls --nonexistant").exitCode != 0
when defined(posix):
assert execCmdEx("echo $FO", env = newStringTable({"FO": "B"})) == ("B\n", 0)
assert execCmdEx("echo $PWD", workingDir = "/") == ("/\n", 0)

when (NimMajor, NimMinor) < (1, 3):
when (NimMajor, NimMinor, NimPatch) < (1, 3, 5):
doAssert input.len == 0
var p = startProcess(command, options = options + {poEvalCommand})
doAssert workingDir.len == 0
doAssert env == nil

var p = startProcess(command, options = options + {poEvalCommand},
workingDir = workingDir, env = env)
var outp = outputStream(p)

if input.len > 0:
Expand Down
3 changes: 2 additions & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2068,8 +2068,9 @@ const
## is the minor number of Nim's version.
## Odd for devel, even for releases.

NimPatch* {.intdefine.}: int = 3
NimPatch* {.intdefine.}: int = 5
## is the patch number of Nim's version.
## Odd for devel, even for releases.

import system/dollars
export dollars
Expand Down
8 changes: 8 additions & 0 deletions tests/stdlib/tosproc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,15 @@ else:
var result = startProcessTest("nim r --hints:off -", options = {}, input = "echo 3*4")
doAssert result == ("12\n", 0)

import std/strtabs
block execProcessTest:
var result = execCmdEx("nim r --hints:off -", options = {}, input = "echo 3*4")
stripLineEnd(result[0])
doAssert result == ("12", 0)
doAssert execCmdEx("ls --nonexistant").exitCode != 0
when false:
# bug: on windows, this raises; on posix, passes
doAssert execCmdEx("nonexistant").exitCode != 0
when defined(posix):
doAssert execCmdEx("echo $FO", env = newStringTable({"FO": "B"})) == ("B\n", 0)
doAssert execCmdEx("echo $PWD", workingDir = "/") == ("/\n", 0)

0 comments on commit 22864f4

Please sign in to comment.