use nu_test_support::{fs::Stub::FileWithContentToBeTrimmed, prelude::*};
#[test]
fn which_ls() -> Result {
test()
.run("which ls | get type.0")
.expect_value_eq("built-in")
}
#[test]
fn which_alias_ls() -> Result {
test()
.run("alias ls = ls -a; which ls | get path.0 | str trim")
.expect_value_eq("nu-tester-0")
}
#[test]
fn which_custom_alias() -> Result {
test()
.run(r#"alias foo = print "foo!"; which foo | to nuon"#)
.expect_value_eq(
r#"[[command, path, type, definition]; [foo, "nu-tester-0", alias, "print \"foo!\""]]"#,
)
}
#[test]
fn which_def_ls() -> Result {
test()
.run("def ls [] {echo def}; which ls | get type.0")
.expect_value_eq("custom")
}
#[test]
fn correct_precedence_alias_def_custom() -> Result {
test()
.run("def ls [] {echo def}; alias ls = echo alias; which ls | get path.0 | str trim")
.expect_value_eq("nu-tester-0")
}
#[ignore]
#[test]
fn correctly_report_of_shadowed_alias() -> Result {
let code = "
alias xaz = echo alias1
def helper [] {
alias xaz = echo alias2
which -a xaz
}
helper | get path | str contains alias2
";
test().run(code).expect_value_eq(true)
}
#[test]
fn one_report_of_multiple_defs() -> Result {
let code = "
def xaz [] { echo def1 }
def helper [] {
def xaz [] { echo def2 }
which -a xaz
}
helper | length
";
test().run(code).expect_value_eq(1)
}
#[test]
fn def_only_seen_once() -> Result {
let code = "def xaz [] {echo def1}; which -a xaz | length";
test().run(code).expect_value_eq(1)
}
#[test]
fn do_not_show_hidden_aliases() -> Result {
let code = "
alias foo = echo foo
hide foo
which foo | length
";
test().run(code).expect_value_eq(0)
}
#[test]
fn do_not_show_hidden_commands() -> Result {
let code = "
def foo [] { echo foo }
hide foo
which foo | length
";
test().run(code).expect_value_eq(0)
}
#[test]
fn which_accepts_spread_list() -> Result {
let code = "
let apps = [ls];
$apps | which ...$in | get command.0
";
test().run(code).expect_value_eq("ls")
}
#[test]
fn which_dedup_is_less_than_all() -> Result {
let all: i32 = test().run("which -a | length")?;
let dedup: i32 = test().run("which | length")?;
assert!(all >= dedup);
Ok(())
}
#[test]
fn which_custom_command_reports_file() -> Result {
Playground::setup("which_file_1", |dirs, sandbox| {
sandbox.with_files(&[FileWithContentToBeTrimmed(
"foo.nu",
"
def foo [] { echo hi }
",
)]);
let code = "
source foo.nu
which foo
";
#[derive(Debug, FromValue)]
struct Outcome {
path: String,
}
let outcome: (Outcome,) = test().cwd(dirs.test()).run(code)?;
assert_contains("foo.nu", outcome.0.path);
Ok(())
})
}
#[cfg(feature = "plugin")]
#[test]
#[deps(NU_PLUGIN_EXAMPLE)]
fn which_plugin_reports_executable() -> Result {
test()
.run("which example | get 0.path")
.expect_value_eq(NU_PLUGIN_EXAMPLE.path())
}
#[test]
#[deps(NU)]
fn which_external_command_reports_path() -> Result {
#[derive(Debug, FromValue)]
struct Outcome {
path: String,
}
let outcome: (Outcome,) = test().run("which nu")?;
assert!(!outcome.0.path.is_empty());
Ok(())
}
#[cfg(windows)]
#[test]
fn which_respects_pathext_from_env() -> Result {
Playground::setup("which_pathext", |dirs, sandbox| {
sandbox.with_files(&[FileWithContentToBeTrimmed("foo.qqq", "")]);
let dir = dirs.test().display().to_string();
let found: i32 = test().cwd(dirs.test()).run(format!(
"with-env {{ Path: '{dir}', PATHEXT: '.QQQ' }} {{ which foo | length }}"
))?;
assert_eq!(
found, 1,
"which should find foo.qqq when $env.PATHEXT has .QQQ"
);
let not_found: i32 = test().cwd(dirs.test()).run(format!(
"with-env {{ Path: '{dir}', PATHEXT: '.EXE' }} {{ which foo | length }}"
))?;
assert_eq!(
not_found, 0,
"which should not find foo.qqq when $env.PATHEXT lacks .QQQ"
);
Ok(())
})
}
#[cfg(windows)]
#[test]
fn which_respects_hidden_pathext() -> Result {
Playground::setup("which_pathext_hidden", |dirs, sandbox| {
sandbox.with_files(&[FileWithContentToBeTrimmed("foo.cmd", "")]);
let dir = dirs.test().display().to_string();
let found: i32 = test().cwd(dirs.test()).run(format!(
"with-env {{ Path: '{dir}', PATHEXT: '.CMD' }} {{ which foo | length }}"
))?;
assert_eq!(
found, 1,
"which should find foo.cmd when $env.PATHEXT has .CMD"
);
let hidden: i32 = test().cwd(dirs.test()).run(format!(
"with-env {{ Path: '{dir}', PATHEXT: '.CMD' }} {{ hide-env PATHEXT; which foo | length }}"
))?;
assert_eq!(
hidden, 0,
"which should not resurrect a hidden $env.PATHEXT from the process environment"
);
Ok(())
})
}