2024-08-03 10:09:13 +02:00
|
|
|
use nu_path::Path;
|
2020-11-24 10:18:38 +01:00
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expands_path_with_dot() {
|
|
|
|
Playground::setup("path_expand_1", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
|
2020-11-24 10:18:38 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "menu/./spam.txt"
|
2020-11-24 10:18:38 +01:00
|
|
|
| path expand
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = dirs.test.join("menu").join("spam.txt");
|
2024-08-03 10:09:13 +02:00
|
|
|
assert_eq!(Path::new(&actual.out), expected);
|
2020-11-24 10:18:38 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-10 15:43:56 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
#[test]
|
|
|
|
fn expands_path_without_follow_symlink() {
|
|
|
|
Playground::setup("path_expand_3", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
|
2022-08-10 15:43:56 +02:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ln -s spam.txt menu/spam_link.ln;
|
|
|
|
echo "menu/./spam_link.ln"
|
|
|
|
| path expand -n
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = dirs.test.join("menu").join("spam_link.ln");
|
2024-08-03 10:09:13 +02:00
|
|
|
assert_eq!(Path::new(&actual.out), expected);
|
2022-08-10 15:43:56 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:18:38 +01:00
|
|
|
#[test]
|
|
|
|
fn expands_path_with_double_dot() {
|
|
|
|
Playground::setup("path_expand_2", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
|
2020-11-24 10:18:38 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "menu/../menu/spam.txt"
|
2020-11-24 10:18:38 +01:00
|
|
|
| path expand
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = dirs.test.join("menu").join("spam.txt");
|
2024-08-03 10:09:13 +02:00
|
|
|
assert_eq!(Path::new(&actual.out), expected);
|
2020-11-24 10:18:38 +01:00
|
|
|
})
|
|
|
|
}
|
2021-06-20 01:07:26 +02:00
|
|
|
|
2023-08-26 15:41:29 +02:00
|
|
|
#[test]
|
|
|
|
fn const_path_expand() {
|
|
|
|
Playground::setup("const_path_expand", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
|
2023-08-26 15:41:29 +02:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
const result = ("menu/./spam.txt" | path expand);
|
|
|
|
$result
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = dirs.test.join("menu").join("spam.txt");
|
2024-08-03 10:09:13 +02:00
|
|
|
assert_eq!(Path::new(&actual.out), expected);
|
2023-08-26 15:41:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-20 01:07:26 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
mod windows {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expands_path_with_tilde_backward_slash() {
|
|
|
|
Playground::setup("path_expand_2", |dirs, _| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
echo "~\tmp.txt" | path expand
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2024-08-03 10:09:13 +02:00
|
|
|
assert!(!Path::new(&actual.out).starts_with("~"));
|
2021-06-20 01:07:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn win_expands_path_with_tilde_forward_slash() {
|
|
|
|
Playground::setup("path_expand_2", |dirs, _| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
echo "~/tmp.txt" | path expand
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2024-08-03 10:09:13 +02:00
|
|
|
assert!(!Path::new(&actual.out).starts_with("~"));
|
2021-06-20 01:07:26 +02:00
|
|
|
})
|
|
|
|
}
|
2022-08-10 15:43:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expands_path_without_follow_symlink() {
|
|
|
|
Playground::setup("path_expand_3", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
|
2022-08-10 15:43:56 +02:00
|
|
|
|
|
|
|
let cwd = dirs.test();
|
|
|
|
std::os::windows::fs::symlink_file(
|
|
|
|
cwd.join("menu").join("spam.txt"),
|
|
|
|
cwd.join("menu").join("spam_link.ln"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
echo "menu/./spam_link.ln"
|
|
|
|
| path expand -n
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = dirs.test.join("menu").join("spam_link.ln");
|
2024-08-03 10:09:13 +02:00
|
|
|
assert_eq!(Path::new(&actual.out), expected);
|
2022-08-10 15:43:56 +02:00
|
|
|
})
|
|
|
|
}
|
2021-06-20 01:07:26 +02:00
|
|
|
}
|