2022-07-29 22:42:00 +02:00
|
|
|
use nu_test_support::{nu, nu_repl_code};
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
fn env_change_hook_code_list(name: &str, code_list: &[&str]) -> String {
|
|
|
|
let mut list = String::new();
|
|
|
|
|
|
|
|
for code in code_list.iter() {
|
|
|
|
list.push_str("{ code: ");
|
|
|
|
list.push_str(code);
|
|
|
|
list.push_str(" }\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
env_change: {{
|
|
|
|
{name} : [
|
|
|
|
{list}
|
|
|
|
]
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_change_hook(name: &str, code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
env_change: {{
|
|
|
|
{name} : {code}
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_change_hook_code(name: &str, code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
env_change: {{
|
|
|
|
{name} : {{
|
|
|
|
code: {code}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_change_hook_code_condition(name: &str, condition: &str, code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
env_change: {{
|
|
|
|
{name} : {{
|
|
|
|
condition: {condition}
|
|
|
|
code: {code}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pre_prompt_hook(code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
pre_prompt: {code}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pre_prompt_hook_code(code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
pre_prompt: {{
|
|
|
|
code: {code}
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pre_execution_hook(code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
pre_execution: {code}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pre_execution_hook_code(code: &str) -> String {
|
|
|
|
format!(
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {{
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {{
|
|
|
|
pre_execution: {{
|
|
|
|
code: {code}
|
|
|
|
}}
|
|
|
|
}}
|
2023-07-12 19:07:20 +02:00
|
|
|
}}"
|
2022-07-10 12:45:46 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_define_command() {
|
|
|
|
let inp = &[
|
|
|
|
&env_change_hook_code("FOO", r#"'def foo [] { "got foo!" }'"#),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_define_variable() {
|
|
|
|
let inp = &[
|
|
|
|
&env_change_hook_code("FOO", r#"'let x = "spam"'"#),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$x",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_define_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&env_change_hook_code("FOO", r#"'$env.SPAM = "spam"'"#),
|
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_define_alias() {
|
|
|
|
let inp = &[
|
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 08:44:05 +01:00
|
|
|
&env_change_hook_code("FOO", r#"'alias spam = echo "spam"'"#),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"spam",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_simple_block_preserve_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&env_change_hook("FOO", r#"{|| $env.SPAM = "spam" }"#),
|
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_simple_block_list_shadow_env_var() {
|
|
|
|
let inp = &[
|
|
|
|
&env_change_hook(
|
|
|
|
"FOO",
|
|
|
|
r#"[
|
2023-06-30 21:57:51 +02:00
|
|
|
{|| $env.SPAM = "foo" }
|
|
|
|
{|| $env.SPAM = "spam" }
|
2022-07-14 16:09:27 +02:00
|
|
|
]"#,
|
2022-07-10 12:45:46 +02:00
|
|
|
),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_block_preserve_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&env_change_hook_code("FOO", r#"{|| $env.SPAM = "spam" }"#),
|
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_prompt_define_command() {
|
|
|
|
let inp = &[
|
|
|
|
&pre_prompt_hook_code(r#"'def foo [] { "got foo!" }'"#),
|
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_prompt_simple_block_preserve_env_var() {
|
2023-06-30 21:57:51 +02:00
|
|
|
let inp = &[&pre_prompt_hook(r#"{|| $env.SPAM = "spam" }"#), "$env.SPAM"];
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_prompt_simple_block_list_shadow_env_var() {
|
|
|
|
let inp = &[
|
|
|
|
&pre_prompt_hook(
|
|
|
|
r#"[
|
2023-06-30 21:57:51 +02:00
|
|
|
{|| $env.SPAM = "foo" }
|
|
|
|
{|| $env.SPAM = "spam" }
|
2022-07-14 16:09:27 +02:00
|
|
|
]"#,
|
2022-07-10 12:45:46 +02:00
|
|
|
),
|
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_prompt_block_preserve_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&pre_prompt_hook_code(r#"{|| $env.SPAM = "spam" }"#),
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_execution_define_command() {
|
|
|
|
let inp = &[
|
|
|
|
&pre_execution_hook_code(r#"'def foo [] { "got foo!" }'"#),
|
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_execution_simple_block_preserve_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&pre_execution_hook(r#"{|| $env.SPAM = "spam" }"#),
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_execution_simple_block_list_shadow_env_var() {
|
|
|
|
let inp = &[
|
|
|
|
&pre_execution_hook(
|
|
|
|
r#"[
|
2023-06-30 21:57:51 +02:00
|
|
|
{|| $env.SPAM = "foo" }
|
|
|
|
{|| $env.SPAM = "spam" }
|
2022-07-10 12:45:46 +02:00
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_execution_block_preserve_env_var() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
&pre_execution_hook_code(r#"{|| $env.SPAM = "spam" }"#),
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:45:35 +01:00
|
|
|
#[test]
|
|
|
|
fn pre_execution_commandline() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
&pre_execution_hook_code("{|| $env.repl_commandline = (commandline) }"),
|
2023-03-17 01:43:13 +01:00
|
|
|
"$env.repl_commandline",
|
2023-03-16 23:45:35 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2023-03-16 23:45:35 +01:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
2023-03-17 01:43:13 +01:00
|
|
|
assert_eq!(actual_repl.out, "$env.repl_commandline");
|
2023-03-16 23:45:35 +01:00
|
|
|
}
|
|
|
|
|
2022-07-10 12:45:46 +02:00
|
|
|
#[test]
|
|
|
|
fn env_change_shadow_command() {
|
|
|
|
let inp = &[
|
|
|
|
&env_change_hook_code_list(
|
|
|
|
"FOO",
|
|
|
|
&[
|
|
|
|
r#"'def foo [] { "got spam!" }'"#,
|
|
|
|
r#"'def foo [] { "got foo!" }'"#,
|
|
|
|
],
|
|
|
|
),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_block_dont_preserve_command() {
|
|
|
|
let inp = &[
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-17 13:36:28 +01:00
|
|
|
&env_change_hook_code("FOO", r#"{|| def foo [] { "foo" } }"#),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-07-10 12:45:46 +02:00
|
|
|
#[cfg(not(windows))]
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("external_command"));
|
2022-07-10 12:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_block_condition_pwd() {
|
|
|
|
let inp = &[
|
|
|
|
&env_change_hook_code_condition(
|
|
|
|
"PWD",
|
2023-07-12 19:07:20 +02:00
|
|
|
"{|before, after| ($after | path basename) == samples }",
|
|
|
|
"'source-env .nu-env'",
|
2022-07-10 12:45:46 +02:00
|
|
|
),
|
|
|
|
"cd samples",
|
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_block_condition_correct_args() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
&env_change_hook_code_condition(
|
|
|
|
"FOO",
|
2023-07-12 19:07:20 +02:00
|
|
|
"{|before, after| $before == 1 and $after == 2}",
|
|
|
|
"{|before, after| $env.SPAM = ($before == 1 and $after == 2) }",
|
2022-07-10 12:45:46 +02:00
|
|
|
),
|
|
|
|
"",
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.FOO = 2",
|
2022-07-10 12:45:46 +02:00
|
|
|
"$env.SPAM",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
|
|
assert_eq!(actual_repl.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_change_dont_panic_with_many_args() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
&env_change_hook_code("FOO", "{ |a, b, c| $env.SPAM = 'spam' }"),
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("incompatible_parameters"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_wrong_env_type_1() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: {
|
|
|
|
FOO : 1
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
}",
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2023-02-24 18:26:31 +01:00
|
|
|
dbg!(&actual_repl.err);
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_wrong_env_type_2() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: "print spam"
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("type_mismatch"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_wrong_env_type_3() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: {
|
|
|
|
FOO : {
|
|
|
|
code: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
}",
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_non_boolean_condition_output() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: {
|
|
|
|
FOO : {
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-03-17 13:36:28 +01:00
|
|
|
condition: {|| "foo" }
|
2022-07-10 12:45:46 +02:00
|
|
|
code: "print spam"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_non_condition_not_a_block() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: {
|
|
|
|
FOO : {
|
|
|
|
condition: "foo"
|
|
|
|
code: "print spam"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_parse_error() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"$env.config = {
|
2022-07-10 12:45:46 +02:00
|
|
|
hooks: {
|
|
|
|
env_change: {
|
|
|
|
FOO : {
|
|
|
|
code: "def foo { 'foo' }"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
2023-06-30 21:57:51 +02:00
|
|
|
"$env.FOO = 1",
|
2022-07-10 12:45:46 +02:00
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
assert_eq!(actual_repl.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn err_hook_dont_allow_string() {
|
2022-07-14 16:09:27 +02:00
|
|
|
let inp = &[&pre_prompt_hook(r#"'def foo [] { "got foo!" }'"#), "foo"];
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
|
|
|
assert!(actual_repl.out.is_empty());
|
2023-02-24 18:26:31 +01:00
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
2022-07-10 12:45:46 +02:00
|
|
|
}
|