Skip to content

Commit

Permalink
Run job-controlled asynchronous item
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Feb 19, 2023
1 parent fb15dc9 commit 0189fdf
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions yash-semantics/src/command/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use yash_env::job::Job;
use yash_env::semantics::Divert;
use yash_env::semantics::ExitStatus;
use yash_env::semantics::Result;
use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::Env;
use yash_syntax::source::Location;
Expand Down Expand Up @@ -63,10 +64,10 @@ impl Command for syntax::Item {
async fn execute_async(env: &mut Env, and_or: &Rc<AndOrList>, async_flag: &Location) -> Result {
let and_or_2 = Rc::clone(and_or);
let subshell = Subshell::new(|env| Box::pin(async move { and_or_2.execute(env).await }));
let result = subshell.start(env).await;
match result {
let subshell = subshell.job_control(JobControl::Background);
match subshell.start(env).await {
Ok((pid, job_control)) => {
debug_assert_eq!(job_control, None);
debug_assert_ne!(job_control, Some(JobControl::Foreground));
let mut job = Job::new(pid);
job.name = and_or.to_string();
env.jobs.add(job);
Expand Down Expand Up @@ -94,12 +95,16 @@ mod tests {
use crate::tests::assert_stderr;
use crate::tests::assert_stdout;
use crate::tests::echo_builtin;
use crate::tests::in_virtual_system;
use crate::tests::return_builtin;
use crate::tests::stub_tty;
use crate::tests::LocalExecutor;
use futures_util::task::LocalSpawnExt;
use futures_util::FutureExt;
use std::rc::Rc;
use yash_env::job::WaitStatus;
use yash_env::option::Option::Monitor;
use yash_env::option::State::On;
use yash_env::VirtualSystem;

#[test]
Expand Down Expand Up @@ -226,4 +231,24 @@ mod tests {
)
});
}

#[test]
fn item_execute_async_background() {
in_virtual_system(|mut env, state| async move {
env.builtins.insert("return", return_builtin());
env.options.set(Monitor, On);
stub_tty(&state);

let item = syntax::Item {
and_or: Rc::new("return -n 42".parse().unwrap()),
async_flag: Some(Location::dummy("")),
};

let _ = item.execute(&mut env).await;

let state = state.borrow();
let process = &state.processes[&env.jobs.last_async_pid()];
assert_ne!(process.pgid(), env.main_pgid);
})
}
}

0 comments on commit 0189fdf

Please sign in to comment.