nushell/crates/nu-command/tests/commands/alias.rs
Jakub Žádník 808e523adc
Disable alias recursion (#8397)
# Description

Prevents alias from aliasing itself. It allows a commonly requested
pattern similar to `alias ls = ls -l`.

One small issue is that the syntax highlighting is a bit off:

![alias_itself_no_color](https://user-images.githubusercontent.com/25571562/224545129-8a3ff535-347b-4a4e-b686-11493bb2a33b.png)

Fixes https://github.com/nushell/nushell/issues/8246

# User-Facing Changes

Shouldn't be a breaking change.

# 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-03-13 06:16:26 +13:00

113 lines
2.5 KiB
Rust

use nu_test_support::{nu, pipeline};
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_simple() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
alias bar = use sample_def.nu greet;
bar;
greet
"#
));
assert_eq!(actual.out, "hello");
}
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_hiding_1() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
overlay use ./activate-foo.nu;
$nu.scope.aliases | find deactivate-foo | length
"#
));
assert_eq!(actual.out, "1");
}
#[ignore = "TODO?: Aliasing parser keywords does not work anymore"]
#[test]
fn alias_hiding_2() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
overlay use ./activate-foo.nu;
deactivate-foo;
$nu.scope.aliases | find deactivate-foo | length
"#
));
assert_eq!(actual.out, "0");
}
#[test]
fn alias_fails_with_invalid_name() {
let err_msg = "name can't be a number, a filesize, or contain a hash # or caret ^";
let actual = nu!(
cwd: ".", pipeline(
r#"
alias 1234 = echo "test"
"#
));
assert!(actual.err.contains(err_msg));
let actual = nu!(
cwd: ".", pipeline(
r#"
alias 5gib = echo "test"
"#
));
assert!(actual.err.contains(err_msg));
let actual = nu!(
cwd: ".", pipeline(
r#"
alias "te#t" = echo "test"
"#
));
assert!(actual.err.contains(err_msg));
let actual = nu!(
cwd: ".", pipeline(
r#"
alias ^foo = echo "bar"
"#
));
assert!(actual.err.contains(err_msg));
}
#[test]
fn cant_alias_keyword() {
let actual = nu!(
cwd: ".", pipeline(
r#"
alias ou = let
"#
));
assert!(actual.err.contains("cant_alias_keyword"));
}
#[test]
fn alias_wont_recurse() {
let actual = nu!(
cwd: ".", pipeline(
r#"
module myspamsymbol {
export def myfoosymbol [prefix: string, msg: string] {
$prefix + $msg
}
};
use myspamsymbol myfoosymbol;
alias myfoosymbol = myfoosymbol 'hello';
myfoosymbol ' world'
"#
));
assert_eq!(actual.out, "hello world");
assert!(actual.err.is_empty());
}