Temporarily remove the circular dep (#4009)

This commit is contained in:
JT 2021-09-15 09:17:31 +12:00 committed by GitHub
parent f9ae882012
commit 7fc65067cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 631 additions and 634 deletions

1
Cargo.lock generated
View File

@ -2764,7 +2764,6 @@ version = "0.37.0"
dependencies = [ dependencies = [
"dirs-next", "dirs-next",
"dunce", "dunce",
"nu-test-support",
] ]
[[package]] [[package]]

View File

@ -10,5 +10,3 @@ version = "0.37.0"
dirs-next = "2.0.0" dirs-next = "2.0.0"
dunce = "1.0.1" dunce = "1.0.1"
[dev-dependencies]
nu-test-support = { version = "0.37.0", path="../nu-test-support" }

View File

@ -1,412 +1,412 @@
use std::path::Path; // use std::path::Path;
use nu_test_support::fs::Stub::EmptyFile; // use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground; // use nu_test_support::playground::Playground;
use nu_path::{canonicalize, canonicalize_with}; // use nu_path::{canonicalize, canonicalize_with};
#[test] // #[test]
fn canonicalize_path() { // fn canonicalize_path() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let mut spam = dirs.test().clone(); // let mut spam = dirs.test().clone();
spam.push("spam.txt"); // spam.push("spam.txt");
let actual = canonicalize(spam).expect("Failed to canonicalize"); // let actual = canonicalize(spam).expect("Failed to canonicalize");
assert!(actual.ends_with("spam.txt")); // assert!(actual.ends_with("spam.txt"));
}); // });
} // }
#[test] // #[test]
fn canonicalize_unicode_path() { // fn canonicalize_unicode_path() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("🚒.txt")]); // sandbox.with_files(vec![EmptyFile("🚒.txt")]);
let mut spam = dirs.test().clone(); // let mut spam = dirs.test().clone();
spam.push("🚒.txt"); // spam.push("🚒.txt");
let actual = canonicalize(spam).expect("Failed to canonicalize"); // let actual = canonicalize(spam).expect("Failed to canonicalize");
assert!(actual.ends_with("🚒.txt")); // assert!(actual.ends_with("🚒.txt"));
}); // });
} // }
#[ignore] // #[ignore]
#[test] // #[test]
fn canonicalize_non_utf8_path() { // fn canonicalize_non_utf8_path() {
// TODO // // TODO
} // }
#[test] // #[test]
fn canonicalize_path_relative_to() { // fn canonicalize_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with("spam.txt", dirs.test()).expect("Failed to canonicalize"); // let actual = canonicalize_with("spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_unicode_path_relative_to_unicode_path_with_spaces() { // fn canonicalize_unicode_path_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("e-$ èрт🚒♞中片-j"); // sandbox.mkdir("e-$ èрт🚒♞中片-j");
sandbox.with_files(vec![EmptyFile("e-$ èрт🚒♞中片-j/🚒.txt")]); // sandbox.with_files(vec![EmptyFile("e-$ èрт🚒♞中片-j/🚒.txt")]);
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("e-$ èрт🚒♞中片-j"); // relative_to.push("e-$ èрт🚒♞中片-j");
let actual = canonicalize_with("🚒.txt", relative_to).expect("Failed to canonicalize"); // let actual = canonicalize_with("🚒.txt", relative_to).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("e-$ èрт🚒♞中片-j/🚒.txt"); // expected.push("e-$ èрт🚒♞中片-j/🚒.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[ignore] // #[ignore]
#[test] // #[test]
fn canonicalize_non_utf8_path_relative_to_non_utf8_path_with_spaces() { // fn canonicalize_non_utf8_path_relative_to_non_utf8_path_with_spaces() {
// TODO // // TODO
} // }
#[test] // #[test]
fn canonicalize_absolute_path_relative_to() { // fn canonicalize_absolute_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let mut absolute_path = dirs.test().clone(); // let mut absolute_path = dirs.test().clone();
absolute_path.push("spam.txt"); // absolute_path.push("spam.txt");
let actual = canonicalize_with(&absolute_path, "non/existent/directory") // let actual = canonicalize_with(&absolute_path, "non/existent/directory")
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let expected = absolute_path; // let expected = absolute_path;
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_dot() { // fn canonicalize_dot() {
let actual = canonicalize(".").expect("Failed to canonicalize"); // let actual = canonicalize(".").expect("Failed to canonicalize");
let expected = std::env::current_dir().expect("Could not get current directory"); // let expected = std::env::current_dir().expect("Could not get current directory");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn canonicalize_many_dots() { // fn canonicalize_many_dots() {
let actual = canonicalize("././/.//////./././//.///").expect("Failed to canonicalize"); // let actual = canonicalize("././/.//////./././//.///").expect("Failed to canonicalize");
let expected = std::env::current_dir().expect("Could not get current directory"); // let expected = std::env::current_dir().expect("Could not get current directory");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn canonicalize_path_with_dot_relative_to() { // fn canonicalize_path_with_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with("./spam.txt", dirs.test()).expect("Failed to canonicalize"); // let actual = canonicalize_with("./spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_many_dots_relative_to() { // fn canonicalize_path_with_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with("././/.//////./././//.////spam.txt", dirs.test()) // let actual = canonicalize_with("././/.//////./././//.////spam.txt", dirs.test())
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_double_dot() { // fn canonicalize_double_dot() {
let actual = canonicalize("..").expect("Failed to canonicalize"); // let actual = canonicalize("..").expect("Failed to canonicalize");
let cwd = std::env::current_dir().expect("Could not get current directory"); // let cwd = std::env::current_dir().expect("Could not get current directory");
let expected = cwd // let expected = cwd
.parent() // .parent()
.expect("Could not get parent of current directory"); // .expect("Could not get parent of current directory");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn canonicalize_path_with_double_dot_relative_to() { // fn canonicalize_path_with_double_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo"); // sandbox.mkdir("foo");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = // let actual =
canonicalize_with("foo/../spam.txt", dirs.test()).expect("Failed to canonicalize"); // canonicalize_with("foo/../spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_many_double_dots_relative_to() { // fn canonicalize_path_with_many_double_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz"); // sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with("foo/bar/baz/../../../spam.txt", dirs.test()) // let actual = canonicalize_with("foo/bar/baz/../../../spam.txt", dirs.test())
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_ndots() { // fn canonicalize_ndots() {
let actual = canonicalize("...").expect("Failed to canonicalize"); // let actual = canonicalize("...").expect("Failed to canonicalize");
let cwd = std::env::current_dir().expect("Could not get current directory"); // let cwd = std::env::current_dir().expect("Could not get current directory");
let expected = cwd // let expected = cwd
.parent() // .parent()
.expect("Could not get parent of current directory") // .expect("Could not get parent of current directory")
.parent() // .parent()
.expect("Could not get parent of a parent of current directory"); // .expect("Could not get parent of a parent of current directory");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn canonicalize_path_with_3_ndots_relative_to() { // fn canonicalize_path_with_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar"); // sandbox.mkdir("foo/bar");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = // let actual =
canonicalize_with("foo/bar/.../spam.txt", dirs.test()).expect("Failed to canonicalize"); // canonicalize_with("foo/bar/.../spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_many_3_ndots_relative_to() { // fn canonicalize_path_with_many_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon"); // sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with( // let actual = canonicalize_with(
"foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt", // "foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt",
dirs.test(), // dirs.test(),
) // )
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_4_ndots_relative_to() { // fn canonicalize_path_with_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz"); // sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with("foo/bar/baz/..../spam.txt", dirs.test()) // let actual = canonicalize_with("foo/bar/baz/..../spam.txt", dirs.test())
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_many_4_ndots_relative_to() { // fn canonicalize_path_with_many_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon"); // sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let actual = canonicalize_with( // let actual = canonicalize_with(
"foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt", // "foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt",
dirs.test(), // dirs.test(),
) // )
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_path_with_way_too_many_dots_relative_to() { // fn canonicalize_path_with_way_too_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon/vikings"); // sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon/vikings");
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings"); // relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings");
let actual = canonicalize_with("././..////././...///././.....///spam.txt", relative_to) // let actual = canonicalize_with("././..////././...///././.....///spam.txt", relative_to)
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces() { // fn canonicalize_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä"); // sandbox.mkdir("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
sandbox.with_files(vec![EmptyFile("🚒.txt")]); // sandbox.with_files(vec![EmptyFile("🚒.txt")]);
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä"); // relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
let actual = canonicalize_with("././..////././...///././.....///🚒.txt", relative_to) // let actual = canonicalize_with("././..////././...///././.....///🚒.txt", relative_to)
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("🚒.txt"); // expected.push("🚒.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_tilde() { // fn canonicalize_tilde() {
let tilde_path = "~"; // let tilde_path = "~";
let actual = canonicalize(tilde_path).expect("Failed to canonicalize"); // let actual = canonicalize(tilde_path).expect("Failed to canonicalize");
assert!(actual.is_absolute()); // assert!(actual.is_absolute());
assert!(!actual.starts_with("~")); // assert!(!actual.starts_with("~"));
} // }
#[test] // #[test]
fn canonicalize_tilde_relative_to() { // fn canonicalize_tilde_relative_to() {
let tilde_path = "~"; // let tilde_path = "~";
let actual = // let actual =
canonicalize_with(tilde_path, "non/existent/path").expect("Failed to canonicalize"); // canonicalize_with(tilde_path, "non/existent/path").expect("Failed to canonicalize");
assert!(actual.is_absolute()); // assert!(actual.is_absolute());
assert!(!actual.starts_with("~")); // assert!(!actual.starts_with("~"));
} // }
#[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
#[test] // #[test]
fn canonicalize_symlink() { // fn canonicalize_symlink() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt"); // sandbox.symlink("spam.txt", "link_to_spam.txt");
let mut symlink_path = dirs.test().clone(); // let mut symlink_path = dirs.test().clone();
symlink_path.push("link_to_spam.txt"); // symlink_path.push("link_to_spam.txt");
let actual = canonicalize(symlink_path).expect("Failed to canonicalize"); // let actual = canonicalize(symlink_path).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
#[test] // #[test]
fn canonicalize_symlink_relative_to() { // fn canonicalize_symlink_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt"); // sandbox.symlink("spam.txt", "link_to_spam.txt");
let actual = // let actual =
canonicalize_with("link_to_spam.txt", dirs.test()).expect("Failed to canonicalize"); // canonicalize_with("link_to_spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
#[cfg(not(windows))] // seems like Windows symlink requires existing file or dir // #[cfg(not(windows))] // seems like Windows symlink requires existing file or dir
#[test] // #[test]
fn canonicalize_symlink_loop_relative_to_should_fail() { // fn canonicalize_symlink_loop_relative_to_should_fail() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
// sandbox.with_files(vec![EmptyFile("spam.txt")]); // // sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt"); // sandbox.symlink("spam.txt", "link_to_spam.txt");
sandbox.symlink("link_to_spam.txt", "spam.txt"); // sandbox.symlink("link_to_spam.txt", "spam.txt");
let actual = canonicalize_with("link_to_spam.txt", dirs.test()); // let actual = canonicalize_with("link_to_spam.txt", dirs.test());
assert!(actual.is_err()); // assert!(actual.is_err());
}); // });
} // }
#[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
#[test] // #[test]
fn canonicalize_nested_symlink_relative_to() { // fn canonicalize_nested_symlink_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]); // sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt"); // sandbox.symlink("spam.txt", "link_to_spam.txt");
sandbox.symlink("link_to_spam.txt", "link_to_link_to_spam.txt"); // sandbox.symlink("link_to_spam.txt", "link_to_link_to_spam.txt");
let actual = canonicalize_with("link_to_link_to_spam.txt", dirs.test()) // let actual = canonicalize_with("link_to_link_to_spam.txt", dirs.test())
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[cfg(not(target_arch = "wasm32"))] // #[cfg(not(target_arch = "wasm32"))]
#[test] // #[test]
fn canonicalize_nested_symlink_within_symlink_dir_relative_to() { // fn canonicalize_nested_symlink_within_symlink_dir_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| { // Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz"); // sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("foo/bar/baz/spam.txt")]); // sandbox.with_files(vec![EmptyFile("foo/bar/baz/spam.txt")]);
sandbox.symlink("foo/bar/baz/spam.txt", "foo/bar/link_to_spam.txt"); // sandbox.symlink("foo/bar/baz/spam.txt", "foo/bar/link_to_spam.txt");
sandbox.symlink("foo/bar/link_to_spam.txt", "foo/link_to_link_to_spam.txt"); // sandbox.symlink("foo/bar/link_to_spam.txt", "foo/link_to_link_to_spam.txt");
sandbox.symlink("foo", "link_to_foo"); // sandbox.symlink("foo", "link_to_foo");
let actual = canonicalize_with("link_to_foo/link_to_link_to_spam.txt", dirs.test()) // let actual = canonicalize_with("link_to_foo/link_to_link_to_spam.txt", dirs.test())
.expect("Failed to canonicalize"); // .expect("Failed to canonicalize");
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("foo/bar/baz/spam.txt"); // expected.push("foo/bar/baz/spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn canonicalize_should_fail() { // fn canonicalize_should_fail() {
let path = Path::new("/foo/bar/baz"); // hopefully, this path does not exist // let path = Path::new("/foo/bar/baz"); // hopefully, this path does not exist
assert!(canonicalize(path).is_err()); // assert!(canonicalize(path).is_err());
} // }
#[test] // #[test]
fn canonicalize_with_should_fail() { // fn canonicalize_with_should_fail() {
let relative_to = "/foo"; // let relative_to = "/foo";
let path = "bar/baz"; // let path = "bar/baz";
assert!(canonicalize_with(path, relative_to).is_err()); // assert!(canonicalize_with(path, relative_to).is_err());
} // }

View File

@ -1,294 +1,294 @@
use std::path::PathBuf; // use std::path::PathBuf;
use nu_test_support::playground::Playground; // use nu_test_support::playground::Playground;
use nu_path::{expand_path, expand_path_with}; // use nu_path::{expand_path, expand_path_with};
#[test] // #[test]
fn expand_path_with_and_without_relative() { // fn expand_path_with_and_without_relative() {
let relative_to = "/foo/bar"; // let relative_to = "/foo/bar";
let path = "../.."; // let path = "../..";
let full_path = "/foo/bar/../.."; // let full_path = "/foo/bar/../..";
assert_eq!(expand_path(full_path), expand_path_with(path, relative_to),); // assert_eq!(expand_path(full_path), expand_path_with(path, relative_to),);
} // }
#[test] // #[test]
fn expand_path_with_relative() { // fn expand_path_with_relative() {
let relative_to = "/foo/bar"; // let relative_to = "/foo/bar";
let path = "../.."; // let path = "../..";
assert_eq!(PathBuf::from("/"), expand_path_with(path, relative_to),); // assert_eq!(PathBuf::from("/"), expand_path_with(path, relative_to),);
} // }
#[test] // #[test]
fn expand_path_no_change() { // fn expand_path_no_change() {
let path = "/foo/bar"; // let path = "/foo/bar";
let actual = expand_path(&path); // let actual = expand_path(&path);
assert_eq!(actual, PathBuf::from(path)); // assert_eq!(actual, PathBuf::from(path));
} // }
#[test] // #[test]
fn expand_unicode_path_no_change() { // fn expand_unicode_path_no_change() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let mut spam = dirs.test().clone(); // let mut spam = dirs.test().clone();
spam.push("🚒.txt"); // spam.push("🚒.txt");
let actual = expand_path(spam); // let actual = expand_path(spam);
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("🚒.txt"); // expected.push("🚒.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[ignore] // #[ignore]
#[test] // #[test]
fn expand_non_utf8_path() { // fn expand_non_utf8_path() {
// TODO // // TODO
} // }
#[test] // #[test]
fn expand_path_relative_to() { // fn expand_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("spam.txt", dirs.test()); // let actual = expand_path_with("spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_unicode_path_relative_to_unicode_path_with_spaces() { // fn expand_unicode_path_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("e-$ èрт🚒♞中片-j"); // relative_to.push("e-$ èрт🚒♞中片-j");
let actual = expand_path_with("🚒.txt", relative_to); // let actual = expand_path_with("🚒.txt", relative_to);
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("e-$ èрт🚒♞中片-j/🚒.txt"); // expected.push("e-$ èрт🚒♞中片-j/🚒.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[ignore] // #[ignore]
#[test] // #[test]
fn expand_non_utf8_path_relative_to_non_utf8_path_with_spaces() { // fn expand_non_utf8_path_relative_to_non_utf8_path_with_spaces() {
// TODO // // TODO
} // }
#[test] // #[test]
fn expand_absolute_path_relative_to() { // fn expand_absolute_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let mut absolute_path = dirs.test().clone(); // let mut absolute_path = dirs.test().clone();
absolute_path.push("spam.txt"); // absolute_path.push("spam.txt");
let actual = expand_path_with(&absolute_path, "non/existent/directory"); // let actual = expand_path_with(&absolute_path, "non/existent/directory");
let expected = absolute_path; // let expected = absolute_path;
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_dot() { // fn expand_path_dot() {
let actual = expand_path("."); // let actual = expand_path(".");
let expected = PathBuf::from("."); // let expected = PathBuf::from(".");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_many_dots() { // fn expand_path_many_dots() {
let actual = expand_path("././/.//////./././//.///"); // let actual = expand_path("././/.//////./././//.///");
let expected = PathBuf::from("././././././."); // let expected = PathBuf::from("././././././.");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_with_dot_relative_to() { // fn expand_path_with_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("./spam.txt", dirs.test()); // let actual = expand_path_with("./spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_double_dot() { // fn expand_path_double_dot() {
let actual = expand_path(".."); // let actual = expand_path("..");
let expected = PathBuf::from(".."); // let expected = PathBuf::from("..");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_dot_double_dot() { // fn expand_path_dot_double_dot() {
let actual = expand_path("./.."); // let actual = expand_path("./..");
let expected = PathBuf::from("./.."); // let expected = PathBuf::from("./..");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_double_dot_dot() { // fn expand_path_double_dot_dot() {
let actual = expand_path("../."); // let actual = expand_path("../.");
let expected = PathBuf::from(".."); // let expected = PathBuf::from("..");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_with_many_dots_relative_to() { // fn expand_path_with_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("././/.//////./././//.////spam.txt", dirs.test()); // let actual = expand_path_with("././/.//////./././//.////spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_double_dot_relative_to() { // fn expand_path_with_double_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("foo/../spam.txt", dirs.test()); // let actual = expand_path_with("foo/../spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_many_double_dots_relative_to() { // fn expand_path_with_many_double_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("foo/bar/baz/../../../spam.txt", dirs.test()); // let actual = expand_path_with("foo/bar/baz/../../../spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_ndots() { // fn expand_path_ndots() {
let actual = expand_path("..."); // let actual = expand_path("...");
let mut expected = PathBuf::from(".."); // let mut expected = PathBuf::from("..");
expected.push(".."); // expected.push("..");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_normal_path_ndots() { // fn expand_normal_path_ndots() {
let actual = expand_path("foo/bar/baz/..."); // let actual = expand_path("foo/bar/baz/...");
let expected = PathBuf::from("foo"); // let expected = PathBuf::from("foo");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
} // }
#[test] // #[test]
fn expand_path_with_3_ndots_relative_to() { // fn expand_path_with_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("foo/bar/.../spam.txt", dirs.test()); // let actual = expand_path_with("foo/bar/.../spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_many_3_ndots_relative_to() { // fn expand_path_with_many_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with( // let actual = expand_path_with(
"foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt", // "foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt",
dirs.test(), // dirs.test(),
); // );
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_4_ndots_relative_to() { // fn expand_path_with_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with("foo/bar/baz/..../spam.txt", dirs.test()); // let actual = expand_path_with("foo/bar/baz/..../spam.txt", dirs.test());
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_many_4_ndots_relative_to() { // fn expand_path_with_many_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let actual = expand_path_with( // let actual = expand_path_with(
"foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt", // "foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt",
dirs.test(), // dirs.test(),
); // );
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_with_way_too_many_dots_relative_to() { // fn expand_path_with_way_too_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings"); // relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings");
let actual = expand_path_with("././..////././...///././.....///spam.txt", relative_to); // let actual = expand_path_with("././..////././...///././.....///spam.txt", relative_to);
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("spam.txt"); // expected.push("spam.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces() { // fn expand_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, _| { // Playground::setup("nu_path_test_1", |dirs, _| {
let mut relative_to = dirs.test().clone(); // let mut relative_to = dirs.test().clone();
relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä"); // relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
let actual = expand_path_with("././..////././...///././.....///🚒.txt", relative_to); // let actual = expand_path_with("././..////././...///././.....///🚒.txt", relative_to);
let mut expected = dirs.test().clone(); // let mut expected = dirs.test().clone();
expected.push("🚒.txt"); // expected.push("🚒.txt");
assert_eq!(actual, expected); // assert_eq!(actual, expected);
}); // });
} // }
#[test] // #[test]
fn expand_path_tilde() { // fn expand_path_tilde() {
let tilde_path = "~"; // let tilde_path = "~";
let actual = expand_path(tilde_path); // let actual = expand_path(tilde_path);
assert!(actual.is_absolute()); // assert!(actual.is_absolute());
assert!(!actual.starts_with("~")); // assert!(!actual.starts_with("~"));
} // }
#[test] // #[test]
fn expand_path_tilde_relative_to() { // fn expand_path_tilde_relative_to() {
let tilde_path = "~"; // let tilde_path = "~";
let actual = expand_path_with(tilde_path, "non/existent/path"); // let actual = expand_path_with(tilde_path, "non/existent/path");
assert!(actual.is_absolute()); // assert!(actual.is_absolute());
assert!(!actual.starts_with("~")); // assert!(!actual.starts_with("~"));
} // }