use simple_fs::SPath;
pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
#[test]
fn test_spath_starts_with_simple() -> Result<()> {
let fx_data = &[
("~/passwd", "~/", true),
("~/passwd", "~", true),
("~passwd", "~", false), ("/etc/passwd", "/etc/passwd", true),
("src/main.rs", "src/main.rs", true),
("/etc/passwd", "/etc", true),
("/etc/passwd", "/etc/", true),
("some/path/to/file", "some/path", true),
("some/path/to/file", "some/path/", true),
("/etc/passwd", "/etc/passwd/", true), ("/etc/passwd", "/etc/passwd///", true), ("/etc/passwd", "/e", false), ("/etc/passwd", "/etc/passwd.txt", false), ("src/main.rs", "src/main", false), ("file.txt", "another-file.txt", false),
("data/project/file.txt", "data/project/files", false), ("relative/path", "relative", true),
("relative/path", "relative/", true),
("./config/settings.toml", "./config", true),
("./config/settings.toml", "./config/", true),
("file", "file", true),
("file", "f", false),
("/", "/", true),
("/a/b", "/", true),
("a/b", "a", true),
("path/to/file", "path/to/file/extra", false),
];
for &(path_str, prefix_str, expected_bool) in fx_data.iter() {
let spath = SPath::new(path_str);
let prefix_path = SPath::new(prefix_str); let actual_bool = spath.starts_with(prefix_path.std_path());
assert_eq!(
actual_bool, expected_bool,
"Path: '{path_str}', Prefix: '{prefix_str}'. Expected: {expected_bool}, Got: {actual_bool}"
);
let actual_bool_str_prefix = spath.starts_with(prefix_str);
assert_eq!(
actual_bool_str_prefix, expected_bool,
"Path: '{path_str}', Prefix (str): '{prefix_str}'. Expected: {expected_bool}, Got: {actual_bool_str_prefix}"
);
}
Ok(())
}
#[test]
fn test_spath_spath_new_sibling() -> Result<()> {
let fx_data = &[
("/some/path/to/file.txt", "new_file.md", "/some/path/to/new_file.md"),
("some/path/to/file.txt", "new_file.md", "some/path/to/new_file.md"),
("/some/path/to/file.txt", "file.txt", "/some/path/to/file.txt"),
("./file.txt", "new_file.md", "./new_file.md"),
("file.txt", "new_file.md", "new_file.md"),
];
for data in fx_data.iter() {
let original_path = SPath::new(data.0);
let sibling_leaf_path = SPath::new(data.1);
let expected_path = SPath::new(data.2);
let actual_path = original_path.new_sibling(sibling_leaf_path);
assert_eq!(actual_path.as_str(), expected_path.as_str());
}
Ok(())
}
#[test]
fn test_spath_replace_prefix_simple() -> Result<()> {
let fx_data = &[
(
"/data/proj/src/main.rs",
"/data/proj/",
"/archive/v1/",
"/archive/v1/src/main.rs",
),
(
"/data/proj/src/main.rs",
"/data/proj",
"/archive/v1",
"/archive/v1/src/main.rs",
),
("src/main.rs", "src/", "lib/", "lib/main.rs"),
("src/main.rs", "src", "lib/", "lib/main.rs"),
(
"/data/proj/src/main.rs",
"/nonexistent/",
"/foo/",
"/data/proj/src/main.rs",
),
("file.txt", "", "prefix", "prefix/file.txt"),
("/file.txt", "/", "/new_root", "/new_root/file.txt"),
(
"/data/project/file.txt",
"/data/project/file.txt",
"/archive/doc.md",
"/archive/doc.md/",
),
("path/to/file", "path/to/file/extra", "new", "path/to/file"),
("project/config.toml", "project/", "", "config.toml"),
];
for &(original_str, base_str, with_str, expected_str) in fx_data.iter() {
let original_path = SPath::new(original_str);
let expected_path = SPath::new(expected_str);
let actual_path = original_path.replace_prefix(base_str, with_str);
assert_eq!(
actual_path.as_str(),
expected_path.as_str(),
"Failed for: original='{original_str}', base='{base_str}', with='{with_str}'"
);
}
Ok(())
}
#[test]
fn test_spath_into_replace_prefix_simple() -> Result<()> {
let fx_data = &[
(
"/data/proj/src/main.rs",
"/data/proj/",
"/archive/v1/",
"/archive/v1/src/main.rs",
),
(
"/data/proj/src/main.rs",
"/data/proj",
"/archive/v1",
"/archive/v1/src/main.rs",
),
("src/main.rs", "src/", "lib/", "lib/main.rs"),
("src/main.rs", "src", "lib/", "lib/main.rs"),
(
"/data/proj/src/main.rs",
"/nonexistent/",
"/foo/",
"/data/proj/src/main.rs",
),
("file.txt", "", "prefix", "prefix/file.txt"),
("/file.txt", "/", "/new_root", "/new_root/file.txt"),
(
"/data/project/file.txt",
"/data/project/file.txt",
"/archive/doc.md",
"/archive/doc.md/",
),
("path/to/file", "path/to/file/extra", "new", "path/to/file"),
("config.toml", "config.toml", "", ""),
("project/config.toml", "project/", "", "config.toml"),
];
for &(original_str, base_str, with_str, expected_str) in fx_data.iter() {
let original_path = SPath::new(original_str);
let original_path_for_check = SPath::new(original_str); let expected_path = SPath::new(expected_str);
let actual_path = original_path.into_replace_prefix(base_str, with_str);
assert_eq!(
actual_path.as_str(),
expected_path.as_str(),
"Failed for: original='{original_str}', base='{base_str}', with='{with_str}'"
);
if original_str == expected_str {
assert_eq!(actual_path.as_str(), original_path_for_check.as_str());
}
}
Ok(())
}
#[test]
fn test_spath_spath_diff() -> Result<()> {
let fx_data = &[
(
"/some/base/path",
"/some/base/path/sub_dir/some_file.md",
"sub_dir/some_file.md",
),
(
"/some/base/path/sub_dir/some_file.md",
"/some/base/path/some/other-file.md",
"../../some/other-file.md",
),
];
for data in fx_data.iter() {
let base_path = SPath::new(data.0);
let target_path = SPath::new(data.1);
let expected_path = SPath::new(data.2);
let diff_path = target_path.diff(&base_path).ok_or("Should have diff")?;
let rejoined_path = base_path.join(&diff_path).collapse();
assert_eq!(diff_path.as_str(), expected_path.as_str());
assert_eq!(rejoined_path.as_str(), target_path.as_str());
}
Ok(())
}