Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to redirect os.Inherited streams globally to be consistent with std streams redirections #283

Merged
merged 10 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions os/src/ProcessOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,31 @@ case class proc(command: Shellable*) {
mergeErrIntoOut: Boolean = false,
propagateEnv: Boolean = true
): SubProcess = {
val builder =
buildProcess(commandChunks, cwd, env, stdin, stdout, stderr, mergeErrIntoOut, propagateEnv)

val cmdChunks = commandChunks
val commandStr = cmdChunks.mkString(" ")

def resolve[T](x: T, y: T) = if (x == os.Inherit) y else x
val resolvedStdin = resolve(stdin, os.Inherit.in.value)
val resolvedStdout = resolve(stdout, os.Inherit.out.value)
val resolvedStderr = resolve(stderr, os.Inherit.err.value)

val builder = buildProcess(
commandChunks,
cwd,
env,
resolvedStdin,
resolvedStdout,
resolvedStderr,
mergeErrIntoOut,
propagateEnv
)

lazy val proc: SubProcess = new SubProcess(
builder.start(),
stdin.processInput(proc.stdin).map(new Thread(_, commandStr + " stdin thread")),
stdout.processOutput(proc.stdout).map(new Thread(_, commandStr + " stdout thread")),
stderr.processOutput(proc.stderr).map(new Thread(_, commandStr + " stderr thread"))
resolvedStdin.processInput(proc.stdin).map(new Thread(_, commandStr + " stdin thread")),
resolvedStdout.processOutput(proc.stdout).map(new Thread(_, commandStr + " stdout thread")),
resolvedStderr.processOutput(proc.stderr).map(new Thread(_, commandStr + " stderr thread"))
)

proc.inputPumperThread.foreach(_.start())
Expand Down
20 changes: 19 additions & 1 deletion os/src/SubProcess.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,31 @@ object ProcessOutput {
}

/**
* Inherit the input/output stream from the current process
* Inherit the input/output stream from the current process.
*
* Can be overriden on a thread local basis for the various
* kinds of streams (stdin, stdout, stderr) via [[in]], [[out]], and [[err]]
*/
object Inherit extends ProcessInput with ProcessOutput {
def redirectTo = ProcessBuilder.Redirect.INHERIT
def redirectFrom = ProcessBuilder.Redirect.INHERIT
def processInput(stdin: => SubProcess.InputStream) = None
def processOutput(stdin: => SubProcess.OutputStream) = None

val in = new scala.util.DynamicVariable[ProcessInput](Inherit)
val out = new scala.util.DynamicVariable[ProcessOutput](Inherit)
val err = new scala.util.DynamicVariable[ProcessOutput](Inherit)
}

/**
* Inherit the input/output stream from the current process.
* Identical of [[os.Inherit]], except it cannot be redirected globally
*/
object Inherit0 extends ProcessInput with ProcessOutput {
def redirectTo = ProcessBuilder.Redirect.INHERIT
def redirectFrom = ProcessBuilder.Redirect.INHERIT
def processInput(stdin: => SubProcess.InputStream) = None
def processOutput(stdin: => SubProcess.OutputStream) = None
}

/**
Expand Down
15 changes: 15 additions & 0 deletions os/test/src-jvm/OpTestsJvmOnly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,20 @@ object OpTestsJvmOnly extends TestSuite {
val d = testFolder / "readWrite"
intercept[nio.NoSuchFileException](os.list(d / "nonexistent"))
}

// Not sure why this doesn't work on native
test("redirectSubprocessInheritedOutput") {
if (Unix()) { // relies on bash scripts that don't run on windows
val scriptFolder = os.pwd / "os" / "test" / "resources" / "test"
val lines = collection.mutable.Buffer.empty[String]
os.Inherit.out.withValue(os.ProcessOutput.Readlines(lines.append(_))) {
os.proc(scriptFolder / "misc" / "echo_with_wd", "HELLO\nWorld").call(
cwd = os.root / "usr",
stdout = os.Inherit
)
}
assert(lines == Seq("HELLO", "World /usr"))
}
}
}
}