use super::helpers as h;
use super::sys_common::io::tmpdir;
use crate::filesystem::primitives as p;
use std::path::Path;
#[cfg(not(windows))]
fn no_such_file_or_directory() -> String {
rustix::io::Errno::NOENT.to_string()
}
#[cfg(windows)]
fn no_such_file_or_directory() -> String {
std::io::Error::from_raw_os_error(windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND as i32)
.to_string()
}
#[test]
#[cfg_attr(windows, ignore)] fn rename_basics() {
let tmpdir = tmpdir();
let dir = h::dir_of(&tmpdir);
check!(h::create_dir_all(&dir, "foo/bar"));
check!(h::create(&dir, "foo/bar/file.txt"));
check!(p::rename(
&dir,
Path::new("foo/bar/file.txt"),
&dir,
Path::new("foo/bar/renamed.txt")
));
assert!(!h::exists(&dir, "foo/bar/file.txt"));
assert!(h::exists(&dir, "foo/bar/renamed.txt"));
check!(p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("foo/bar/renamed.txt")
));
error_contains!(
p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("..")
),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("foo/../..")
),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("/tmp")
),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("foo/bar/baz/..")
),
&no_such_file_or_directory()
);
check!(p::rename(
&dir,
Path::new("foo/bar"),
&dir,
Path::new("foo/bar")
));
check!(p::rename(
&dir,
Path::new("foo/bar/renamed.txt"),
&dir,
Path::new("file.txt")
));
assert!(!h::exists(&dir, "foo/bar/renamed.txt"));
assert!(h::exists(&dir, "file.txt"));
error_contains!(
p::rename(
&dir,
Path::new("file.txt"),
&dir,
Path::new("foo/bar/../../..")
),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(
&dir,
Path::new("file.txt"),
&dir,
Path::new("foo/bar/../../../something")
),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("file.txt"), &dir, Path::new("")),
"No such file"
);
error_contains!(
p::rename(&dir, Path::new("file.txt"), &dir, Path::new("/")),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("file.txt"), &dir, Path::new("/.")),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("file.txt"), &dir, Path::new("/..")),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("/"), &dir, Path::new("nope.txt")),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("/.."), &dir, Path::new("nope.txt")),
"a path led outside of the filesystem"
);
error_contains!(
p::rename(&dir, Path::new("file.txt/"), &dir, Path::new("nope.txt")),
"Not a directory"
);
check!(h::create(&dir, "existing.txt"));
check!(p::rename(
&dir,
Path::new("file.txt"),
&dir,
Path::new("existing.txt")
));
assert!(!h::exists(&dir, "file.txt"));
assert!(h::exists(&dir, "existing.txt"));
}