Skip to content

Commit

Permalink
Apply redirections to built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Oct 17, 2021
1 parent 26f19ef commit a4ae105
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions yash-semantics/src/command_impl/simple_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Command for syntax::SimpleCommand {
use crate::command_search::Target::{Builtin, External, Function};
if let Some(name) = fields.get(0) {
match search(env, &name.value) {
Some(Builtin(builtin)) => execute_builtin(env, builtin, fields).await,
Some(Builtin(builtin)) => execute_builtin(env, builtin, fields, &self.redirs).await,
Some(Function(function)) => {
execute_function(env, function, &self.assigns, &self.redirs).await
}
Expand Down Expand Up @@ -243,10 +243,17 @@ async fn execute_absent_target(
}
}

async fn execute_builtin(env: &mut Env, builtin: Builtin, fields: Vec<Field>) -> Result {
// TODO open redirections
async fn execute_builtin(
env: &mut Env,
builtin: Builtin,
fields: Vec<Field>,
redirs: &[Redir],
) -> Result {
let mut env = RedirEnv::new(env);
perform_redirs(&mut env, redirs).await?;

// TODO expand and perform assignments
let (exit_status, abort) = (builtin.execute)(env, fields).await;
let (exit_status, abort) = (builtin.execute)(&mut env, fields).await;
env.exit_status = exit_status;
abort
}
Expand Down Expand Up @@ -452,6 +459,20 @@ mod tests {
assert_eq!(env.exit_status, ExitStatus(37));
}

#[test]
fn simple_command_applies_redirections_to_builtin() {
let system = VirtualSystem::new();
let state = Rc::clone(&system.state);
let mut env = Env::with_system(Box::new(system));
env.builtins.insert("echo", echo_builtin());
let command: syntax::SimpleCommand = "echo hello >/tmp/file".parse().unwrap();
block_on(command.execute(&mut env));

let state = state.borrow();
let file = state.file_system.get("/tmp/file").unwrap().borrow();
assert_eq!(file.content, "hello\n".as_bytes());
}

#[test]
fn simple_command_returns_exit_status_from_function() {
use yash_env::function::HashEntry;
Expand Down

0 comments on commit a4ae105

Please sign in to comment.