Skip to content

Commit

Permalink
fix: do not propogate env changes outside subshells (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 19, 2024
1 parent 5fb9d68 commit d1430a8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,15 @@ async fn execute_subshell(
)
.await;

// sub shells do not cause an exit
match result {
ExecuteResult::Exit(code, handles) => {
// sub shells do not cause an exit
ExecuteResult::Continue(code, Vec::new(), handles)
}
ExecuteResult::Continue(code, _env_changes, handles) => {
// env changes are not propagated
ExecuteResult::Continue(code, Vec::new(), handles)
}
ExecuteResult::Continue(_, _, _) => result,
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/shell/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,44 @@ async fn pwd() {
.await;
}

#[tokio::test]
async fn subshells() {
TestBuilder::new()
.command("(export TEST=1) && echo $TEST")
.assert_stdout("\n")
.assert_exit_code(0)
.run()
.await;
TestBuilder::new()
.directory("sub_dir")
.command("echo $PWD && (cd sub_dir && echo $PWD) && echo $PWD")
.assert_stdout(&format!(
"$TEMP_DIR\n$TEMP_DIR{FOLDER_SEPERATOR}sub_dir\n$TEMP_DIR\n"
))
.assert_exit_code(0)
.run()
.await;
TestBuilder::new()
.command(
"export TEST=1 && (echo $TEST && unset TEST && echo $TEST) && echo $TEST",
)
.assert_stdout("1\n\n1\n")
.assert_exit_code(0)
.run()
.await;
TestBuilder::new()
.command("(exit 1) && echo 1")
.assert_exit_code(1)
.run()
.await;
TestBuilder::new()
.command("(exit 1) || echo 1")
.assert_stdout("1\n")
.assert_exit_code(0)
.run()
.await;
}

#[tokio::test]
#[cfg(unix)]
async fn pwd_logical() {
Expand Down

0 comments on commit d1430a8

Please sign in to comment.