class ShellProcess : AutoCloseable


Represents a process that is running sh as a shell user.

Opposite to Shell this class doesn't handle the concept of command, rather it allows to read and write in the process streams, i.e. stdin, stdout, stderr. For example, using ShellProcess, it's possible to write a file in shell space like /data/local/tmp launching cp /dev/stdin /data/local/tmp/myfile:

ShellProcess.create().use {

// Writing a file
it.write("cp /dev/stdin /data/local/tmp/myfile")
it.stdin.write(myByteByffer)

// Reading a file
it.write("cat /data/local/tmp/somefile")
assert(it.stdout.bufferedReader().readLine() == "This is a test")
}

For the vast majority of commands used for testing, Shell process is a better candidate and automatically captures the full command output, as opposed to ShellProcess that doesn't distinguish where a command ends and another starts.

Note that this class is not thread safe due to internal state changes, although streams can be read or written by another thread, as long as it's always the same. For example it's possible to read ShellProcess.stdOut from another thread, as long as this doesn't change later.

ShellProcess should be used only when access to streams is required, like in the above example.

Summary

Public functions

open Unit

Closes the current process.

Boolean

Returns whether the process is closed.

Unit
writeLine(string: String)

Writes a string in the process standard input.

Public properties

InputStream

An InputStream for the process standard error.

OutputStream

An OutputStream for the process standard output.

InputStream

An InputStream for the process standard output.

Public functions

close

Added in 1.0.0-alpha02
open fun close(): Unit

Closes the current process. Internally this simply sends the shell process the exit command. This means that the process is not immediately terminated, but gracefully shutdown finishing prior commands execution. Once a shell process is closed, further calls to close don't have any effect, as the stdIn will be closed after the first one.

isClosed

Added in 1.0.0-alpha02
fun isClosed(): Boolean

Returns whether the process is closed.

writeLine

Added in 1.0.0-alpha02
fun writeLine(string: String): Unit

Writes a string in the process standard input.

Parameters
string: String

a utf-8 string to write in the process stdin.

Public properties

stdErr

Added in 1.0.0-alpha02
val stdErrInputStream

An InputStream for the process standard error. Note that this is backed by a tcp connection to the native cli utility. If a large output is expected but it's not important to capture it, a shell command may be launched piping in /dev/null.

For example:

cat large_file 2> /dev/null

stdIn

Added in 1.0.0-alpha02
val stdInOutputStream

An OutputStream for the process standard output.

stdOut

Added in 1.0.0-alpha02
val stdOutInputStream

An InputStream for the process standard output. Note that this is backed by a tcp connection to the native cli utility. If a large output is expected but it's not important to capture it, a shell command may be launched piping in /dev/null.

For example:

cat large_file /dev/null