2024-06-26 01:33:37 +00:00
|
|
|
use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed};
|
2022-07-29 08:57:10 +00:00
|
|
|
use nu_test_support::playground::Playground;
|
2023-07-12 17:07:20 +00:00
|
|
|
use nu_test_support::{nu, nu_repl_code};
|
2023-04-08 18:52:37 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-11-06 13:40:29 +00:00
|
|
|
use rstest::rstest;
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_private_import_decl() {
|
|
|
|
Playground::setup("module_private_import_decl", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
use spam.nu foo-helper
|
|
|
|
|
|
|
|
export def foo [] { foo-helper }
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
def get-foo [] { "foo" }
|
|
|
|
export def foo-helper [] { get-foo }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu foo", "foo"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_private_import_alias() {
|
|
|
|
Playground::setup("module_private_import_alias", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
use spam.nu foo-helper
|
|
|
|
|
|
|
|
export def foo [] { foo-helper }
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
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 07:44:05 +00:00
|
|
|
export alias foo-helper = echo "foo"
|
2022-07-29 08:57:10 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu foo", "foo"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_private_import_decl_not_public() {
|
|
|
|
Playground::setup("module_private_import_decl_not_public", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
use spam.nu foo-helper
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
def get-foo [] { "foo" }
|
|
|
|
export def foo-helper [] { get-foo }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu foo", "foo-helper"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_public_import_decl() {
|
|
|
|
Playground::setup("module_public_import_decl", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam.nu foo
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
def foo-helper [] { "foo" }
|
|
|
|
export def foo [] { foo-helper }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu foo", "foo"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_public_import_alias() {
|
|
|
|
Playground::setup("module_public_import_alias", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam.nu foo
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
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 07:44:05 +00:00
|
|
|
export alias foo = echo "foo"
|
2022-07-29 08:57:10 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu foo", "foo"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_nested_imports() {
|
|
|
|
Playground::setup("module_nested_imports", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam2.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam2.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam3.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam3.nu",
|
|
|
|
r#"
|
|
|
|
export def foo [] { "foo" }
|
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 07:44:05 +00:00
|
|
|
export alias bar = echo "bar"
|
2022-07-29 08:57:10 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp1 = &["use main.nu foo", "foo"];
|
|
|
|
let inp2 = &["use main.nu bar", "bar"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp1.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp2.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_nested_imports_in_dirs() {
|
|
|
|
Playground::setup("module_nested_imports_in_dirs", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("spam")
|
|
|
|
.mkdir("spam/spam2")
|
|
|
|
.mkdir("spam/spam3")
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam/spam.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam2/spam2.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam2/spam2.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use ../spam3/spam3.nu [ foo bar ]
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam3/spam3.nu",
|
|
|
|
r#"
|
|
|
|
export def foo [] { "foo" }
|
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 07:44:05 +00:00
|
|
|
export alias bar = echo "bar"
|
2022-07-29 08:57:10 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp1 = &["use main.nu foo", "foo"];
|
|
|
|
let inp2 = &["use main.nu bar", "bar"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp1.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp2.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_public_import_decl_prefixed() {
|
|
|
|
Playground::setup("module_public_import_decl", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
def foo-helper [] { "foo" }
|
|
|
|
export def foo [] { foo-helper }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use main.nu", "main spam foo"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_nested_imports_in_dirs_prefixed() {
|
|
|
|
Playground::setup("module_nested_imports_in_dirs", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("spam")
|
|
|
|
.mkdir("spam/spam2")
|
|
|
|
.mkdir("spam/spam3")
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"main.nu",
|
|
|
|
r#"
|
|
|
|
export use spam/spam.nu [ "spam2 foo" "spam2 spam3 bar" ]
|
|
|
|
"#,
|
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use spam2/spam2.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam2/spam2.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-07-29 08:57:10 +00:00
|
|
|
export use ../spam3/spam3.nu
|
|
|
|
export use ../spam3/spam3.nu foo
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-07-29 08:57:10 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-07-29 08:57:10 +00:00
|
|
|
"spam/spam3/spam3.nu",
|
|
|
|
r#"
|
|
|
|
export def foo [] { "foo" }
|
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 07:44:05 +00:00
|
|
|
export alias bar = echo "bar"
|
2022-07-29 08:57:10 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp1 = &["use main.nu", "main spam2 foo"];
|
|
|
|
let inp2 = &[r#"use main.nu "spam2 spam3 bar""#, "spam2 spam3 bar"];
|
2022-07-29 08:57:10 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp1.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp2.join("; "));
|
2022-07-29 08:57:10 +00:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
})
|
|
|
|
}
|
2022-08-23 07:45:17 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-08-31 20:32:56 +00:00
|
|
|
fn module_import_env_1() {
|
2022-12-30 15:44:37 +00:00
|
|
|
Playground::setup("module_import_env_1", |dirs, sandbox| {
|
2022-08-31 20:32:56 +00:00
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 20:32:56 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-08-31 20:32:56 +00:00
|
|
|
export-env { source-env spam.nu }
|
|
|
|
|
|
|
|
export def foo [] { $env.FOO_HELPER }
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-08-31 20:32:56 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 20:32:56 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
export-env { $env.FOO_HELPER = "foo" }
|
2022-08-31 20:32:56 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["source-env main.nu", "use main.nu foo", "foo"];
|
2022-08-31 20:32:56 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-08-31 20:32:56 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_import_env_2() {
|
|
|
|
Playground::setup("module_import_env_2", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 20:32:56 +00:00
|
|
|
"main.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-08-31 20:32:56 +00:00
|
|
|
export-env { source-env spam.nu }
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-08-31 20:32:56 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 20:32:56 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
export-env { $env.FOO = "foo" }
|
2022-08-31 20:32:56 +00:00
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["source-env main.nu", "$env.FOO"];
|
2022-08-23 07:45:17 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-08-23 07:45:17 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
2022-09-04 20:19:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_cyclical_imports_0() {
|
|
|
|
Playground::setup("module_cyclical_imports_0", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use eggs.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["module eggs { use spam.nu }"];
|
2022-09-04 20:19:20 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-09-04 20:19:20 +00:00
|
|
|
|
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# 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.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
|
|
|
assert!(actual.err.contains("Module not found"));
|
2022-09-04 20:19:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_cyclical_imports_1() {
|
|
|
|
Playground::setup("module_cyclical_imports_1", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use spam.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use spam.nu"];
|
2022-09-04 20:19:20 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-09-04 20:19:20 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("cyclical"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_cyclical_imports_2() {
|
|
|
|
Playground::setup("module_cyclical_imports_2", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use eggs.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"eggs.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use spam.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use spam.nu"];
|
2022-09-04 20:19:20 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-09-04 20:19:20 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("cyclical"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_cyclical_imports_3() {
|
|
|
|
Playground::setup("module_cyclical_imports_3", |dirs, sandbox| {
|
|
|
|
sandbox
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"spam.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use eggs.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"eggs.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use bacon.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)])
|
2024-05-04 00:53:15 +00:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-09-04 20:19:20 +00:00
|
|
|
"bacon.nu",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2022-09-04 20:19:20 +00:00
|
|
|
use spam.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2022-09-04 20:19:20 +00:00
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["use spam.nu"];
|
2022-09-04 20:19:20 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-09-04 20:19:20 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("cyclical"));
|
|
|
|
})
|
|
|
|
}
|
2022-12-21 22:21:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_import_const_file() {
|
|
|
|
Playground::setup("module_import_const_file", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2022-12-21 22:21:03 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
export def foo [] { "foo" }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["const file = 'spam.nu'", "use $file foo", "foo"];
|
2022-12-21 22:21:03 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-12-21 22:21:03 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_import_const_module_name() {
|
|
|
|
Playground::setup("module_import_const_file", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2022-12-21 22:21:03 +00:00
|
|
|
"spam.nu",
|
|
|
|
r#"
|
|
|
|
export def foo [] { "foo" }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 17:07:20 +00:00
|
|
|
"const mod = 'spam'",
|
|
|
|
"use $mod foo",
|
|
|
|
"foo",
|
2022-12-21 22:21:03 +00:00
|
|
|
];
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-12-21 22:21:03 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_valid_def_name() {
|
|
|
|
let inp = &[r#"module spam { def spam [] { "spam" } }"#];
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_invalid_def_name() {
|
|
|
|
let inp = &[r#"module spam { export def spam [] { "spam" } }"#];
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("named_as_module"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_valid_alias_name_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 07:44:05 +00:00
|
|
|
let inp = &[r#"module spam { alias spam = echo "spam" }"#];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_valid_alias_name_2() {
|
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 07:44:05 +00:00
|
|
|
let inp = &[r#"module spam { alias main = echo "spam" }"#];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_invalid_alias_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 07:44:05 +00:00
|
|
|
let inp = &[r#"module spam { export alias spam = echo "spam" }"#];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("named_as_module"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_main_alias_not_allowed() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["module spam { export alias main = echo 'spam' }"];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("export_main_alias_not_allowed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_valid_known_external_name() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["module spam { extern spam [] }"];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_invalid_known_external_name() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["module spam { export extern spam [] }"];
|
2023-01-22 19:34:15 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("named_as_module"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn main_inside_module_is_main() {
|
|
|
|
let inp = &[
|
2023-07-12 17:07:20 +00:00
|
|
|
"module spam {
|
2023-01-22 19:34:15 +00:00
|
|
|
export def main [] { 'foo' };
|
|
|
|
export def foo [] { main }
|
2023-07-12 17:07:20 +00:00
|
|
|
}",
|
2023-01-22 19:34:15 +00:00
|
|
|
"use spam foo",
|
|
|
|
"foo",
|
|
|
|
];
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 19:34:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
2023-05-06 18:39:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_as_file() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["module samples/spam.nu", "use spam foo", "foo"];
|
2023-05-06 18:39:54 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn export_module_as_file() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let inp = &["export module samples/spam.nu", "use spam foo", "foo"];
|
2023-05-06 18:39:54 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deep_import_patterns() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let module_decl = "
|
2023-05-06 18:39:54 +00:00
|
|
|
module spam {
|
|
|
|
export module eggs {
|
|
|
|
export module beans {
|
|
|
|
export def foo [] { 'foo' };
|
|
|
|
export def bar [] { 'bar' }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2023-07-12 17:07:20 +00:00
|
|
|
";
|
2023-05-06 18:39:54 +00:00
|
|
|
|
|
|
|
let inp = &[module_decl, "use spam", "spam eggs beans foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[module_decl, "use spam eggs", "eggs beans foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[module_decl, "use spam eggs beans", "beans foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[module_decl, "use spam eggs beans foo", "foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
2024-11-06 13:40:29 +00:00
|
|
|
#[rstest]
|
|
|
|
fn deep_import_aliased_external_args(
|
|
|
|
#[values(
|
|
|
|
"use spam; spam eggs beans foo bar",
|
|
|
|
"use spam eggs; eggs beans foo bar",
|
|
|
|
"use spam eggs beans; beans foo bar",
|
|
|
|
"use spam eggs beans foo; foo bar"
|
|
|
|
)]
|
|
|
|
input: &str,
|
|
|
|
) {
|
|
|
|
let module_decl = "
|
|
|
|
module spam {
|
|
|
|
export module eggs {
|
|
|
|
export module beans {
|
|
|
|
export alias foo = ^echo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
let actual = nu!(format!("{module_decl}; {input}"));
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:39:54 +00:00
|
|
|
#[test]
|
|
|
|
fn module_dir() {
|
|
|
|
let import = "use samples/spam";
|
|
|
|
|
|
|
|
let inp = &[import, "spam"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
|
|
|
|
let inp = &[import, "spam foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bar"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
|
|
|
|
let inp = &[import, "spam foo baz"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "foobaz");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bar baz"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "barbaz");
|
|
|
|
|
|
|
|
let inp = &[import, "spam baz"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "spambaz");
|
|
|
|
}
|
|
|
|
|
2023-05-12 22:20:33 +00:00
|
|
|
#[test]
|
|
|
|
fn module_dir_deep() {
|
|
|
|
let import = "use samples/spam";
|
|
|
|
|
|
|
|
let inp = &[import, "spam bacon"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual_repl = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-12 22:20:33 +00:00
|
|
|
assert_eq!(actual_repl.out, "bacon");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bacon foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual_repl = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-12 22:20:33 +00:00
|
|
|
assert_eq!(actual_repl.out, "bacon foo");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bacon beans"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual_repl = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-12 22:20:33 +00:00
|
|
|
assert_eq!(actual_repl.out, "beans");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bacon beans foo"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual_repl = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-12 22:20:33 +00:00
|
|
|
assert_eq!(actual_repl.out, "beans foo");
|
|
|
|
}
|
|
|
|
|
2023-05-07 11:41:40 +00:00
|
|
|
#[test]
|
|
|
|
fn module_dir_import_twice_no_panic() {
|
|
|
|
let import = "use samples/spam";
|
|
|
|
let inp = &[import, import, "spam"];
|
|
|
|
let actual_repl = nu!(cwd: "tests/modules", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
|
|
|
|
2023-05-12 22:20:33 +00:00
|
|
|
#[test]
|
|
|
|
fn module_dir_missing_mod_nu() {
|
|
|
|
let inp = &["use samples/missing_mod_nu"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-12 22:20:33 +00:00
|
|
|
assert!(actual.err.contains("module_missing_mod_nu_file"));
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:39:54 +00:00
|
|
|
#[test]
|
|
|
|
fn allowed_local_module() {
|
|
|
|
let inp = &["module spam { module spam {} }"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_allowed_submodule() {
|
|
|
|
let inp = &["module spam { export module spam {} }"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.contains("named_as_module"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_self_name() {
|
|
|
|
let inp = &[
|
|
|
|
"module spam { export module mod { export def main [] { 'spam' } } }",
|
|
|
|
"use spam",
|
|
|
|
"spam",
|
|
|
|
];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_self_name_main_not_allowed() {
|
|
|
|
let inp = &[
|
2023-07-12 17:07:20 +00:00
|
|
|
"module spam {
|
2023-05-06 18:39:54 +00:00
|
|
|
export def main [] { 'main spam' };
|
|
|
|
|
|
|
|
export module mod {
|
|
|
|
export def main [] { 'mod spam' }
|
|
|
|
}
|
2023-07-12 17:07:20 +00:00
|
|
|
}",
|
2023-05-06 18:39:54 +00:00
|
|
|
"use spam",
|
|
|
|
"spam",
|
|
|
|
];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.contains("module_double_main"));
|
|
|
|
|
|
|
|
let inp = &[
|
2023-07-12 17:07:20 +00:00
|
|
|
"module spam {
|
2023-05-06 18:39:54 +00:00
|
|
|
export module mod {
|
|
|
|
export def main [] { 'mod spam' }
|
|
|
|
};
|
|
|
|
|
|
|
|
export def main [] { 'main spam' }
|
2023-07-12 17:07:20 +00:00
|
|
|
}",
|
2023-05-06 18:39:54 +00:00
|
|
|
"use spam",
|
|
|
|
"spam",
|
|
|
|
];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.contains("module_double_main"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn module_main_not_found() {
|
|
|
|
let inp = &["module spam {}", "use spam main"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.contains("export_not_found"));
|
|
|
|
|
|
|
|
let inp = &["module spam {}", "use spam [ main ]"];
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-05-06 18:39:54 +00:00
|
|
|
assert!(actual.err.contains("export_not_found"));
|
|
|
|
}
|
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# 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 -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# 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-08-20 12:51:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_list_export_works() {
|
|
|
|
let module = r#"
|
|
|
|
module spam {
|
|
|
|
export module eggs {
|
|
|
|
export def bacon [] { 'bacon' }
|
|
|
|
}
|
|
|
|
|
|
|
|
export def sausage [] { 'sausage' }
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let inp = &[module, "use spam [sausage eggs]", "eggs bacon"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "bacon");
|
|
|
|
}
|
2024-06-26 01:33:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reload_submodules() {
|
|
|
|
Playground::setup("reload_submodule_changed_file", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export module animals.nu"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
// should also verify something unchanged if `use voice`.
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'meow'}" | save -f animals.nu"#,
|
|
|
|
"use voice",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
// should also works if we use members directly.
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export module animals.nu"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu animals cat",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu animals cat",
|
|
|
|
"(cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn use_submodules() {
|
|
|
|
Playground::setup("use_submodules", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
// should also verify something unchanged if `use voice`.
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'meow'}" | save -f animals.nu"#,
|
|
|
|
"use voice",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
// also verify something is changed when using members.
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu cat"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu *"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu [cat]"#),
|
|
|
|
FileWithContent("animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn use_nested_submodules() {
|
|
|
|
Playground::setup("use_submodules", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu"#),
|
|
|
|
FileWithContent("animals.nu", r#"export use nested_animals.nu"#),
|
|
|
|
FileWithContent("nested_animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice animals nested_animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("voice.nu", r#"export use animals.nu"#),
|
|
|
|
FileWithContent("animals.nu", r#"export use nested_animals.nu cat"#),
|
|
|
|
FileWithContent("nested_animals.nu", "export def cat [] { 'meow'}"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"use voice.nu",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
sandbox.with_files(&[
|
|
|
|
FileWithContent("animals.nu", r#"export use nested_animals.nu cat"#),
|
|
|
|
FileWithContent("nested_animals.nu", "export def cat [] { 'meow' }"),
|
|
|
|
]);
|
|
|
|
let inp = [
|
|
|
|
"module voice { export module animals.nu }",
|
|
|
|
"use voice",
|
|
|
|
r#""export def cat [] {'woem'}" | save -f nested_animals.nu"#,
|
|
|
|
"use voice.nu",
|
|
|
|
"(voice animals cat) == 'woem'",
|
|
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
})
|
|
|
|
}
|