2022-12-30 16:44:37 +01:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, nu_repl_code, pipeline};
|
2021-02-26 21:05:22 +01:00
|
|
|
|
2023-08-17 10:58:38 +02:00
|
|
|
// Note: These tests might slightly overlap with tests/scope/mod.rs
|
|
|
|
|
2021-02-26 21:05:22 +01:00
|
|
|
#[test]
|
2021-03-13 22:46:40 +01:00
|
|
|
fn help_commands_length() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!("help commands | length");
|
2021-02-26 21:05:22 +01:00
|
|
|
|
|
|
|
let output = actual.out;
|
|
|
|
let output_int: i32 = output.parse().unwrap();
|
|
|
|
let is_positive = output_int.is_positive();
|
|
|
|
assert!(is_positive);
|
|
|
|
}
|
2022-11-20 14:22:42 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_shows_signature() {
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!("help str distance");
|
2023-07-14 20:23:21 +02:00
|
|
|
assert!(actual.out.contains("Input/output types"));
|
2022-11-20 14:22:42 +01:00
|
|
|
|
|
|
|
// don't show signature for parser keyword
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!("help alias");
|
2023-07-14 20:23:21 +02:00
|
|
|
assert!(!actual.out.contains("Input/output types"));
|
2022-11-20 14:22:42 +01:00
|
|
|
}
|
2022-12-30 16:44:37 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_aliases() {
|
|
|
|
let code = &[
|
|
|
|
"alias SPAM = print 'spam'",
|
|
|
|
"help aliases | where name == SPAM | length",
|
|
|
|
];
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-30 16:44:37 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_alias_usage_1() {
|
|
|
|
Playground::setup("help_alias_usage_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
alias SPAM = print 'spam'
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &[
|
|
|
|
"source spam.nu",
|
|
|
|
"help aliases | where name == SPAM | get 0.usage",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "line1");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_alias_usage_2() {
|
|
|
|
let code = &[
|
|
|
|
"alias SPAM = print 'spam' # line2",
|
|
|
|
"help aliases | where name == SPAM | get 0.usage",
|
|
|
|
];
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-30 16:44:37 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "line2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_alias_usage_3() {
|
|
|
|
Playground::setup("help_alias_usage_3", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
alias SPAM = print 'spam' # line2
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &[
|
|
|
|
"source spam.nu",
|
|
|
|
"help aliases | where name == SPAM | get 0.usage",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_alias_name() {
|
|
|
|
Playground::setup("help_alias_name", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
alias SPAM = print 'spam' # line2
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["source spam.nu", "help aliases SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
assert!(actual.out.contains("SPAM"));
|
|
|
|
assert!(actual.out.contains("print 'spam'"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_alias_name_f() {
|
|
|
|
Playground::setup("help_alias_name_f", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
alias SPAM = print 'spam' # line2
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["source spam.nu", "help aliases -f SPAM | get 0.usage"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_export_alias_name_single_word() {
|
|
|
|
Playground::setup("help_export_alias_name_single_word", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
export alias SPAM = print 'spam' # line2
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["use spam.nu SPAM", "help aliases SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
assert!(actual.out.contains("SPAM"));
|
|
|
|
assert!(actual.out.contains("print 'spam'"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_export_alias_name_multi_word() {
|
|
|
|
Playground::setup("help_export_alias_name_multi_word", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
export alias SPAM = print 'spam' # line2
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["use spam.nu", "help aliases spam SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
assert!(actual.out.contains("SPAM"));
|
|
|
|
assert!(actual.out.contains("print 'spam'"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_module_usage_1() {
|
|
|
|
Playground::setup("help_module_usage", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
module SPAM {
|
|
|
|
# line2
|
|
|
|
} #line3
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &[
|
|
|
|
"source spam.nu",
|
|
|
|
"help modules | where name == SPAM | get 0.usage",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
assert!(actual.out.contains("line3"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_module_name() {
|
|
|
|
Playground::setup("help_module_name", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# line1
|
|
|
|
module SPAM {
|
|
|
|
# line2
|
|
|
|
} #line3
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["source spam.nu", "help modules SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("line1"));
|
|
|
|
assert!(actual.out.contains("line2"));
|
|
|
|
assert!(actual.out.contains("line3"));
|
|
|
|
assert!(actual.out.contains("SPAM"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_module_sorted_decls() {
|
|
|
|
Playground::setup("help_module_sorted_decls", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
module SPAM {
|
|
|
|
export def z [] {}
|
|
|
|
export def a [] {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["source spam.nu", "help modules SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("a, z"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_module_sorted_aliases() {
|
|
|
|
Playground::setup("help_module_sorted_aliases", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
module SPAM {
|
2023-04-06 23:40:53 +02:00
|
|
|
export alias z = echo 'z'
|
|
|
|
export alias a = echo 'a'
|
2022-12-30 16:44:37 +01:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let code = &["source spam.nu", "help modules SPAM"];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(code));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("a, z"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
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
|
|
|
fn help_usage_extra_usage_command() {
|
|
|
|
Playground::setup("help_usage_extra_usage_command", |dirs, sandbox| {
|
2022-12-30 16:44:37 +01:00
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# module_line1
|
|
|
|
#
|
|
|
|
# module_line2
|
|
|
|
|
|
|
|
# def_line1
|
|
|
|
#
|
|
|
|
# def_line2
|
|
|
|
export def foo [] {}
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), "use spam.nu *; help modules spam");
|
2022-12-30 16:44:37 +01:00
|
|
|
assert!(actual.out.contains("module_line1"));
|
|
|
|
assert!(actual.out.contains("module_line2"));
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(),
|
2023-05-18 01:55:26 +02:00
|
|
|
"use spam.nu *; help modules | where name == spam | get 0.usage");
|
2022-12-30 16:44:37 +01:00
|
|
|
assert!(actual.out.contains("module_line1"));
|
|
|
|
assert!(!actual.out.contains("module_line2"));
|
|
|
|
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), "use spam.nu *; help commands foo");
|
2022-12-30 16:44:37 +01:00
|
|
|
assert!(actual.out.contains("def_line1"));
|
|
|
|
assert!(actual.out.contains("def_line2"));
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(),
|
2023-05-18 01:55:26 +02:00
|
|
|
"use spam.nu *; help commands | where name == foo | get 0.usage");
|
2022-12-30 16:44:37 +01:00
|
|
|
assert!(actual.out.contains("def_line1"));
|
|
|
|
assert!(!actual.out.contains("def_line2"));
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_usage_extra_usage_alias() {
|
|
|
|
Playground::setup("help_usage_extra_usage_alias", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
# module_line1
|
|
|
|
#
|
|
|
|
# module_line2
|
|
|
|
|
|
|
|
# alias_line1
|
|
|
|
#
|
|
|
|
# alias_line2
|
|
|
|
export alias bar = echo 'bar'
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline("use spam.nu *; help modules spam"));
|
|
|
|
assert!(actual.out.contains("module_line1"));
|
|
|
|
assert!(actual.out.contains("module_line2"));
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(),
|
|
|
|
pipeline("use spam.nu *; help modules | where name == spam | get 0.usage"));
|
|
|
|
assert!(actual.out.contains("module_line1"));
|
|
|
|
assert!(!actual.out.contains("module_line2"));
|
2022-12-30 16:44:37 +01:00
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline("use spam.nu *; help aliases bar"));
|
|
|
|
assert!(actual.out.contains("alias_line1"));
|
|
|
|
assert!(actual.out.contains("alias_line2"));
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(),
|
|
|
|
pipeline("use spam.nu *; help aliases | where name == bar | get 0.usage"));
|
|
|
|
assert!(actual.out.contains("alias_line1"));
|
|
|
|
assert!(!actual.out.contains("alias_line2"));
|
|
|
|
})
|
|
|
|
}
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_modules_main_1() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam {
|
|
|
|
export def main [] { 'foo' };
|
|
|
|
}"#,
|
|
|
|
"help spam",
|
|
|
|
];
|
|
|
|
|
2023-05-18 01:55:26 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains(" spam"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_modules_main_2() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam {
|
|
|
|
export def main [] { 'foo' };
|
|
|
|
}"#,
|
2023-08-17 10:58:38 +02:00
|
|
|
"help modules | where name == spam | get 0.commands.0.name",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual = nu!(pipeline(&inp.join("; ")));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
2023-02-05 23:51:09 +01:00
|
|
|
|
2023-08-08 19:10:34 +02:00
|
|
|
#[test]
|
|
|
|
fn nothing_type_annotation() {
|
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
|
|
|
def foo []: nothing -> nothing {};
|
|
|
|
help commands | where name == foo | get input_output.0.output.0
|
|
|
|
"
|
|
|
|
));
|
|
|
|
assert_eq!(actual.out, "nothing");
|
|
|
|
}
|