nushell/tests/hooks/mod.rs
Jakub Žádník a3f817d71b
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 20:44:05 +13:00

552 lines
12 KiB
Rust

use nu_test_support::{nu, nu_repl_code};
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!(
r#"let-env config = {{
hooks: {{
env_change: {{
{name} : [
{list}
]
}}
}}
}}"#
)
}
fn env_change_hook(name: &str, code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
env_change: {{
{name} : {code}
}}
}}
}}"#
)
}
fn env_change_hook_code(name: &str, code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
env_change: {{
{name} : {{
code: {code}
}}
}}
}}
}}"#
)
}
fn env_change_hook_code_condition(name: &str, condition: &str, code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
env_change: {{
{name} : {{
condition: {condition}
code: {code}
}}
}}
}}
}}"#
)
}
fn pre_prompt_hook(code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
pre_prompt: {code}
}}
}}"#
)
}
fn pre_prompt_hook_code(code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
pre_prompt: {{
code: {code}
}}
}}
}}"#
)
}
fn pre_execution_hook(code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
pre_execution: {code}
}}
}}"#
)
}
fn pre_execution_hook_code(code: &str) -> String {
format!(
r#"let-env config = {{
hooks: {{
pre_execution: {{
code: {code}
}}
}}
}}"#
)
}
#[test]
fn env_change_define_command() {
let inp = &[
&env_change_hook_code("FOO", r#"'def foo [] { "got foo!" }'"#),
"let-env FOO = 1",
"foo",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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"'"#),
"let-env FOO = 1",
"$x",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_define_env_var() {
let inp = &[
&env_change_hook_code("FOO", r#"'let-env SPAM = "spam"'"#),
"let-env FOO = 1",
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_define_alias() {
let inp = &[
&env_change_hook_code("FOO", r#"'alias spam = echo "spam"'"#),
"let-env FOO = 1",
"spam",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_simple_block_preserve_env_var() {
let inp = &[
&env_change_hook("FOO", r#"{ let-env SPAM = "spam" }"#),
"let-env FOO = 1",
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
]"#,
),
"let-env FOO = 1",
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_block_preserve_env_var() {
let inp = &[
&env_change_hook_code("FOO", r#"{ let-env SPAM = "spam" }"#),
"let-env FOO = 1",
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "got foo!");
}
#[test]
fn pre_prompt_simple_block_preserve_env_var() {
let inp = &[
&pre_prompt_hook(r#"{ let-env SPAM = "spam" }"#),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
]"#,
),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn pre_prompt_block_preserve_env_var() {
let inp = &[
&pre_prompt_hook_code(r#"{ let-env SPAM = "spam" }"#),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "got foo!");
}
#[test]
fn pre_execution_simple_block_preserve_env_var() {
let inp = &[
&pre_execution_hook(r#"{ let-env SPAM = "spam" }"#),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
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#"[
{ let-env SPAM = "foo" }
{ let-env SPAM = "spam" }
]"#,
),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn pre_execution_block_preserve_env_var() {
let inp = &[
&pre_execution_hook_code(r#"{ let-env SPAM = "spam" }"#),
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_shadow_command() {
let inp = &[
&env_change_hook_code_list(
"FOO",
&[
r#"'def foo [] { "got spam!" }'"#,
r#"'def foo [] { "got foo!" }'"#,
],
),
"let-env FOO = 1",
"foo",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "got foo!");
}
#[test]
fn env_change_block_dont_preserve_command() {
let inp = &[
&env_change_hook_code("FOO", r#"{ def foo [] { "foo" } }"#),
"let-env FOO = 1",
"foo",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
#[cfg(windows)]
assert_ne!(actual_repl.out, "foo");
#[cfg(not(windows))]
assert!(actual_repl.err.contains("external_command"));
}
#[test]
fn env_change_block_condition_pwd() {
let inp = &[
&env_change_hook_code_condition(
"PWD",
r#"{|before, after| ($after | path basename) == samples }"#,
r#"'source-env .nu-env'"#,
),
"cd samples",
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "spam");
}
#[test]
fn env_change_block_condition_correct_args() {
let inp = &[
r#"let-env FOO = 1"#,
&env_change_hook_code_condition(
"FOO",
r#"{|before, after| $before == 1 and $after == 2}"#,
r#"{|before, after| let-env SPAM = ($before == 1 and $after == 2) }"#,
),
"",
r#"let-env FOO = 2"#,
"$env.SPAM",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert_eq!(actual_repl.err, "");
assert_eq!(actual_repl.out, "true");
}
#[test]
fn env_change_dont_panic_with_many_args() {
let inp = &[
&env_change_hook_code("FOO", r#"{ |a, b, c| let-env SPAM = 'spam' }"#),
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("incompatible_parameters"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_wrong_env_type_1() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: {
FOO : 1
}
}
}"#,
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
dbg!(&actual_repl.err);
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_wrong_env_type_2() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: "print spam"
}
}"#,
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("type_mismatch"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_wrong_env_type_3() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: {
FOO : {
code: 1
}
}
}
}"#,
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_non_boolean_condition_output() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: {
FOO : {
condition: { "foo" }
code: "print spam"
}
}
}
}"#,
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_non_condition_not_a_block() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: {
FOO : {
condition: "foo"
code: "print spam"
}
}
}
}"#,
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_parse_error() {
let inp = &[
r#"let-env config = {
hooks: {
env_change: {
FOO : {
code: "def foo { 'foo' }"
}
}
}
}"#,
"let-env FOO = 1",
"",
];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.err.contains("unsupported_config_value"));
assert_eq!(actual_repl.out, "");
}
#[test]
fn err_hook_dont_allow_string() {
let inp = &[&pre_prompt_hook(r#"'def foo [] { "got foo!" }'"#), "foo"];
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
assert!(actual_repl.out.is_empty());
assert!(actual_repl.err.contains("unsupported_config_value"));
}