| 1 | #[test] |
| 2 | fn test_dir_read_from() { |
| 3 | let t = rustix::fs::openat( |
| 4 | rustix::fs::CWD, |
| 5 | rustix::cstr!("."), |
| 6 | rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 7 | rustix::fs::Mode::empty(), |
| 8 | ) |
| 9 | .unwrap(); |
| 10 | |
| 11 | let mut dir = rustix::fs::Dir::read_from(&t).unwrap(); |
| 12 | |
| 13 | let _file = rustix::fs::openat( |
| 14 | &t, |
| 15 | rustix::cstr!("Cargo.toml"), |
| 16 | rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 17 | rustix::fs::Mode::empty(), |
| 18 | ) |
| 19 | .unwrap(); |
| 20 | |
| 21 | // Read the directory entries. We use `while let Some(entry)` so that we |
| 22 | // don't consume the `Dir` so that we can run more tests on it. |
| 23 | let mut saw_dot = false; |
| 24 | let mut saw_dotdot = false; |
| 25 | let mut saw_cargo_toml = false; |
| 26 | while let Some(entry) = dir.read() { |
| 27 | let entry = entry.unwrap(); |
| 28 | if entry.file_name() == rustix::cstr!(".") { |
| 29 | saw_dot = true; |
| 30 | } else if entry.file_name() == rustix::cstr!("..") { |
| 31 | saw_dotdot = true; |
| 32 | } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 33 | saw_cargo_toml = true; |
| 34 | } |
| 35 | } |
| 36 | assert!(saw_dot); |
| 37 | assert!(saw_dotdot); |
| 38 | assert!(saw_cargo_toml); |
| 39 | |
| 40 | // Rewind the directory so we can iterate over the entries again. |
| 41 | dir.rewind(); |
| 42 | |
| 43 | // For what comes next, we don't need `mut` anymore. |
| 44 | let dir = dir; |
| 45 | |
| 46 | // Read the directory entries, again. This time we use `for entry in dir`. |
| 47 | let mut saw_dot = false; |
| 48 | let mut saw_dotdot = false; |
| 49 | let mut saw_cargo_toml = false; |
| 50 | for entry in dir { |
| 51 | let entry = entry.unwrap(); |
| 52 | if entry.file_name() == rustix::cstr!(".") { |
| 53 | saw_dot = true; |
| 54 | } else if entry.file_name() == rustix::cstr!("..") { |
| 55 | saw_dotdot = true; |
| 56 | } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 57 | saw_cargo_toml = true; |
| 58 | } |
| 59 | } |
| 60 | assert!(saw_dot); |