mirror of
https://github.com/awfixers-stuff/src.git
synced 2026-03-27 04:55:59 +00:00
create src
This commit is contained in:
100
src-path/tests/path/convert/mod.rs
Normal file
100
src-path/tests/path/convert/mod.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use bstr::ByteSlice;
|
||||
use gix_path::{to_unix_separators, to_windows_separators};
|
||||
|
||||
#[test]
|
||||
fn assure_unix_separators() {
|
||||
assert_eq!(to_unix_separators(b"no-backslash".as_bstr()).as_bstr(), "no-backslash");
|
||||
|
||||
assert_eq!(to_unix_separators(br"\a\b\\".as_bstr()).as_bstr(), "/a/b//");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assure_windows_separators() {
|
||||
assert_eq!(
|
||||
to_windows_separators(b"no-backslash".as_bstr()).as_bstr(),
|
||||
"no-backslash"
|
||||
);
|
||||
|
||||
assert_eq!(to_windows_separators(b"/a/b//".as_bstr()).as_bstr(), r"\a\b\\");
|
||||
}
|
||||
|
||||
mod normalize;
|
||||
|
||||
mod join_bstr_unix_pathsep {
|
||||
use bstr::BStr;
|
||||
use gix_path::join_bstr_unix_pathsep;
|
||||
|
||||
fn b(s: &str) -> &BStr {
|
||||
s.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typical_with_double_slash_avoidance() {
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base"), "path"), b("base/path"));
|
||||
assert_eq!(
|
||||
join_bstr_unix_pathsep(b("base/"), "path"),
|
||||
b("base/path"),
|
||||
"no double slashes"
|
||||
);
|
||||
assert_eq!(join_bstr_unix_pathsep(b("/base"), "path"), b("/base/path"));
|
||||
assert_eq!(join_bstr_unix_pathsep(b("/base/"), "path"), b("/base/path"));
|
||||
}
|
||||
#[test]
|
||||
fn relative_base_or_path_are_nothing_special() {
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base"), "."), b("base/."));
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base"), ".."), b("base/.."));
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base"), "../dir"), b("base/../dir"));
|
||||
}
|
||||
#[test]
|
||||
fn absolute_path_produces_double_slashes() {
|
||||
assert_eq!(join_bstr_unix_pathsep(b("/base"), "/root"), b("/base//root"));
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base/"), "/root"), b("base//root"));
|
||||
}
|
||||
#[test]
|
||||
fn empty_path_makes_base_end_with_a_slash() {
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base"), ""), b("base/"));
|
||||
assert_eq!(join_bstr_unix_pathsep(b("base/"), ""), b("base/"));
|
||||
}
|
||||
#[test]
|
||||
fn empty_base_leaves_everything_untouched() {
|
||||
assert_eq!(join_bstr_unix_pathsep(b(""), ""), b(""));
|
||||
assert_eq!(join_bstr_unix_pathsep(b(""), "hi"), b("hi"));
|
||||
assert_eq!(join_bstr_unix_pathsep(b(""), "/hi"), b("/hi"));
|
||||
}
|
||||
}
|
||||
|
||||
mod relativize_with_prefix {
|
||||
fn r(path: &str, prefix: &str) -> String {
|
||||
gix_path::to_unix_separators_on_windows(
|
||||
gix_path::os_str_into_bstr(gix_path::relativize_with_prefix(path.as_ref(), prefix.as_ref()).as_os_str())
|
||||
.expect("no illformed UTF-8"),
|
||||
)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basics() {
|
||||
assert_eq!(
|
||||
r("a", "a"),
|
||||
".",
|
||||
"reaching the prefix is signalled by a '.', the current dir"
|
||||
);
|
||||
assert_eq!(r("a/b/c", "a/b"), "c", "'c' is clearly within the current directory");
|
||||
assert_eq!(
|
||||
r("c/b/c", "a/b"),
|
||||
"../../c/b/c",
|
||||
"when there is a complete disjoint prefix, we have to get out of it with ../"
|
||||
);
|
||||
assert_eq!(
|
||||
r("a/a", "a/b"),
|
||||
"../a",
|
||||
"when there is mismatch, we have to get out of the CWD"
|
||||
);
|
||||
assert_eq!(
|
||||
r("a/a", ""),
|
||||
"a/a",
|
||||
"empty prefix means nothing happens (and no work is done)"
|
||||
);
|
||||
assert_eq!(r("", ""), "", "empty stays empty");
|
||||
}
|
||||
}
|
||||
147
src-path/tests/path/convert/normalize.rs
Normal file
147
src-path/tests/path/convert/normalize.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
use std::{borrow::Cow, path::Path};
|
||||
|
||||
use gix_path::normalize;
|
||||
|
||||
fn p(input: &str) -> &Path {
|
||||
Path::new(input)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_change_if_there_are_no_trailing_relative_components() {
|
||||
for input in ["./a/b/c/d", "/absolute/path", r"C:\hello\world"] {
|
||||
let path = p(input);
|
||||
assert_eq!(normalize(path.into(), &std::env::current_dir().unwrap()).unwrap(), path);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn special_cases_around_cwd() -> crate::Result {
|
||||
let cwd = std::env::current_dir()?;
|
||||
assert_eq!(
|
||||
normalize(p("./../../.git/modules/src/llvm-project").into(), &cwd).unwrap(),
|
||||
cwd.parent()
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join(".git/modules/src/llvm-project"),
|
||||
"'.' is handled specifically to not fail to swap in the CWD"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize((&cwd).into(), &cwd).unwrap(),
|
||||
cwd,
|
||||
"absolute inputs yield absolute outputs"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p("a/../..").into(), &cwd).unwrap(),
|
||||
cwd.parent().expect("parent"),
|
||||
"it automatically extends the pop-able items by using the current working dir"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p("a/..").into(), &cwd).unwrap(),
|
||||
p("."),
|
||||
"absolute CWDs are always shortened…"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p("./a/..").into(), &cwd).unwrap(),
|
||||
p("."),
|
||||
"…like this as well…"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize((&cwd).into(), &cwd).unwrap(),
|
||||
cwd,
|
||||
"…but only if there were relative to begin with."
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p(".").into(), &cwd).unwrap(),
|
||||
p("."),
|
||||
"and this means that `.`. stays `.`"
|
||||
);
|
||||
{
|
||||
let mut path = cwd.clone();
|
||||
let last_component = path.file_name().expect("directory").to_owned();
|
||||
path.push("..");
|
||||
path.push(last_component);
|
||||
|
||||
assert_eq!(
|
||||
normalize(path.into(), &cwd).unwrap(),
|
||||
cwd,
|
||||
"absolute input paths stay absolute"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_dirs_cause_the_cwd_to_be_used() {
|
||||
assert_eq!(
|
||||
normalize(p("./a/b/../../..").into(), "/users/name".as_ref())
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
p("/users")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_parent_dir_movements_eat_into_the_current_dir() {
|
||||
assert_eq!(
|
||||
normalize(p("../../../d/e").into(), "/users/name/a/b/c".as_ref())
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
p("/users/name/d/e")
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p("c/../../../d/e").into(), "/users/name/a/b".as_ref())
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
p("/users/name/d/e")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn walking_up_too_much_yield_none() {
|
||||
let cwd = "/users/name".as_ref();
|
||||
assert_eq!(normalize(p("./a/b/../../../../../.").into(), cwd), None);
|
||||
assert_eq!(normalize(p("./a/../../../..").into(), cwd), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailing_directories_after_too_numerous_parent_dirs_yield_none() {
|
||||
assert_eq!(
|
||||
normalize(p("./a/b/../../../../../actually-invalid").into(), "/users".as_ref()).as_ref(),
|
||||
None,
|
||||
);
|
||||
assert_eq!(
|
||||
normalize(p("/a/b/../../..").into(), "/does-not/matter".as_ref()).as_ref(),
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailing_relative_components_are_resolved() {
|
||||
let cwd = Path::new("/a/b/c");
|
||||
for (input, expected) in [
|
||||
("./a/b/./c/../d/..", "./a/b"),
|
||||
("a/./b/c/.././..", "a"),
|
||||
("/a/b/c/.././../.", "/a"),
|
||||
("./a/..", "."),
|
||||
("a/..", "."),
|
||||
("./a", "./a"),
|
||||
("./a/./b", "./a/./b"),
|
||||
("./a/./b/..", "./a/."),
|
||||
("/a/./b/c/.././../.", "/a"),
|
||||
("/a/./b", "/a/./b"),
|
||||
("././/a/./b", "././/a/./b"),
|
||||
("/a/././c/.././../.", "/"),
|
||||
("/a/b/../c/../..", "/"),
|
||||
("C:/hello/../a", "C:/a"),
|
||||
("./a/../b/..", "./"),
|
||||
("/a/../b", "/b"),
|
||||
] {
|
||||
let path = p(input);
|
||||
assert_eq!(
|
||||
normalize(path.into(), cwd).unwrap_or_else(|| panic!("{path:?}")),
|
||||
Cow::Borrowed(p(expected)),
|
||||
"'{input}' got an unexpected result"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user