Skip to content

std.process

The running program’s arguments, environment and exit code, and the programs it runs. main may return () (exit code 0), an ExitCode, or a Result of either. An Err is written to standard error and exits with 1.

type ExitCode = Int
where 0 <= it && it <= 255

A process exit code from 0 to 255. 0 is success.

capability Process {}

The running process. Only the runtime makes one, for main.

type FakeEnvironment = {
arguments: List[String],
variables: Dict[String, String],
}

An environment for tests.

type RunMode =
// `strata run`, or a built executable
| Running
// `strata test`
| Testing
// `strata run --trace`
| Tracing

How the program is being run. main or a test may ask for it only when strata.toml sets mode = true under [run]. Otherwise a program behaves the same wherever it runs.

type Finished = { code: Int, out: String, err: String }

A finished program’s exit code and output. A program killed by a signal has code 128 + the signal.

type RunOptions = {
/// Its standard input. `""` is none.
input: String = "",
/// Environment variables set on top of this program's own, such as `("GIT_DIR", "x")`.
env: List[(String, String)] = [],
/// Gives it this program's terminal, for an editor or output shown as it comes. `Finished` then holds only the exit code.
terminal: Bool = false,
}

How a program is run, besides its arguments and folder.

capability Launcher {}

The real system’s programs. Only the runtime makes one, for main or a test that names it.

type FinishedBytes = { code: Int, out: List[Int], err: List[Int] }

A finished program’s exit code and output, as bytes (run_bytes), each an Int from 0 to 255.

capability trait Environment {
/// The command-line arguments, without the program's own name. Bytes that aren't UTF-8 read as U+FFFD.
fn args(self) -> List[String]
/// The value of an environment variable, if it is set. Bytes that aren't UTF-8 read as U+FFFD.
fn var(self, name: String) -> Option[String]
/// The current folder, absolute.
fn current_dir(self) -> String
}

What the program was started with: the real process or a stand-in.

capability trait Commands {
/// Runs `program` with `args` in the folder `dir` and waits for it.
/// - `program` is looked up in `PATH` unless it holds a `/`. No shell is involved.
/// - `dir` is `""` for the current folder.
/// - Its standard input is empty.
/// - `Err` when it can't be started (no such program or folder, or a NUL byte in an argument), or when its output isn't UTF-8 text (`run_bytes` reads it as bytes). A program that starts and fails is `Ok` with its exit code.
fn run(
mut self,
program: String,
args: List[String],
dir: String,
) -> Result[Finished, String]
ensures match it {
Ok(f) => 0 <= f.code,
Err(_) => true,
}
/// `run`, with standard input, environment and terminal set by `options`.
fn run_with(
mut self,
program: String,
args: List[String],
dir: String,
options: RunOptions,
) -> Result[Finished, String]
ensures match it {
Ok(f) => 0 <= f.code,
Err(_) => true,
}
/// `run_with`, its output and error output as bytes, whether or not they
/// are UTF-8 text: for a program that writes binary data.
fn run_bytes(
mut self,
program: String,
args: List[String],
dir: String,
options: RunOptions,
) -> Result[FinishedBytes, String]
ensures match it {
Ok(f) => 0 <= f.code,
Err(_) => true,
}
}

Starts other programs: Launcher for the real system, or a mock in a test.

fn fake_environment(
arguments: List[String],
variables: List[(String, String)],
) -> FakeEnvironment

A test environment with these arguments and variables.

  • impl Environment for Process
  • impl Environment for FakeEnvironment
  • impl Commands for Launcher