2023-08-21 08:49:27 +00:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
//
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
// file that was distributed with this source code.
|
2023-03-20 13:51:19 +00:00
|
|
|
use crate::common::util::TestScenario;
|
2021-04-02 07:55:57 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
struct TestCase<'a> {
|
|
|
|
from: &'a str,
|
|
|
|
to: &'a str,
|
|
|
|
expected: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
const TESTS: [TestCase; 10] = [
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A",
|
|
|
|
expected: "../..",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B",
|
|
|
|
expected: "..",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B/C",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B/C/D",
|
|
|
|
expected: "D",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B/C/D/E",
|
|
|
|
expected: "D/E",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B/D",
|
|
|
|
expected: "../D",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/B/D/E",
|
|
|
|
expected: "../D/E",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/D",
|
|
|
|
expected: "../../D",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "D/E/F",
|
|
|
|
expected: "../../../D/E/F",
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
from: "A/B/C",
|
|
|
|
to: "A/D/E",
|
|
|
|
expected: "../../D/E",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
#[allow(clippy::needless_lifetimes)]
|
2021-04-02 07:55:57 +00:00
|
|
|
fn convert_path<'a>(path: &'a str) -> Cow<'a, str> {
|
|
|
|
#[cfg(windows)]
|
2022-02-24 17:33:40 +00:00
|
|
|
return path.replace('/', "\\").into();
|
2021-04-02 07:55:57 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
return path.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_relpath_with_from_no_d() {
|
2021-04-05 19:48:39 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2022-01-30 13:07:07 +00:00
|
|
|
for test in &TESTS {
|
2021-04-02 07:55:57 +00:00
|
|
|
let from: &str = &convert_path(test.from);
|
|
|
|
let to: &str = &convert_path(test.to);
|
|
|
|
let expected: &str = &convert_path(test.expected);
|
|
|
|
|
|
|
|
at.mkdir_all(to);
|
|
|
|
at.mkdir_all(from);
|
|
|
|
|
|
|
|
scene
|
|
|
|
.ucmd()
|
|
|
|
.arg(to)
|
|
|
|
.arg(from)
|
|
|
|
.succeeds()
|
2023-01-27 09:29:45 +00:00
|
|
|
.stdout_only(&format!("{expected}\n"));
|
2021-04-02 07:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_relpath_with_from_with_d() {
|
2021-04-05 19:48:39 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2022-01-30 13:07:07 +00:00
|
|
|
for test in &TESTS {
|
2021-04-02 07:55:57 +00:00
|
|
|
let from: &str = &convert_path(test.from);
|
|
|
|
let to: &str = &convert_path(test.to);
|
|
|
|
let pwd = at.as_string();
|
|
|
|
at.mkdir_all(to);
|
|
|
|
at.mkdir_all(from);
|
|
|
|
|
|
|
|
// d is part of subpath -> expect relative path
|
2021-05-02 07:42:30 +00:00
|
|
|
let mut _result_stdout = scene
|
2021-04-02 07:55:57 +00:00
|
|
|
.ucmd()
|
|
|
|
.arg(to)
|
|
|
|
.arg(from)
|
2023-01-27 09:29:45 +00:00
|
|
|
.arg(&format!("-d{pwd}"))
|
2021-04-05 19:48:39 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2021-04-02 07:55:57 +00:00
|
|
|
// relax rules for windows test environment
|
|
|
|
#[cfg(not(windows))]
|
2021-05-02 07:42:30 +00:00
|
|
|
assert!(Path::new(&_result_stdout).is_relative());
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
// d is not part of subpath -> expect absolute path
|
2021-05-02 07:42:30 +00:00
|
|
|
_result_stdout = scene
|
2021-04-05 19:48:39 +00:00
|
|
|
.ucmd()
|
|
|
|
.arg(to)
|
|
|
|
.arg(from)
|
2021-05-31 03:55:28 +00:00
|
|
|
.arg("-dnon_existing") // spell-checker:disable-line
|
2021-04-05 19:48:39 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2021-05-02 07:42:30 +00:00
|
|
|
assert!(Path::new(&_result_stdout).is_absolute());
|
2021-04-02 07:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_relpath_no_from_no_d() {
|
2021-04-05 19:48:39 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2022-01-30 13:07:07 +00:00
|
|
|
for test in &TESTS {
|
2021-04-02 07:55:57 +00:00
|
|
|
let to: &str = &convert_path(test.to);
|
|
|
|
at.mkdir_all(to);
|
|
|
|
|
2021-05-02 07:42:30 +00:00
|
|
|
let _result_stdout = scene.ucmd().arg(to).succeeds().stdout_move_str();
|
2021-04-02 07:55:57 +00:00
|
|
|
#[cfg(not(windows))]
|
2023-01-27 09:29:45 +00:00
|
|
|
assert_eq!(_result_stdout, format!("{to}\n"));
|
2021-04-02 07:55:57 +00:00
|
|
|
// relax rules for windows test environment
|
|
|
|
#[cfg(windows)]
|
2023-01-27 09:29:45 +00:00
|
|
|
assert!(_result_stdout.ends_with(&format!("{to}\n")));
|
2021-04-02 07:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_relpath_no_from_with_d() {
|
2021-04-05 19:48:39 +00:00
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
let at = &scene.fixtures;
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2022-01-30 13:07:07 +00:00
|
|
|
for test in &TESTS {
|
2021-04-02 07:55:57 +00:00
|
|
|
let to: &str = &convert_path(test.to);
|
|
|
|
let pwd = at.as_string();
|
|
|
|
at.mkdir_all(to);
|
|
|
|
|
|
|
|
// d is part of subpath -> expect relative path
|
2021-05-18 23:04:24 +00:00
|
|
|
let _result_stdout = scene
|
2021-04-05 19:48:39 +00:00
|
|
|
.ucmd()
|
|
|
|
.arg(to)
|
2023-01-27 09:29:45 +00:00
|
|
|
.arg(&format!("-d{pwd}"))
|
2021-04-05 19:48:39 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
2021-04-02 07:55:57 +00:00
|
|
|
// relax rules for windows test environment
|
|
|
|
#[cfg(not(windows))]
|
2021-05-18 23:04:24 +00:00
|
|
|
assert!(Path::new(&_result_stdout).is_relative());
|
2021-04-02 07:55:57 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
// d is not part of subpath -> expect absolute path
|
2021-05-18 23:04:24 +00:00
|
|
|
let result_stdout = scene
|
2021-04-05 19:48:39 +00:00
|
|
|
.ucmd()
|
|
|
|
.arg(to)
|
2021-05-31 03:55:28 +00:00
|
|
|
.arg("-dnon_existing") // spell-checker:disable-line
|
2021-04-05 19:48:39 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_move_str();
|
|
|
|
assert!(Path::new(&result_stdout).is_absolute());
|
2021-04-02 07:55:57 +00:00
|
|
|
}
|
|
|
|
}
|