nushell/tests/plugins/env.rs
Devyn Cairns 8e2917b9ae
Make assignment and const consistent with let/mut (#13385)
# 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!)
2024-07-30 18:55:22 -05:00

55 lines
1.3 KiB
Rust

use nu_test_support::nu_with_plugins;
#[test]
fn get_env_by_name() {
let result = nu_with_plugins!(
cwd: ".",
plugin: ("nu_plugin_example"),
r#"
$env.FOO = 'bar'
example env FOO | print
$env.FOO = 'baz'
example env FOO | print
"#
);
assert!(result.status.success());
assert_eq!("barbaz", result.out);
}
#[test]
fn get_envs() {
let result = nu_with_plugins!(
cwd: ".",
plugin: ("nu_plugin_example"),
"$env.BAZ = 'foo'; example env | get BAZ"
);
assert!(result.status.success());
assert_eq!("foo", result.out);
}
#[test]
fn get_current_dir() {
let cwd = std::env::current_dir()
.expect("failed to get current dir")
.join("tests")
.to_string_lossy()
.into_owned();
let result = nu_with_plugins!(
cwd: ".",
plugin: ("nu_plugin_example"),
"cd tests; example env --cwd"
);
assert!(result.status.success());
assert_eq!(cwd, result.out);
}
#[test]
fn set_env() {
let result = nu_with_plugins!(
cwd: ".",
plugin: ("nu_plugin_example"),
"example env NUSHELL_OPINION --set=rocks; $env.NUSHELL_OPINION"
);
assert!(result.status.success());
assert_eq!("rocks", result.out);
}