| 140 | |
| 141 | #[test] |
| 142 | fn test_dir_new() { |
| 143 | let t = rustix::fs::openat( |
| 144 | rustix::fs::CWD, |
| 145 | rustix::cstr!("."), |
| 146 | rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 147 | rustix::fs::Mode::empty(), |
| 148 | ) |
| 149 | .unwrap(); |
| 150 | |
| 151 | let _file = rustix::fs::openat( |
| 152 | &t, |
| 153 | rustix::cstr!("Cargo.toml"), |
| 154 | rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 155 | rustix::fs::Mode::empty(), |
| 156 | ) |
| 157 | .unwrap(); |
| 158 | |
| 159 | let mut dir = rustix::fs::Dir::new(t).unwrap(); |
| 160 | |
| 161 | // Read the directory entries. We use `while let Some(entry)` so that we |
| 162 | // don't consume the `Dir` so that we can run more tests on it. |
| 163 | let mut saw_dot = false; |
| 164 | let mut saw_dotdot = false; |
| 165 | let mut saw_cargo_toml = false; |
| 166 | while let Some(entry) = dir.read() { |
| 167 | let entry = entry.unwrap(); |
| 168 | if entry.file_name() == rustix::cstr!(".") { |
| 169 | saw_dot = true; |
| 170 | } else if entry.file_name() == rustix::cstr!("..") { |
| 171 | saw_dotdot = true; |
| 172 | } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 173 | saw_cargo_toml = true; |
| 174 | } |
| 175 | } |
| 176 | assert!(saw_dot); |
| 177 | assert!(saw_dotdot); |
| 178 | assert!(saw_cargo_toml); |
| 179 | |
| 180 | // Rewind the directory so we can iterate over the entries again. |
| 181 | dir.rewind(); |
| 182 | |
| 183 | // For what comes next, we don't need `mut` anymore. |
| 184 | let dir = dir; |
| 185 | |
| 186 | // Read the directory entries, again. This time we use `for entry in dir`. |
| 187 | let mut saw_dot = false; |
| 188 | let mut saw_dotdot = false; |
| 189 | let mut saw_cargo_toml = false; |
| 190 | for entry in dir { |
| 191 | let entry = entry.unwrap(); |
| 192 | if entry.file_name() == rustix::cstr!(".") { |
| 193 | saw_dot = true; |
| 194 | } else if entry.file_name() == rustix::cstr!("..") { |
| 195 | saw_dotdot = true; |
| 196 | } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 197 | saw_cargo_toml = true; |
| 198 | } |
| 199 | } |