oysh/crates/oyster_runtime/tests/it/builtins/mod.rs
Charlotte Meyer ac8a22ad99 refactor(runtime tests): split off builtin tests
Signed-off-by: Charlotte Meyer <dev@buffet.sh>
2022-11-02 15:41:01 +00:00

70 lines
1.6 KiB
Rust

use std::{borrow::Cow, ffi::OsStr, io::Write};
use expect_test::{expect, Expect};
use oyster_builtin_proc::builtin;
use oyster_parser::ast;
use oyster_runtime::Shell;
use crate::collect_output;
#[builtin(description = "test builtin")]
fn test_builtin(_: &mut Shell, _: &[Cow<OsStr>]) {
// XXX: this is a workaround around libtest's use of io::set_output_capture
let _ = write!(std::io::stdout(), "this is a test\n");
}
fn check(ast: &ast::Code, expect: Expect) {
let actual = collect_output(|| {
let mut shell = Shell::new().unwrap();
shell.builtins_mut().add(test_builtin);
shell.run(ast).unwrap();
});
expect.assert_debug_eq(&actual);
}
#[test]
fn simple_builtin() {
let ast = {
use oyster_parser::ast::*;
Code(vec![Statement::Pipeline(Pipeline(vec![Command(
vec![Word(vec![WordPart::Text("test_builtin")])],
Redirect::None,
)]))])
};
check(
&ast,
expect![[r#"
"this is a test\r\n"
"#]],
);
}
#[test]
fn builtin_redirection() {
let ast = {
use oyster_parser::ast::*;
Code(vec![Statement::Pipeline(Pipeline(vec![
Command(
vec![Word(vec![WordPart::Text("test_builtin")])],
Redirect::Stdout,
),
Command(
vec![
Word(vec![WordPart::Text("wc")]),
Word(vec![WordPart::Text("-c")]),
],
Redirect::None,
),
]))])
};
check(
&ast,
expect![[r#"
"15\r\n"
"#]],
);
}