mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
check existance w/o traversing symlinks (#10872)
# Description
Currently `path exists` checks the file/folder's existence by traversing
symlinks. I've added a `-n` switch/flag that disables symlink
traversing, similar to what `path expand -n` does.
## The Long Story (for those interested)
Hello! 👋 While working on one of my scripts, I discovered that the `path
exists` command was traversing symlinks. This meant that even if the
file existed, it would fail if the pointed location didn't exist. To
address this, I've introduced a new `-n` flag, which I borrowed from the
`path expand` command. This addition should make the behavior more
consistent within the *path commands universe*.
## But, is it any useful?
```nushell
let compat = /run/media/userX/DriveX/steam/steamapps/compatdata
if "symlink" == ($compat | path expand -n | path type) {}
# to this
if ($compat | path exists -n) {}
```
# User-Facing Changes
Users, will not efect. Unless they use the mentioned `-n` flag/switch.
This commit is contained in:
@ -63,3 +63,30 @@ fn const_path_exists() {
|
||||
let actual = nu!("const exists = ('~' | path exists); $exists");
|
||||
assert_eq!(actual.out, "true");
|
||||
}
|
||||
|
||||
#[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);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user