summaryrefslogtreecommitdiffstats
path: root/third_party/rust/nix/test/test_dir.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/nix/test/test_dir.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/nix/test/test_dir.rs')
-rw-r--r--third_party/rust/nix/test/test_dir.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/third_party/rust/nix/test/test_dir.rs b/third_party/rust/nix/test/test_dir.rs
new file mode 100644
index 0000000000..2af4aa5c0a
--- /dev/null
+++ b/third_party/rust/nix/test/test_dir.rs
@@ -0,0 +1,65 @@
+use nix::dir::{Dir, Type};
+use nix::fcntl::OFlag;
+use nix::sys::stat::Mode;
+use std::fs::File;
+use tempfile::tempdir;
+
+#[cfg(test)]
+fn flags() -> OFlag {
+ #[cfg(target_os = "illumos")]
+ let f = OFlag::O_RDONLY | OFlag::O_CLOEXEC;
+
+ #[cfg(not(target_os = "illumos"))]
+ let f = OFlag::O_RDONLY | OFlag::O_CLOEXEC | OFlag::O_DIRECTORY;
+
+ f
+}
+
+#[test]
+#[allow(clippy::unnecessary_sort_by)] // False positive
+fn read() {
+ let tmp = tempdir().unwrap();
+ File::create(tmp.path().join("foo")).unwrap();
+ std::os::unix::fs::symlink("foo", tmp.path().join("bar")).unwrap();
+ let mut dir = Dir::open(tmp.path(), flags(), Mode::empty()).unwrap();
+ let mut entries: Vec<_> = dir.iter().map(|e| e.unwrap()).collect();
+ entries.sort_by(|a, b| a.file_name().cmp(b.file_name()));
+ let entry_names: Vec<_> = entries
+ .iter()
+ .map(|e| e.file_name().to_str().unwrap().to_owned())
+ .collect();
+ assert_eq!(&entry_names[..], &[".", "..", "bar", "foo"]);
+
+ // Check file types. The system is allowed to return DT_UNKNOWN (aka None here) but if it does
+ // return a type, ensure it's correct.
+ assert!(&[Some(Type::Directory), None].contains(&entries[0].file_type())); // .: dir
+ assert!(&[Some(Type::Directory), None].contains(&entries[1].file_type())); // ..: dir
+ assert!(&[Some(Type::Symlink), None].contains(&entries[2].file_type())); // bar: symlink
+ assert!(&[Some(Type::File), None].contains(&entries[3].file_type())); // foo: regular file
+}
+
+#[test]
+fn rewind() {
+ let tmp = tempdir().unwrap();
+ let mut dir = Dir::open(tmp.path(), flags(), Mode::empty()).unwrap();
+ let entries1: Vec<_> = dir
+ .iter()
+ .map(|e| e.unwrap().file_name().to_owned())
+ .collect();
+ let entries2: Vec<_> = dir
+ .iter()
+ .map(|e| e.unwrap().file_name().to_owned())
+ .collect();
+ let entries3: Vec<_> = dir
+ .into_iter()
+ .map(|e| e.unwrap().file_name().to_owned())
+ .collect();
+ assert_eq!(entries1, entries2);
+ assert_eq!(entries2, entries3);
+}
+
+#[cfg(not(target_os = "haiku"))]
+#[test]
+fn ebadf() {
+ assert_eq!(Dir::from_fd(-1).unwrap_err(), nix::Error::EBADF);
+}