nushell/tests/modules/mod.rs
Jakub Žádník a3f817d71b
Re-implement aliases (#8123)
# Description

This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.

Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.

The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).

Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754

No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)

Should use custom command instead:
https://github.com/nushell/nushell/issues/6048

# User-Facing Changes

Since aliases are now basically commands, it has some new implications:

1. `alias spam = "spam"` (requires command call)
	* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
	* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
	* TODO: Needs a better error message when attempting to do so

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 20:44:05 +13:00

564 lines
15 KiB
Rust

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn module_private_import_decl() {
Playground::setup("module_private_import_decl", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
use spam.nu foo-helper
export def foo [] { foo-helper }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def get-foo [] { "foo" }
export def foo-helper [] { get-foo }
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_private_import_alias() {
Playground::setup("module_private_import_alias", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
use spam.nu foo-helper
export def foo [] { foo-helper }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export alias foo-helper = echo "foo"
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_private_import_decl_not_public() {
Playground::setup("module_private_import_decl_not_public", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
use spam.nu foo-helper
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def get-foo [] { "foo" }
export def foo-helper [] { get-foo }
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo-helper"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(!actual.err.is_empty());
})
}
#[test]
fn module_public_import_decl() {
Playground::setup("module_public_import_decl", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam.nu foo
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def foo-helper [] { "foo" }
export def foo [] { foo-helper }
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_public_import_alias() {
Playground::setup("module_public_import_alias", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam.nu foo
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export alias foo = echo "foo"
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_nested_imports() {
Playground::setup("module_nested_imports", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export use spam2.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam2.nu",
r#"
export use spam3.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam3.nu",
r#"
export def foo [] { "foo" }
export alias bar = echo "bar"
"#,
)]);
let inp1 = &[r#"use main.nu foo"#, r#"foo"#];
let inp2 = &[r#"use main.nu bar"#, r#"bar"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
assert_eq!(actual.out, "foo");
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
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")
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam/spam.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam.nu",
r#"
export use spam2/spam2.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam2/spam2.nu",
r#"
export use ../spam3/spam3.nu [ foo bar ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam3/spam3.nu",
r#"
export def foo [] { "foo" }
export alias bar = echo "bar"
"#,
)]);
let inp1 = &[r#"use main.nu foo"#, r#"foo"#];
let inp2 = &[r#"use main.nu bar"#, r#"bar"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
assert_eq!(actual.out, "foo");
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
assert_eq!(actual.out, "bar");
})
}
#[test]
fn module_public_import_decl_prefixed() {
Playground::setup("module_public_import_decl", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def foo-helper [] { "foo" }
export def foo [] { foo-helper }
"#,
)]);
let inp = &[r#"use main.nu"#, r#"main spam foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
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")
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam/spam.nu [ "spam2 foo" "spam2 spam3 bar" ]
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam.nu",
r#"
export use spam2/spam2.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam2/spam2.nu",
r#"
export use ../spam3/spam3.nu
export use ../spam3/spam3.nu foo
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam/spam3/spam3.nu",
r#"
export def foo [] { "foo" }
export alias bar = echo "bar"
"#,
)]);
let inp1 = &[r#"use main.nu"#, r#"main spam2 foo"#];
let inp2 = &[r#"use main.nu "spam2 spam3 bar""#, r#"spam2 spam3 bar"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
assert_eq!(actual.out, "foo");
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
assert_eq!(actual.out, "bar");
})
}
#[test]
fn module_import_env_1() {
Playground::setup("module_import_env_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export-env { source-env spam.nu }
export def foo [] { $env.FOO_HELPER }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO_HELPER = "foo" }
"#,
)]);
let inp = &[r#"source-env main.nu"#, r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_import_env_2() {
Playground::setup("module_import_env_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export-env { source-env spam.nu }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO = "foo" }
"#,
)]);
let inp = &[r#"source-env main.nu"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_cyclical_imports_0() {
Playground::setup("module_cyclical_imports_0", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
use eggs.nu
"#,
)]);
let inp = &[r#"module eggs { use spam.nu }"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("module not found"));
})
}
#[test]
fn module_cyclical_imports_1() {
Playground::setup("module_cyclical_imports_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
use spam.nu
"#,
)]);
let inp = &[r#"use spam.nu"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cyclical"));
})
}
#[test]
fn module_cyclical_imports_2() {
Playground::setup("module_cyclical_imports_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
use eggs.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"eggs.nu",
r#"
use spam.nu
"#,
)]);
let inp = &[r#"use spam.nu"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cyclical"));
})
}
#[test]
fn module_cyclical_imports_3() {
Playground::setup("module_cyclical_imports_3", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
use eggs.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"eggs.nu",
r#"
use bacon.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"bacon.nu",
r#"
use spam.nu
"#,
)]);
let inp = &[r#"use spam.nu"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cyclical"));
})
}
#[test]
fn module_import_const_file() {
Playground::setup("module_import_const_file", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export def foo [] { "foo" }
"#,
)]);
let inp = &[r#"const file = 'spam.nu'"#, r#"use $file foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_import_const_module_name() {
Playground::setup("module_import_const_file", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export def foo [] { "foo" }
"#,
)]);
let inp = &[
r#"module spam { export def foo [] { "foo" } }"#,
r#"const mod = 'spam'"#,
r#"use $mod foo"#,
r#"foo"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_valid_def_name() {
let inp = &[r#"module spam { def spam [] { "spam" } }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "");
}
#[test]
fn module_invalid_def_name() {
let inp = &[r#"module spam { export def spam [] { "spam" } }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert!(actual.err.contains("named_as_module"));
}
#[test]
fn module_valid_alias_name_1() {
let inp = &[r#"module spam { alias spam = echo "spam" }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "");
}
#[test]
fn module_valid_alias_name_2() {
let inp = &[r#"module spam { alias main = echo "spam" }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "");
}
#[test]
fn module_invalid_alias_name() {
let inp = &[r#"module spam { export alias spam = echo "spam" }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert!(actual.err.contains("named_as_module"));
}
#[test]
fn module_main_alias_not_allowed() {
let inp = &[r#"module spam { export alias main = echo 'spam' }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert!(actual.err.contains("export_main_alias_not_allowed"));
}
#[test]
fn module_valid_known_external_name() {
let inp = &[r#"module spam { extern spam [] }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "");
}
#[test]
fn module_invalid_known_external_name() {
let inp = &[r#"module spam { export extern spam [] }"#];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert!(actual.err.contains("named_as_module"));
}
#[test]
fn main_inside_module_is_main() {
let inp = &[
r#"module spam {
export def main [] { 'foo' };
export def foo [] { main }
}"#,
"use spam foo",
"foo",
];
let actual = nu!(cwd: ".", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
}