Skip to content

Commit

Permalink
Added support for default value assign with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Oct 9, 2024
1 parent b30bae6 commit fc2ea1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 16 additions & 5 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,13 +1210,13 @@ impl From<miette::Error> for EvaluateWordTextError {
impl VariableModifier {
pub async fn apply(
&self,
variable: Option<&String>,
name: &str,
state: &mut ShellState,
stdin: ShellPipeReader,
stderr: ShellPipeWriter,
) -> Result<(Text, Option<Vec<EnvChange>>), miette::Report> {
match self {
VariableModifier::DefaultValue(default_value) => match variable {
VariableModifier::DefaultValue(default_value) => match state.get_var(name) {
Some(v) => Ok((v.clone().into(), None)),
None => {
let v = evaluate_word(default_value.clone(), state, stdin, stderr)
Expand All @@ -1225,6 +1225,18 @@ impl VariableModifier {
Ok((v.value.into(), Some(v.changes)))
}
},
VariableModifier::AssignDefault(default_value) => match state.get_var(name) {
Some(v) => Ok((v.clone().into(), None)),
None => {
let v = evaluate_word(default_value.clone(), state, stdin, stderr)
.await
.into_diagnostic()?;
state.apply_env_var(name, &v.value);
let mut changes = v.changes;
changes.push(EnvChange::SetShellVar(name.to_string(), v.value.clone()));
Ok((v.value.into(), Some(changes)))
}
},
// VariableModifier::Substring { begin, length } => {
// if variable.is_none() {
// return Err(miette::miette!("Variable not found"));
Expand Down Expand Up @@ -1365,16 +1377,15 @@ fn evaluate_word_parts(
continue;
}
WordPart::Variable(name, modifier) => {
let value = state.get_var(&name).map(|v| v.to_string());
if let Some(modifier) = modifier {
let (text, env_changes) = modifier
.apply(value.as_ref(), state, stdin.clone(), stderr.clone())
.apply(&name, state, stdin.clone(), stderr.clone())
.await?;
if let Some(env_changes) = env_changes {
result.with_changes(env_changes);
}
Ok(Some(text))
} else if let Some(val) = value {
} else if let Some(val) = state.get_var(&name).map(|v| v.to_string()) {
Ok(Some(val.into()))
} else {
Err(miette::miette!("Undefined variable: {}", name))
Expand Down
21 changes: 21 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ async fn touch() {

#[tokio::test]
async fn variable_expansion() {
// DEFAULT VALUE EXPANSION
TestBuilder::new()
.command("echo ${FOO:-5}")
.assert_stdout("5\n")
Expand All @@ -1089,6 +1090,26 @@ async fn variable_expansion() {
.assert_stdout("2\n")
.run()
.await;

// ASSIGN DEFAULT EXPANSION
TestBuilder::new()
.command("echo ${FOO:=5} && echo $FOO")
.assert_stdout("5\n5\n")
.run()
.await;

TestBuilder::new()
.command(r#"FOO=1 && echo ${FOO:=5} && echo $FOO"#)
.assert_stdout("1\n1\n")
.run()
.await;

TestBuilder::new()
.command(r#"echo ${FOO:=${BAR:=5}} && echo $FOO && echo $BAR"#)
.assert_stdout("5\n5\n5\n")
.run()
.await;

}

#[cfg(test)]
Expand Down

0 comments on commit fc2ea1b

Please sign in to comment.