2020-11-24 10:18:38 +01:00
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
use super::join_path_sep;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_dirname_of_empty_input() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo ""
|
2020-11-24 10:18:38 +01:00
|
|
|
| path dirname
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_empty_input() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo ""
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --replace newdir
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "newdir");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_dirname_of_path_ending_with_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/dir/."
|
|
|
|
| path dirname
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "some");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_path_ending_with_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/dir/."
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --replace eggs
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["eggs", "dir"]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_dirname_of_path_ending_with_double_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/dir/.."
|
|
|
|
| path dirname
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "some/dir");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_path_with_double_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/dir/.."
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --replace eggs
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["eggs", ".."]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_dirname_of_zero_levels() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "some/dir/with/spam.txt"
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --num-levels 0
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "some/dir/with/spam.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_zero_levels_with_empty_string() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "some/dir/with/spam.txt"
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --num-levels 0 --replace ""
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_more_levels() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "some/dir/with/spam.txt"
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --replace eggs -n 2
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["eggs", "with/spam.txt"]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_dirname_of_way_too_many_levels() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
|
|
|
echo "some/dir/with/spam.txt"
|
2023-10-19 22:07:01 +02:00
|
|
|
| path dirname --replace eggs -n 999
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["eggs", "some/dir/with/spam.txt"]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
2023-08-26 15:41:29 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_path_dirname() {
|
|
|
|
let actual = nu!("const name = ('spam/eggs.txt' | path dirname); $name");
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|