Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement exit status var #110

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.75.0"
channel = "1.76.0"
components = ["clippy", "rustfmt"]
15 changes: 7 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,9 @@ fn parse_word_parts(
preceded(ch('$'), |input| {
if let Some(char) = input.chars().next() {
// $$ - process id
// $? - last exit code
// $# - number of arguments in $*
// $* - list of arguments passed to the current process
if "$?#*".contains(char) {
if "$#*".contains(char) {
return ParseError::fail(
input,
format!("${char} is currently not supported."),
Expand Down Expand Up @@ -753,7 +752,10 @@ fn parse_word_parts(
}

let (input, parts) = many0(or7(
map(first_escaped_char(mode), PendingPart::Char),
or(
map(tag("$?"), |_| PendingPart::Variable("?")),
map(first_escaped_char(mode), PendingPart::Char),
),
map(parse_command_substitution, PendingPart::Command),
map(preceded(ch('$'), parse_env_var_name), PendingPart::Variable),
|input| {
Expand Down Expand Up @@ -1419,11 +1421,6 @@ mod test {
"$$",
Err("$$ is currently not supported."),
);
run_test(
parse_unquoted_word,
"$?",
Err("$? is currently not supported."),
);
run_test(
parse_unquoted_word,
"$#",
Expand Down Expand Up @@ -1458,6 +1455,7 @@ mod test {
run_test(parse_u32, "4294967296", Err("backtrace"));
}

#[track_caller]
fn run_test<'a, T: PartialEq + std::fmt::Debug>(
combinator: impl Fn(&'a str) -> ParseResult<'a, T>,
input: &'a str,
Expand All @@ -1466,6 +1464,7 @@ mod test {
run_test_with_end(combinator, input, expected, "");
}

#[track_caller]
fn run_test_with_end<'a, T: PartialEq + std::fmt::Debug>(
combinator: impl Fn(&'a str) -> ParseResult<'a, T>,
input: &'a str,
Expand Down
2 changes: 2 additions & 0 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ fn execute_sequential_list(
}
ExecuteResult::Continue(exit_code, changes, handles) => {
state.apply_changes(&changes);
state.apply_env_var("?", &exit_code.to_string());
final_changes.extend(changes);
async_handles.extend(handles);
// use the final sequential item's exit code
Expand Down Expand Up @@ -258,6 +259,7 @@ fn execute_sequence(
let (exit_code, mut async_handles) = match first_result {
ExecuteResult::Exit(_, _) => return first_result,
ExecuteResult::Continue(exit_code, sub_changes, async_handles) => {
state.apply_env_var("?", &exit_code.to_string());
state.apply_changes(&sub_changes);
changes.extend(sub_changes);
(exit_code, async_handles)
Expand Down
24 changes: 24 additions & 0 deletions src/shell/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,30 @@ async fn env_variables() {
.await;
}

#[tokio::test]
async fn exit_code_var() {
TestBuilder::new()
.command(r#"echo $? ; echo $? ; false ; echo $?"#)
.assert_stdout("\n0\n1\n")
.run()
.await;
TestBuilder::new()
.command(r#"(false || echo $?) && echo $?"#)
.assert_stdout("1\n0\n")
.run()
.await;
TestBuilder::new()
.command(r#"! false && echo $?"#)
.assert_stdout("0\n")
.run()
.await;
TestBuilder::new()
.command(r#"(deno eval 'Deno.exit(25)') || echo $?"#)
.assert_stdout("25\n")
.run()
.await;
}

#[tokio::test]
async fn sequential_lists() {
TestBuilder::new()
Expand Down
Loading