This version of the module is deprecated: Superseded by the shell package in moonbitlang/async (import moonbitlang/async/shell): use @shell.Cmd with run, status, output and each_line, and @shell.glob. This module gets no further updates.
A small shell-free process EDSL for MoonBit
moon runwasm --experimental-policy examples/moonrun-policy.json cmd/smokemoon add bobzhang/myshell///|
import {
"bobzhang/myshell",
"moonbitlang/async",
}///|
#cfg(not(platform="windows"))
async test {
let output = Process::run(Cmd::new("printf", ["hello"]))
assert_eq(output.exit_code, 0)
assert_eq(output.stdout, "hello")
}///|
#cfg(not(platform="windows"))
async test {
let output = Process::run(Cmd::new("printf", ["%s", "$(echo no) | *"]))
assert_eq(output.stdout, "$(echo no) | *")
}///|
test {
let git = Cmd::new("git", [])
let status = git.args(["status", "--short"])
let diff = git.arg("diff")
debug_inspect(status.arguments(), content="[\"status\", \"--short\"]")
debug_inspect(diff.arguments(), content="[\"diff\"]")
debug_inspect(git.arguments(), content="[]")
}///|
#cfg(not(platform="windows"))
async test {
let plan = Pipeline::new([
Cmd::new("printf", ["alpha\nbeta\n"]),
Cmd::new("grep", ["beta"]),
Cmd::new("tr", ["a-z", "A-Z"]),
])
let output = Process::pipeline(plan)
assert_eq(output.stdout, "BETA\n")
assert_eq(output.stage_exit_codes, [0, 0, 0])
}///|
test {
let plan = Cmd::new("printf", ["hello"])
.pipe(Cmd::new("cat", []))
.pipe(Cmd::new("wc", ["-c"]))
assert_eq(plan.commands().length(), 3)
}///|
#cfg(not(platform="windows"))
async test {
let plan = Cmd::new("cat", [])
.stdin("hello")
.pipe(Cmd::new("tr", ["a-z", "A-Z"]))
let output = Process::pipeline(plan)
assert_eq(output.stdout, "HELLO")
}///|
test {
let command = Cmd::new("tool", ["check"])
.cwd("workspace")
.env("NO_COLOR", "1")
debug_inspect(command.working_directory(), content="Some(\"workspace\")")
assert_eq(command.environment()["NO_COLOR"], "1")
}///|
#cfg(not(platform="windows"))
async test {
let output = Process::run(Cmd::new("false", []))
if !output.success() {
assert_eq(output.exit_code, 1)
}
}///|
#cfg(not(platform="windows"))
async test {
let output = Process::pipeline(
Pipeline::new([Cmd::new("false", []), Cmd::new("cat", [])]),
)
assert_eq(output.exit_code, 1)
assert_eq(output.stage_exit_codes, [1, 0])
}///|
test {
let output : Output = {
exit_code: 1,
stage_exit_codes: [1],
stdout_bytes: b"",
stdout: "",
stderr_bytes: b"problem\n",
stderr: "problem\n",
stage_stderr_bytes: [b"problem\n"],
stage_stderr: ["problem\n"],
}
assert_eq(output.stage_stderr[0], "problem\n")
}///|
#cfg(not(platform="windows"))
async test {
try Process::run(Cmd::new("sleep", ["1"]), timeout=20) catch {
@async.TimeoutError => ()
_ => fail("expected TimeoutError")
} noraise {
_ => fail("expected TimeoutError")
}
}///|
let output = Process::run(command, max_output_bytes=32 * 1024 * 1024)moon run -e 'import {
"bobzhang/myshell",
"moonbitlang/async",
}
async fn main {
let output = @myshell.Process::pipeline(
@myshell.Pipeline::new([
@myshell.Cmd::new("printf", ["hello\nworld\n"]),
@myshell.Cmd::new("grep", ["world"]),
]),
)
print(output.stdout)
}'test {
let cmd = Cmd::new("git", ["status", "--short"])
inspect(cmd.program(), content="git")
debug_inspect(cmd.arguments(), content="[\"status\", \"--short\"]")
}pub enum Process {
}