nushell/crates/nu-command/tests/commands/which.rs
Ayush Singh ad125abf6a
fixes which showing aliases as built-in nushell commands (#9580)
fixes #8577 

# Description
Currently, using `which` on an alias describes it as a nushell built-in
command:
```bash
> alias foo = print "foo!"                                                                                                                                                                                   > which ls foo --all                                                                                                                                                                                        
╭───┬─────┬──────────────────────────┬──────────╮
│ # │ arg │           path           │ built-in │
├───┼─────┼──────────────────────────┼──────────┤
│ 0 │ ls  │ Nushell built-in command │ true     │
│ 1 │ ls  │ /bin/ls                  │ false    │
│ 2 │ foo │ Nushell built-in command │ true     │
╰───┴─────┴──────────────────────────┴──────────╯
```

This PR fixes the behaviour above to the following:
```bash
> alias foo = print "foo!"
> which ls foo --all
╭───┬─────┬──────────────────────────┬──────────╮
│ # │ arg │           path           │ built-in │
├───┼─────┼──────────────────────────┼──────────┤
│ 0 │ ls  │ Nushell built-in command │ true     │
│ 1 │ ls  │ /bin/ls                  │ false    │
│ 2 │ foo │ Nushell alias            │ false    │
╰───┴─────┴──────────────────────────┴──────────╯
```

# User-Facing Changes
Passing in an alias to `which` will no longer return `Nushell built-in
command`, `true` for `path` and `built-in` respectively.

# Tests + Formatting

# After Submitting
2023-07-08 10:48:42 +02:00

143 lines
3.1 KiB
Rust

use nu_test_support::nu;
#[test]
fn which_ls() {
let actual = nu!(
cwd: ".",
"which ls | get path.0 | str trim"
);
assert_eq!(actual.out, "Nushell built-in command");
}
#[ignore = "TODO: Can't have alias recursion"]
#[test]
fn which_alias_ls() {
let actual = nu!(
cwd: ".",
"alias ls = ls -a; which ls | get path.0 | str trim"
);
assert_eq!(actual.out, "Nushell alias: ls -a");
}
#[test]
fn which_custom_alias() {
let actual = nu!(
cwd: ".",
r#"alias foo = print "foo!"; which foo | to nuon"#
);
assert_eq!(
actual.out,
"[[arg, path, built-in]; [foo, \"Nushell alias\", false]]"
);
}
#[test]
fn which_def_ls() {
let actual = nu!(
cwd: ".",
"def ls [] {echo def}; which ls | get path.0 | str trim"
);
assert_eq!(actual.out, "Nushell custom command");
}
#[ignore = "TODO: Can't have alias with the same name as command"]
#[test]
fn correct_precedence_alias_def_custom() {
let actual = nu!(
cwd: ".",
"def ls [] {echo def}; alias ls = echo alias; which ls | get path.0 | str trim"
);
assert_eq!(actual.out, "Nushell alias: echo alias");
}
#[ignore = "TODO: Can't have alias with the same name as command"]
#[test]
fn multiple_reports_for_alias_def_custom() {
let actual = nu!(
cwd: ".",
"def ls [] {echo def}; alias ls = echo alias; which -a ls | length"
);
let length: i32 = actual.out.parse().unwrap();
assert!(length >= 2);
}
// `get_aliases_with_name` and `get_custom_commands_with_name` don't return the correct count of
// values
// I suspect this is due to the ScopeFrame getting discarded at '}' and the command is then
// executed in the parent scope
// See: parse_definition, line 2187 for reference.
#[ignore]
#[test]
fn correctly_report_of_shadowed_alias() {
let actual = nu!(
cwd: ".",
r#"alias xaz = echo alias1
def helper [] {
alias xaz = echo alias2
which -a xaz
}
helper | get path | str contains alias2"#
);
assert_eq!(actual.out, "true");
}
#[test]
fn one_report_of_multiple_defs() {
let actual = nu!(
cwd: ".",
r#"def xaz [] { echo def1 }
def helper [] {
def xaz [] { echo def2 }
which -a xaz
}
helper | length"#
);
let length: i32 = actual.out.parse().unwrap();
assert_eq!(length, 1);
}
#[test]
fn def_only_seen_once() {
let actual = nu!(
cwd: ".",
"def xaz [] {echo def1}; which -a xaz | length"
);
let length: i32 = actual.out.parse().unwrap();
assert_eq!(length, 1);
}
#[test]
fn do_not_show_hidden_aliases() {
let actual = nu!(
cwd: ".",
r#"alias foo = echo foo
hide foo
which foo | length"#
);
let length: i32 = actual.out.parse().unwrap();
assert_eq!(length, 0);
}
#[test]
fn do_not_show_hidden_commands() {
let actual = nu!(
cwd: ".",
r#"def foo [] { echo foo }
hide foo
which foo | length"#
);
let length: i32 = actual.out.parse().unwrap();
assert_eq!(length, 0);
}