Skip to content

std::process::testing

Testing code that starts programs, without starting any. Inside testing.run(answers, body), every std::process call body makes, and every call the tasks it starts make, is handled by the first Answer for its program and arguments, and recorded. run returns what body returned together with each call, in order, with the input the program was given. A program no Answer names fails to start with NotFound, as a program missing from the PATH does. As with std::fs::testing.run, the answers last for the call and follow its tasks, and a nested run takes over for its own call.

import
    std::process::testing.{self, Answer, Call}
    branch
end

test "reads the current branch"
    ran = testing.run([Answer{program: "git", stdout: "main\n"}], fn ->
        branch.current()
    end)

    assert ran.value == Ok("main")

    assert ran.calls == [
        Call{program: "git", args: ["branch", "--show-current"]},
    ]
end

Import with import std::process::testing.

struct Answer
    program: String
    args: Maybe<List<String>> = None
    code: Int64 = 0
    stdout: String = ""
    stderr: String = ""
    error: Maybe<Error> = None
end

How the fake responds to a start of program: with the given output and exit code, or, when error is set, by failing to start with it.

import std::process.{self, Command, Status}

answers = [
    Answer{program: "git", args: Some(["push"]), code: 1, stderr: "rejected\n"},
    Answer{program: "git"},
    Answer{program: "deploy", error: Some(Error.PermissionDenied{path: "deploy"})},
]

ran = run(answers, fn ->
    (
        process.output(Command{program: "git", args: ["push"]}),
        process.run(Command{program: "git", args: ["fetch"]}),
        process.run(Command{program: "deploy"}),
    )
end)

(pushed, fetched, deployed) = ran.value
assert Result.map(pushed, fn o -> o.status) == Ok(Status.Exited{code: 1})
assert fetched == Ok(Status.Exited{code: 0})
assert deployed == Err(Error.PermissionDenied{path: "deploy"})

impl Debug for Answer

fn inspect(value: Answer) -> String

impl Debug.inspect

struct Call
    program: String
    args: List<String> = []
    dir: Maybe<String> = None
    env: Map<String, String> = {}
    input: Bytes = Bytes.from_list([])
end

One start the fake responded to, or refused.

impl Debug for Call

fn inspect(value: Call) -> String

impl Debug.inspect

impl Equatable for Call

fn equal(a: Call, b: Call) -> Bool

impl Equatable.equal

struct Ran<T>
    value: T
    calls: List<Call>
end

What run returns.

impl Debug for Ran<T>

fn inspect<T>(value: Ran<T>) -> String where T: Debug

impl Debug.inspect

fn run<T>(answers: List<Answer>, body: () -> T) -> Ran<T>

Runs body with answers handling every std::process call it makes, and returns what it returned with each call. Nothing is started. If body traps, the trap propagates.

import std::process.{self, Command, Stdin}

ran = run([
    Answer{program: "wc", args: Some(["-l"]), stdout: "2\n"},
], fn ->
    process.output(Command{program: "wc", args: ["-l"], stdin: Stdin.Text("a\nb\n")})
end)

assert Result.map(ran.value, fn o -> o.stdout) == Ok(String.to_bytes("2\n"))

assert ran.calls == [
    Call{program: "wc", args: ["-l"], input: String.to_bytes("a\nb\n")},
]