Skip to content

Commit

Permalink
Apply noexec option
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Nov 17, 2022
1 parent 01918e8 commit 670942b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions yash-semantics/src/command/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::ops::ControlFlow::{Break, Continue};
use std::rc::Rc;
use yash_env::io::Fd;
use yash_env::job::Pid;
use yash_env::option::Option::Exec;
use yash_env::option::State::Off;
use yash_env::semantics::Divert;
use yash_env::semantics::ExitStatus;
use yash_env::semantics::Result;
Expand Down Expand Up @@ -57,13 +59,22 @@ use yash_syntax::syntax;
/// shells. This implementation does not invert the exit status when the return
/// value is `Err(Divert::...)`, which is different from yash 2.
///
/// # `noexec` option
///
/// If the `Exec` option is `Off` in `env.options`, the entire execution of the
/// pipeline is skipped.
///
/// # Stack
///
/// if `self.negation` is true, [`Frame::Condition`] is pushed to the
/// environment's stack while the pipeline is executed.
#[async_trait(?Send)]
impl Command for syntax::Pipeline {
async fn execute(&self, env: &mut Env) -> Result {
if env.options.get(Exec) == Off {
return Continue(());
}

if !self.negation {
return execute_commands_in_pipeline(env, &self.commands).await;
}
Expand Down Expand Up @@ -252,6 +263,7 @@ mod tests {
use std::rc::Rc;
use yash_env::builtin::Builtin;
use yash_env::builtin::Type::Special;
use yash_env::option::State::Off;
use yash_env::semantics::Field;
use yash_env::system::r#virtual::FileBody;
use yash_env::system::r#virtual::ProcessState;
Expand Down Expand Up @@ -410,6 +422,17 @@ mod tests {
assert_eq!(env.exit_status, ExitStatus(15));
}

#[test]
fn noexec_option() {
let mut env = Env::new_virtual();
env.builtins.insert("return", return_builtin());
env.options.set(Exec, Off);
let pipeline: syntax::Pipeline = "return -n 93".parse().unwrap();
let result = pipeline.execute(&mut env).now_or_never().unwrap();
assert_eq!(result, Continue(()));
assert_eq!(env.exit_status, ExitStatus::SUCCESS);
}

#[test]
fn stack_without_inversion() {
fn stub_builtin(
Expand Down

0 comments on commit 670942b

Please sign in to comment.