mirror of
https://github.com/nushell/nushell
synced 2025-01-13 05:38:57 +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 crate::{current_dir_str, get_full_help};
|
||||||
use nu_path::expand_path_with;
|
use nu_path::expand_path_with;
|
||||||
use nu_protocol::{
|
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},
|
engine::{EngineState, Stack, Visibility},
|
||||||
Config, HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
|
Config, HistoryFileFormat, IntoInterruptiblePipelineData, IntoPipelineData, ListStream,
|
||||||
PipelineData, Range, RawStream, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId,
|
PipelineData, Range, RawStream, ShellError, Span, Spanned, SyntaxShape, Unit, Value, VarId,
|
||||||
|
@ -446,6 +449,20 @@ pub fn eval_expression(
|
||||||
}
|
}
|
||||||
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
|
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
|
||||||
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
|
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
|
||||||
|
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 {
|
||||||
let var_info = engine_state.get_var(*var_id);
|
let var_info = engine_state.get_var(*var_id);
|
||||||
if var_info.mutable {
|
if var_info.mutable {
|
||||||
let mut lhs =
|
let mut lhs =
|
||||||
|
@ -457,6 +474,7 @@ pub fn eval_expression(
|
||||||
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
||||||
},
|
},
|
||||||
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
||||||
|
|
|
@ -77,6 +77,14 @@ fn env_shorthand_multi() {
|
||||||
assert_eq!(actual.out, "barbaz");
|
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]
|
#[test]
|
||||||
fn let_env_file_pwd_env_var_fails() {
|
fn let_env_file_pwd_env_var_fails() {
|
||||||
let actual = nu!(cwd: ".", r#"let-env FILE_PWD = 'foo'"#);
|
let actual = nu!(cwd: ".", r#"let-env FILE_PWD = 'foo'"#);
|
||||||
|
|
Loading…
Reference in a new issue