|
1 | 1 | #[test]
|
2 |
| -fn test_dir() { |
| 2 | +fn test_dir_read_from() { |
3 | 3 | let t = rustix::fs::openat(
|
4 | 4 | rustix::fs::CWD,
|
5 | 5 | rustix::cstr!("."),
|
@@ -62,6 +62,70 @@ fn test_dir() {
|
62 | 62 | assert!(saw_cargo_toml);
|
63 | 63 | }
|
64 | 64 |
|
| 65 | +#[test] |
| 66 | +fn test_dir_new() { |
| 67 | + let t = rustix::fs::openat( |
| 68 | + rustix::fs::CWD, |
| 69 | + rustix::cstr!("."), |
| 70 | + rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 71 | + rustix::fs::Mode::empty(), |
| 72 | + ) |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + let _file = rustix::fs::openat( |
| 76 | + &t, |
| 77 | + rustix::cstr!("Cargo.toml"), |
| 78 | + rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::CLOEXEC, |
| 79 | + rustix::fs::Mode::empty(), |
| 80 | + ) |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + let mut dir = rustix::fs::Dir::new(t).unwrap(); |
| 84 | + |
| 85 | + // Read the directory entries. We use `while let Some(entry)` so that we |
| 86 | + // don't consume the `Dir` so that we can run more tests on it. |
| 87 | + let mut saw_dot = false; |
| 88 | + let mut saw_dotdot = false; |
| 89 | + let mut saw_cargo_toml = false; |
| 90 | + while let Some(entry) = dir.read() { |
| 91 | + let entry = entry.unwrap(); |
| 92 | + if entry.file_name() == rustix::cstr!(".") { |
| 93 | + saw_dot = true; |
| 94 | + } else if entry.file_name() == rustix::cstr!("..") { |
| 95 | + saw_dotdot = true; |
| 96 | + } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 97 | + saw_cargo_toml = true; |
| 98 | + } |
| 99 | + } |
| 100 | + assert!(saw_dot); |
| 101 | + assert!(saw_dotdot); |
| 102 | + assert!(saw_cargo_toml); |
| 103 | + |
| 104 | + // Rewind the directory so we can iterate over the entries again. |
| 105 | + dir.rewind(); |
| 106 | + |
| 107 | + // For what comes next, we don't need `mut` anymore. |
| 108 | + let dir = dir; |
| 109 | + |
| 110 | + // Read the directory entries, again. This time we use `for entry in dir`. |
| 111 | + let mut saw_dot = false; |
| 112 | + let mut saw_dotdot = false; |
| 113 | + let mut saw_cargo_toml = false; |
| 114 | + for entry in dir { |
| 115 | + let entry = entry.unwrap(); |
| 116 | + if entry.file_name() == rustix::cstr!(".") { |
| 117 | + saw_dot = true; |
| 118 | + } else if entry.file_name() == rustix::cstr!("..") { |
| 119 | + saw_dotdot = true; |
| 120 | + } else if entry.file_name() == rustix::cstr!("Cargo.toml") { |
| 121 | + saw_cargo_toml = true; |
| 122 | + } |
| 123 | + } |
| 124 | + assert!(saw_dot); |
| 125 | + assert!(saw_dotdot); |
| 126 | + assert!(saw_cargo_toml); |
| 127 | +} |
| 128 | + |
65 | 129 | // Test that `Dir` silently stops iterating if the directory has been removed.
|
66 | 130 | //
|
67 | 131 | // Except on FreeBSD and macOS, where apparently `readdir` just keeps reading.
|
|
0 commit comments