mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 03:44:19 +01:00
8e2917b9ae
# Description This makes assignment operations and `const` behave the same way `let` and `mut` do, absorbing the rest of the pipeline. Changes the lexer to be able to recognize assignment operators as a separate token, and then makes the lite parser continue to push spans into the same command regardless of any redirections or pipes if an assignment operator is encountered. Because the pipeline is no longer split up by the lite parser at this point, it's trivial to just parse the right hand side as if it were a subexpression not contained within parentheses. # User-Facing Changes Big breaking change. These are all now possible: ```nushell const path = 'a' | path join 'b' mut x = 2 $x = random int $x = [1 2 3] | math sum $env.FOO = random chars ``` In the past, these would have led to (an attempt at) bare word string parsing. So while `$env.FOO = bar` would have previously set the environment variable `FOO` to the string `"bar"`, it now tries to run the command named `bar`, hence the major breaking change. However, this is desirable because it is very consistent - if you see the `=`, you can just assume it absorbs everything else to the right of it. # Tests + Formatting Added tests for the new behaviour. Adjusted some existing tests that depended on the right hand side of assignments being parsed as barewords. # After Submitting - [ ] release notes (breaking change!)
123 lines
3.0 KiB
Rust
123 lines
3.0 KiB
Rust
use nu_test_support::nu;
|
|
|
|
#[test]
|
|
fn let_name_builtin_var() {
|
|
let actual = nu!("let in = 3");
|
|
|
|
assert!(actual
|
|
.err
|
|
.contains("'in' is the name of a builtin Nushell variable"));
|
|
}
|
|
|
|
#[test]
|
|
fn let_doesnt_mutate() {
|
|
let actual = nu!("let i = 3; $i = 4");
|
|
|
|
assert!(actual.err.contains("immutable"));
|
|
}
|
|
|
|
#[test]
|
|
fn let_takes_pipeline() {
|
|
let actual = nu!(r#"let x = "hello world" | str length; print $x"#);
|
|
|
|
assert_eq!(actual.out, "11");
|
|
}
|
|
|
|
#[test]
|
|
fn let_takes_pipeline_with_declared_type() {
|
|
let actual = nu!(r#"let x: list<string> = [] | append "hello world"; print $x.0"#);
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
}
|
|
|
|
#[test]
|
|
fn let_pipeline_allows_in() {
|
|
let actual =
|
|
nu!(r#"def foo [] { let x = $in | str length; print ($x + 10) }; "hello world" | foo"#);
|
|
|
|
assert_eq!(actual.out, "21");
|
|
}
|
|
|
|
#[test]
|
|
fn mut_takes_pipeline() {
|
|
let actual = nu!(r#"mut x = "hello world" | str length; print $x"#);
|
|
|
|
assert_eq!(actual.out, "11");
|
|
}
|
|
|
|
#[test]
|
|
fn mut_takes_pipeline_with_declared_type() {
|
|
let actual = nu!(r#"mut x: list<string> = [] | append "hello world"; print $x.0"#);
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
}
|
|
|
|
#[test]
|
|
fn mut_pipeline_allows_in() {
|
|
let actual =
|
|
nu!(r#"def foo [] { mut x = $in | str length; print ($x + 10) }; "hello world" | foo"#);
|
|
|
|
assert_eq!(actual.out, "21");
|
|
}
|
|
|
|
#[test]
|
|
fn let_pipeline_redirects_internals() {
|
|
let actual = nu!(r#"let x = echo 'bar'; $x | str length"#);
|
|
|
|
assert_eq!(actual.out, "3");
|
|
}
|
|
|
|
#[test]
|
|
fn let_pipeline_redirects_externals() {
|
|
let actual = nu!(r#"let x = nu --testbin cococo 'bar'; $x | str length"#);
|
|
|
|
assert_eq!(actual.out, "3");
|
|
}
|
|
|
|
#[test]
|
|
fn let_err_pipeline_redirects_externals() {
|
|
let actual = nu!(
|
|
r#"let x = with-env { FOO: "foo" } {nu --testbin echo_env_stderr FOO e>| str length}; $x"#
|
|
);
|
|
assert_eq!(actual.out, "3");
|
|
}
|
|
|
|
#[test]
|
|
fn let_outerr_pipeline_redirects_externals() {
|
|
let actual = nu!(
|
|
r#"let x = with-env { FOO: "foo" } {nu --testbin echo_env_stderr FOO o+e>| str length}; $x"#
|
|
);
|
|
assert_eq!(actual.out, "3");
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn let_with_external_failed() {
|
|
// FIXME: this test hasn't run successfully for a long time. We should
|
|
// bring it back to life at some point.
|
|
let actual = nu!(r#"let x = nu --testbin outcome_err "aa"; echo fail"#);
|
|
|
|
assert!(!actual.out.contains("fail"));
|
|
}
|
|
|
|
#[test]
|
|
fn let_glob_type() {
|
|
let actual = nu!("let x: glob = 'aa'; $x | describe");
|
|
assert_eq!(actual.out, "glob");
|
|
}
|
|
|
|
#[test]
|
|
fn let_raw_string() {
|
|
let actual = nu!(r#"let x = r#'abcde""fghi"''''jkl'#; $x"#);
|
|
assert_eq!(actual.out, r#"abcde""fghi"''''jkl"#);
|
|
|
|
let actual = nu!(r#"let x = r##'abcde""fghi"''''#jkl'##; $x"#);
|
|
assert_eq!(actual.out, r#"abcde""fghi"''''#jkl"#);
|
|
|
|
let actual = nu!(r#"let x = r###'abcde""fghi"'''##'#jkl'###; $x"#);
|
|
assert_eq!(actual.out, r#"abcde""fghi"'''##'#jkl"#);
|
|
|
|
let actual = nu!(r#"let x = r#'abc'#; $x"#);
|
|
assert_eq!(actual.out, "abc");
|
|
}
|