2023-03-22 22:16:06 +01:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|
|
|
use nu_test_support::playground::Playground;
|
2022-03-17 23:35:50 +01:00
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
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
|
|
|
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
2022-03-17 23:35:50 +01:00
|
|
|
#[test]
|
|
|
|
fn alias_simple() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2022-08-31 22:32:56 +02:00
|
|
|
alias bar = use sample_def.nu greet;
|
|
|
|
bar;
|
|
|
|
greet
|
2022-03-17 23:35:50 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
2022-03-17 23:35:50 +01:00
|
|
|
#[test]
|
2022-08-31 22:32:56 +02:00
|
|
|
fn alias_hiding_1() {
|
2022-03-17 23:35:50 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2022-08-31 22:32:56 +02:00
|
|
|
overlay use ./activate-foo.nu;
|
|
|
|
$nu.scope.aliases | find deactivate-foo | length
|
2022-03-17 23:35:50 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
|
2022-03-17 23:35:50 +01:00
|
|
|
#[test]
|
2022-08-31 22:32:56 +02:00
|
|
|
fn alias_hiding_2() {
|
2022-03-17 23:35:50 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2022-08-31 22:32:56 +02:00
|
|
|
overlay use ./activate-foo.nu;
|
|
|
|
deactivate-foo;
|
|
|
|
$nu.scope.aliases | find deactivate-foo | length
|
2022-03-17 23:35:50 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "0");
|
|
|
|
}
|
2022-09-29 00:08:38 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_fails_with_invalid_name() {
|
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
|
|
|
let err_msg = "name can't be a number, a filesize, or contain a hash # or caret ^";
|
2023-04-02 17:25:05 +02:00
|
|
|
let actual = nu!(r#" alias 1234 = echo "test" "#);
|
|
|
|
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-12-22 21:31:34 +01:00
|
|
|
|
2023-04-02 17:25:05 +02:00
|
|
|
let actual = nu!(r#" alias 5gib = echo "test" "#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-12-22 21:31:34 +01:00
|
|
|
|
2023-04-02 17:25:05 +02:00
|
|
|
let actual = nu!(r#" alias "te#t" = echo "test" "#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
|
|
|
|
2023-04-02 17:25:05 +02:00
|
|
|
let actual = nu!(r#" alias ^foo = echo "bar" "#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-09-29 00:08:38 +02:00
|
|
|
}
|
2023-03-10 22:20:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cant_alias_keyword() {
|
2023-04-02 17:25:05 +02:00
|
|
|
let actual = nu!(r#" alias ou = let "#);
|
2023-03-10 22:20:31 +01:00
|
|
|
assert!(actual.err.contains("cant_alias_keyword"));
|
|
|
|
}
|
2023-03-12 18:16:26 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_wont_recurse() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
module myspamsymbol {
|
|
|
|
export def myfoosymbol [prefix: string, msg: string] {
|
|
|
|
$prefix + $msg
|
|
|
|
}
|
|
|
|
};
|
|
|
|
use myspamsymbol myfoosymbol;
|
|
|
|
alias myfoosymbol = myfoosymbol 'hello';
|
|
|
|
myfoosymbol ' world'
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
2023-03-22 22:16:06 +01:00
|
|
|
|
|
|
|
// Issue https://github.com/nushell/nushell/issues/8246
|
|
|
|
#[test]
|
|
|
|
fn alias_wont_recurse2() {
|
|
|
|
Playground::setup("alias_wont_recurse2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
def eggs [] { spam 'eggs' }
|
|
|
|
alias spam = spam 'spam'
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
def spam [what: string] { 'spam ' + $what };
|
|
|
|
source spam.nu;
|
|
|
|
spam
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam spam");
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
})
|
|
|
|
}
|
2023-04-06 23:05:09 +02:00
|
|
|
|
2023-04-06 23:40:53 +02:00
|
|
|
#[test]
|
|
|
|
fn alias_invalid_expression() {
|
|
|
|
let actual = nu!(r#" alias spam = 'foo' "#);
|
|
|
|
assert!(actual.err.contains("cant_alias_expression"));
|
|
|
|
|
|
|
|
let actual = nu!(r#" alias spam = ([1 2 3] | length) "#);
|
|
|
|
assert!(actual.err.contains("cant_alias_expression"));
|
|
|
|
|
|
|
|
let actual = nu!(r#" alias spam = 0..12 "#);
|
|
|
|
assert!(actual.err.contains("cant_alias_expression"));
|
|
|
|
}
|
|
|
|
|
2023-04-07 00:12:21 +02:00
|
|
|
#[test]
|
|
|
|
fn alias_if() {
|
|
|
|
let actual = nu!(r#" alias spam = if true { 'spam' } else { 'eggs' }; spam "#);
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_match() {
|
|
|
|
let actual = nu!(r#" alias spam = match 3 { 1..10 => 'yes!' }; spam "#);
|
|
|
|
assert_eq!(actual.out, "yes!");
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:40:53 +02:00
|
|
|
// Issue https://github.com/nushell/nushell/issues/8103
|
2023-04-06 23:05:09 +02:00
|
|
|
#[test]
|
|
|
|
fn alias_multiword_name() {
|
|
|
|
let actual = nu!(r#"alias `foo bar` = echo 'test'; foo bar"#);
|
|
|
|
assert_eq!(actual.out, "test");
|
|
|
|
|
|
|
|
let actual = nu!(r#"alias 'foo bar' = echo 'test'; foo bar"#);
|
|
|
|
assert_eq!(actual.out, "test");
|
|
|
|
|
|
|
|
let actual = nu!(r#"alias "foo bar" = echo 'test'; foo bar"#);
|
|
|
|
assert_eq!(actual.out, "test");
|
|
|
|
}
|