2020-11-24 10:18:38 +01:00
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
use super::join_path_sep;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_basename_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 basename
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_basename_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 basename --replace newname.txt
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "newname.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_basename_of_path_ending_with_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/file.txt/."
|
|
|
|
| path basename
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "file.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_basename_of_path_ending_with_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/file.txt/."
|
2023-10-19 22:07:01 +02:00
|
|
|
| path basename --replace viking.txt
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["some", "viking.txt"]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_basename_of_path_ending_with_double_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/file.txt/.."
|
|
|
|
| path basename
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_basename_of_path_ending_with_double_dot() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests", pipeline(
|
|
|
|
r#"
|
2022-02-21 14:38:15 +01:00
|
|
|
echo "some/file.txt/.."
|
2023-10-19 22:07:01 +02:00
|
|
|
| path basename --replace eggs
|
2020-11-24 10:18:38 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
let expected = join_path_sep(&["some/file.txt/..", "eggs"]);
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
}
|
2023-08-26 15:41:29 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_path_basename() {
|
|
|
|
let actual = nu!("const name = ('spam/eggs.txt' | path basename); $name");
|
|
|
|
assert_eq!(actual.out, "eggs.txt");
|
|
|
|
}
|