nushell/crates/nu-command/tests/commands/path/exists.rs
Wind 217eb4ed70
fix path exists on a non-directory file (#13763)
# Description
Fixes:  #13460

The issue is caused by `try_exists` method on path, it will return
`Err(NotADirectory)` if user tried to check for a path under a regular
file.
To fix it, I think it's ok to use `exists` rather than `try_exists`,
although
[Path::exists()](https://doc.rust-lang.org/std/path/struct.Path.html#method.exists)
only checks whether or not a path was both found and readable. I think
it's ok, and we can add this information under `extra_description`.

# User-Facing Changes
The following code will no longer raise error:
```
touch a; 'a/b' | path exists
```

# Tests + Formatting
Added 1 test.
2024-09-11 12:45:39 -05:00

105 lines
2.7 KiB
Rust

use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground;
#[test]
fn checks_if_existing_file_exists() {
Playground::setup("path_exists_1", |dirs, sandbox| {
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = nu!(
cwd: dirs.test(),
"echo spam.txt | path exists"
);
assert_eq!(actual.out, "true");
})
}
#[test]
fn checks_if_missing_file_exists() {
Playground::setup("path_exists_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo spam.txt | path exists"
);
assert_eq!(actual.out, "false");
})
}
#[test]
fn checks_if_dot_exists() {
Playground::setup("path_exists_3", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo '.' | path exists"
);
assert_eq!(actual.out, "true");
})
}
#[test]
fn checks_if_double_dot_exists() {
Playground::setup("path_exists_4", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo '..' | path exists"
);
assert_eq!(actual.out, "true");
})
}
#[test]
fn checks_tilde_relative_path_exists() {
let actual = nu!("'~' | path exists");
assert_eq!(actual.out, "true");
}
#[test]
fn const_path_exists() {
let actual = nu!("const exists = ('~' | path exists); $exists");
assert_eq!(actual.out, "true");
}
#[test]
fn path_exists_under_a_non_directory() {
Playground::setup("path_exists_6", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"touch test_file; 'test_file/aaa' | path exists"
);
assert_eq!(actual.out, "false");
assert!(actual.err.is_empty());
})
}
#[test]
fn test_check_symlink_exists() {
use nu_test_support::{nu, playground::Playground};
let symlink_target = "symlink_target";
let symlink = "symlink";
Playground::setup("path_exists_5", |dirs, sandbox| {
#[cfg(not(windows))]
std::os::unix::fs::symlink(dirs.test().join(symlink_target), dirs.test().join(symlink))
.unwrap();
#[cfg(windows)]
std::os::windows::fs::symlink_file(
dirs.test().join(symlink_target),
dirs.test().join(symlink),
)
.unwrap();
let false_out = "false".to_string();
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink_target' | path exists");
assert_eq!(false_out, shell_res.out);
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink' | path exists");
assert_eq!(false_out, shell_res.out);
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink' | path exists -n");
assert_eq!("true".to_string(), shell_res.out);
});
}