mirror of
https://github.com/nushell/nushell
synced 2024-11-14 00:47:09 +00:00
All field assignment into the env variable (#7099)
This commit is contained in:
parent
cb926f7b49
commit
099b571e8f
2 changed files with 34 additions and 8 deletions
|
@ -1,7 +1,10 @@
|
|||
use crate::{current_dir_str, get_full_help};
|
||||
use nu_path::expand_path_with;
|
||||
use nu_protocol::{
|
||||
ast::{Assignment, Bits, Block, Boolean, Call, Comparison, Expr, Expression, Math, Operator},
|
||||
ast::{
|
||||
Assignment, Bits, Block, Boolean, Call, Comparison, Expr, Expression, Math, Operator,
|
||||
PathMember,
|
||||
},
|
||||
engine::{EngineState, Stack, Visibility},
|
||||
Config, HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
|
||||
PipelineData, Range, RawStream, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId,
|
||||
|
@ -446,15 +449,30 @@ pub fn eval_expression(
|
|||
}
|
||||
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
|
||||
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
|
||||
let var_info = engine_state.get_var(*var_id);
|
||||
if var_info.mutable {
|
||||
let mut lhs =
|
||||
eval_expression(engine_state, stack, &cell_path.head)?;
|
||||
lhs.update_data_at_cell_path(&cell_path.tail, rhs)?;
|
||||
stack.vars.insert(*var_id, lhs);
|
||||
if var_id == &ENV_VARIABLE_ID {
|
||||
// let mut lhs =
|
||||
// eval_expression(engine_state, stack, &cell_path.head)?;
|
||||
//lhs.update_data_at_cell_path(&cell_path.tail, rhs)?;
|
||||
match &cell_path.tail[0] {
|
||||
PathMember::String { val, .. } => {
|
||||
stack.add_env_var(val.to_string(), rhs);
|
||||
}
|
||||
PathMember::Int { val, .. } => {
|
||||
stack.add_env_var(val.to_string(), rhs);
|
||||
}
|
||||
}
|
||||
Ok(Value::nothing(cell_path.head.span))
|
||||
} else {
|
||||
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
||||
let var_info = engine_state.get_var(*var_id);
|
||||
if var_info.mutable {
|
||||
let mut lhs =
|
||||
eval_expression(engine_state, stack, &cell_path.head)?;
|
||||
lhs.update_data_at_cell_path(&cell_path.tail, rhs)?;
|
||||
stack.vars.insert(*var_id, lhs);
|
||||
Ok(Value::nothing(cell_path.head.span))
|
||||
} else {
|
||||
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
||||
|
|
|
@ -77,6 +77,14 @@ fn env_shorthand_multi() {
|
|||
assert_eq!(actual.out, "barbaz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_assignment() {
|
||||
let actual = nu!(cwd: ".", r#"
|
||||
$env.FOOBAR = "barbaz"; $env.FOOBAR
|
||||
"#);
|
||||
assert_eq!(actual.out, "barbaz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn let_env_file_pwd_env_var_fails() {
|
||||
let actual = nu!(cwd: ".", r#"let-env FILE_PWD = 'foo'"#);
|
||||
|
|
Loading…
Reference in a new issue