2022-11-05 12:28:55 +00:00
|
|
|
mod math;
|
|
|
|
|
2022-11-02 15:33:29 +00:00
|
|
|
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#"
|
2022-11-05 13:32:38 +00:00
|
|
|
"this is a test\r\n"
|
|
|
|
"#]],
|
2022-11-02 15:33:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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#"
|
2022-11-05 13:32:38 +00:00
|
|
|
"15\r\n"
|
|
|
|
"#]],
|
2022-11-02 15:33:29 +00:00
|
|
|
);
|
|
|
|
}
|