Skip to content

Commit

Permalink
Apply errexit in multi-command pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Apr 21, 2024
1 parent 362b7c9 commit 82b206a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion yash-cli/tests/scripted_test/errexit-p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ __IN__
reached
__OUT__

: TODO yash is broken <<\__IN__
test_O -e n 'errexit: last of pipeline' -e
true | true | false
echo not reached
Expand Down
4 changes: 4 additions & 0 deletions yash-semantics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.2.0] - Unreleased

### Added

- Support for the `ErrExit` shell option in multi-command pipelines

### Changed

- `<expansion::Error as handle::Handle>::handle` now returns `Divert::Exit`
Expand Down
27 changes: 25 additions & 2 deletions yash-semantics/src/command/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,17 @@ async fn execute_commands_in_pipeline(env: &mut Env, commands: &[Rc<syntax::Comm
env.exit_status = ExitStatus::SUCCESS;
Continue(())
}

1 => commands[0].execute(env).await,
_ if env.controls_jobs() => execute_job_controlled_pipeline(env, commands).await,
_ => execute_multi_command_pipeline(env, commands).await,

_ => {
if env.controls_jobs() {
execute_job_controlled_pipeline(env, commands).await?
} else {
execute_multi_command_pipeline(env, commands).await?
}
env.apply_errexit()
}
}
}

Expand Down Expand Up @@ -322,6 +330,7 @@ mod tests {
use yash_env::builtin::Builtin;
use yash_env::builtin::Type::Special;
use yash_env::job::ProcessState;
use yash_env::option::Option::ErrExit;
use yash_env::option::Option::Monitor;
use yash_env::option::State::On;
use yash_env::semantics::Field;
Expand Down Expand Up @@ -495,6 +504,20 @@ mod tests {
assert_eq!(env.exit_status, ExitStatus::SUCCESS);
}

#[test]
fn errexit_option() {
in_virtual_system(|mut env, _state| async move {
env.builtins.insert("return", return_builtin());
env.options.set(ErrExit, On);

let pipeline: syntax::Pipeline = "return -n 0 | return -n 93".parse().unwrap();
let result = pipeline.execute(&mut env).await;

assert_eq!(result, Break(Divert::Exit(None)));
assert_eq!(env.exit_status, ExitStatus(93));
});
}

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

0 comments on commit 82b206a

Please sign in to comment.