nushell/src/tests/test_hiding.rs

408 lines
12 KiB
Rust
Raw Normal View History

use crate::tests::{fail_test, run_test, TestResult};
// TODO: Test the use/hide tests also as separate lines in REPL (i.e., with merging the delta in between)
#[test]
fn hides_def() -> TestResult {
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias() -> TestResult {
fail_test(
r#"alias myfoosymbol = echo "myfoosymbol"; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_env() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; hide-env myfoosymbol; $env.myfoosymbol"#,
"",
)
}
#[test]
fn hides_def_then_redefines() -> TestResult {
// this one should fail because of predecl -- cannot have more defs with the same name in a
// block
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; hide myfoosymbol; def myfoosymbol [] { "bar" }; myfoosymbol"#,
"defined more than once",
)
}
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: We'd need to make predecls work with hiding as well"]
#[test]
fn hides_alias_then_redefines() -> TestResult {
run_test(
r#"alias myfoosymbol = echo "myfoosymbol"; hide myfoosymbol; alias myfoosymbol = echo "myfoosymbol"; myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn hides_env_then_redefines() -> TestResult {
run_test(
r#"let-env myfoosymbol = "myfoosymbol"; hide-env myfoosymbol; let-env myfoosymbol = "bar"; $env.myfoosymbol"#,
"bar",
)
}
#[test]
fn hides_def_in_scope_1() -> TestResult {
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; do { hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_def_in_scope_2() -> TestResult {
run_test(
r#"def myfoosymbol [] { "myfoosymbol" }; do { def myfoosymbol [] { "bar" }; hide myfoosymbol; myfoosymbol }"#,
"myfoosymbol",
)
}
#[test]
fn hides_def_in_scope_3() -> TestResult {
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; do { hide myfoosymbol; def myfoosymbol [] { "bar" }; hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_def_in_scope_4() -> TestResult {
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; do { def myfoosymbol [] { "bar" }; hide myfoosymbol; hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_alias_in_scope_1() -> TestResult {
fail_test(
r#"alias myfoosymbol = echo "myfoosymbol"; do { hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_alias_in_scope_2() -> TestResult {
run_test(
r#"alias myfoosymbol = echo "myfoosymbol"; do { alias myfoosymbol = echo "bar"; hide myfoosymbol; myfoosymbol }"#,
"myfoosymbol",
)
}
#[test]
fn hides_alias_in_scope_3() -> TestResult {
fail_test(
r#"alias myfoosymbol = echo "myfoosymbol"; do { hide myfoosymbol; alias myfoosymbol = echo "bar"; hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_alias_in_scope_4() -> TestResult {
fail_test(
r#"alias myfoosymbol = echo "myfoosymbol"; do { alias myfoosymbol = echo "bar"; hide myfoosymbol; hide myfoosymbol; myfoosymbol }"#,
"external_command",
)
}
#[test]
fn hides_env_in_scope_1() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; do { hide-env myfoosymbol; $env.myfoosymbol }"#,
"not_found",
)
}
#[test]
fn hides_env_in_scope_2() -> TestResult {
run_test(
r#"let-env myfoosymbol = "myfoosymbol"; do { let-env myfoosymbol = "bar"; hide-env myfoosymbol; $env.myfoosymbol }"#,
"myfoosymbol",
)
}
#[test]
fn hides_env_in_scope_3() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; do { hide-env myfoosymbol; let-env myfoosymbol = "bar"; hide-env myfoosymbol; $env.myfoosymbol }"#,
"",
)
}
#[test]
fn hides_env_in_scope_4() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; do { let-env myfoosymbol = "bar"; hide-env myfoosymbol; hide-env myfoosymbol; $env.myfoosymbol }"#,
"",
)
}
#[test]
#[ignore]
fn hide_def_twice_not_allowed() -> TestResult {
fail_test(
r#"def myfoosymbol [] { "myfoosymbol" }; hide myfoosymbol; hide myfoosymbol"#,
"did not find",
)
}
#[test]
#[ignore]
fn hide_alias_twice_not_allowed() -> TestResult {
fail_test(
r#"alias myfoosymbol = echo "myfoosymbol"; hide myfoosymbol; hide myfoosymbol"#,
"did not find",
)
}
#[test]
fn hide_env_twice_not_allowed() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; hide-env myfoosymbol; hide-env myfoosymbol"#,
"",
)
}
#[test]
fn hide_env_twice_allowed() -> TestResult {
fail_test(
r#"let-env myfoosymbol = "myfoosymbol"; hide-env myfoosymbol; hide-env -i myfoosymbol; $env.myfoosymbol"#,
"",
)
}
#[test]
fn hides_def_runs_env() -> TestResult {
run_test(
r#"let-env myfoosymbol = "bar"; def myfoosymbol [] { "myfoosymbol" }; hide myfoosymbol; $env.myfoosymbol"#,
"bar",
)
}
#[test]
fn hides_def_import_1() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule; hide myspammodule myfoosymbol; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_2() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule; hide myspammodule; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_3() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule; hide myspammodule [myfoosymbol]; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_4() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule myfoosymbol; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_5() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule *; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_6() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule *; hide myspammodule *; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_def_import_then_reimports() -> TestResult {
run_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule myfoosymbol; hide myfoosymbol; use myspammodule myfoosymbol; myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn hides_alias_import_1() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule; hide myspammodule myfoosymbol; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_2() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule; hide myspammodule; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_3() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule; hide myspammodule [myfoosymbol]; myspammodule myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_4() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule myfoosymbol; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_5() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule *; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_6() -> TestResult {
fail_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule *; hide myspammodule *; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_alias_import_then_reimports() -> TestResult {
run_test(
r#"module myspammodule { export alias myfoosymbol = echo "myfoosymbol" }; use myspammodule myfoosymbol; hide myfoosymbol; use myspammodule myfoosymbol; myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn hides_env_import_1() -> TestResult {
fail_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "myfoosymbol" } }; use myspammodule; hide-env myfoosymbol; $env.myfoosymbol"#,
"",
)
}
#[test]
fn hides_def_runs_env_import() -> TestResult {
run_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "myfoosymbol" }; export def myfoosymbol [] { "bar" } }; use myspammodule myfoosymbol; hide myfoosymbol; $env.myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn hides_def_and_env_import_1() -> TestResult {
fail_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "myfoosymbol" }; export def myfoosymbol [] { "bar" } }; use myspammodule myfoosymbol; hide myfoosymbol; hide-env myfoosymbol; $env.myfoosymbol"#,
"",
)
}
#[test]
fn use_def_import_after_hide() -> TestResult {
run_test(
r#"module myspammodule { export def myfoosymbol [] { "myfoosymbol" } }; use myspammodule myfoosymbol; hide myfoosymbol; use myspammodule myfoosymbol; myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn use_env_import_after_hide() -> TestResult {
run_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "myfoosymbol" } }; use myspammodule; hide-env myfoosymbol; use myspammodule; $env.myfoosymbol"#,
"myfoosymbol",
)
}
#[test]
fn hide_shadowed_decl() -> TestResult {
run_test(
r#"module myspammodule { export def myfoosymbol [] { "bar" } }; def myfoosymbol [] { "myfoosymbol" }; do { use myspammodule myfoosymbol; hide myfoosymbol; myfoosymbol }"#,
"myfoosymbol",
)
}
#[test]
fn hide_shadowed_env() -> TestResult {
run_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "bar" } }; let-env myfoosymbol = "myfoosymbol"; do { use myspammodule; hide-env myfoosymbol; $env.myfoosymbol }"#,
"myfoosymbol",
)
}
#[test]
fn hides_all_decls_within_scope() -> TestResult {
fail_test(
r#"module myspammodule { export def myfoosymbol [] { "bar" } }; def myfoosymbol [] { "myfoosymbol" }; use myspammodule myfoosymbol; hide myfoosymbol; myfoosymbol"#,
"external_command",
)
}
#[test]
fn hides_all_envs_within_scope() -> TestResult {
fail_test(
r#"module myspammodule { export-env { let-env myfoosymbol = "bar" } }; let-env myfoosymbol = "myfoosymbol"; use myspammodule; hide-env myfoosymbol; $env.myfoosymbol"#,
"",
)
}
#[test]
fn hides_main_import_1() -> TestResult {
fail_test(
r#"module myspammodule { export def main [] { "myfoosymbol" } }; use myspammodule; hide myspammodule; myspammodule"#,
"external_command",
)
}
#[test]
fn hides_main_import_2() -> TestResult {
fail_test(
r#"module myspammodule { export def main [] { "myfoosymbol" } }; use myspammodule; hide myspammodule main; myspammodule"#,
"external_command",
)
}
#[test]
fn hides_main_import_3() -> TestResult {
fail_test(
r#"module myspammodule { export def main [] { "myfoosymbol" } }; use myspammodule; hide myspammodule [ main ]; myspammodule"#,
"external_command",
)
}
#[test]
fn hides_main_import_4() -> TestResult {
fail_test(
r#"module myspammodule { export def main [] { "myfoosymbol" } }; use myspammodule; hide myspammodule *; myspammodule"#,
"external_command",
)
}