1 use nix::dir::{Dir, Type};
2 use nix::fcntl::OFlag;
3 use nix::sys::stat::Mode;
4 use std::fs::File;
5 use tempfile::tempdir;
6
7 #[test]
read()8 fn read() {
9 let tmp = tempdir().unwrap();
10 File::create(&tmp.path().join("foo")).unwrap();
11 ::std::os::unix::fs::symlink("foo", tmp.path().join("bar")).unwrap();
12 let mut dir = Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC,
13 Mode::empty()).unwrap();
14 let mut entries: Vec<_> = dir.iter().map(|e| e.unwrap()).collect();
15 entries.sort_by(|a, b| a.file_name().cmp(b.file_name()));
16 let entry_names: Vec<_> = entries
17 .iter()
18 .map(|e| e.file_name().to_str().unwrap().to_owned())
19 .collect();
20 assert_eq!(&entry_names[..], &[".", "..", "bar", "foo"]);
21
22 // Check file types. The system is allowed to return DT_UNKNOWN (aka None here) but if it does
23 // return a type, ensure it's correct.
24 assert!(&[Some(Type::Directory), None].contains(&entries[0].file_type())); // .: dir
25 assert!(&[Some(Type::Directory), None].contains(&entries[1].file_type())); // ..: dir
26 assert!(&[Some(Type::Symlink), None].contains(&entries[2].file_type())); // bar: symlink
27 assert!(&[Some(Type::File), None].contains(&entries[3].file_type())); // foo: regular file
28 }
29
30 #[test]
rewind()31 fn rewind() {
32 let tmp = tempdir().unwrap();
33 let mut dir = Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC,
34 Mode::empty()).unwrap();
35 let entries1: Vec<_> = dir.iter().map(|e| e.unwrap().file_name().to_owned()).collect();
36 let entries2: Vec<_> = dir.iter().map(|e| e.unwrap().file_name().to_owned()).collect();
37 let entries3: Vec<_> = dir.into_iter().map(|e| e.unwrap().file_name().to_owned()).collect();
38 assert_eq!(entries1, entries2);
39 assert_eq!(entries2, entries3);
40 }
41
42 #[test]
ebadf()43 fn ebadf() {
44 assert_eq!(Dir::from_fd(-1).unwrap_err(), nix::Error::Sys(nix::errno::Errno::EBADF));
45 }
46