Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_file/tests/walk.rs

23.4 KiB, 19 runs

created by r1870400018:70647, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
2//! Anthropic Claude
3
4use oxedyne_fe2o3_file::{
5 glob::IgnoreFile,
6 walk::{
7 Disk,
8 Listed,
9 Rules,
10 Step,
11 Walk,
12 WalkEnd,
13 WalkSource,
14 EXCLUDES_NAME,
15 git_config_excludes,
16 git_global_excludes,
17 repo_top,
18 },
19};
20
21use oxedyne_fe2o3_core::{
22 prelude::*,
23 test::test_it,
24};
25
26use std::{
27 collections::BTreeMap,
28 fs,
29 path::PathBuf,
30};
31
32
33// A tree held in memory: every file path with its content. Directories are implied by the paths.
34struct Mem {
35 files: BTreeMap<Vec<u8>, Vec<u8>>,
36 lists: usize, // listings asked for
37 reads: Vec<String>, // ignore files asked for, in order
38}
39
40impl Mem {
41 fn new(files: &[(&str, &str)]) -> Self {
42 Self {
43 files: files.iter().map(|(p, c)| (p.as_bytes().to_vec(), c.as_bytes().to_vec())).collect(),
44 lists: 0,
45 reads: Vec::new(),
46 }
47 }
48}
49
50impl WalkSource for Mem {
51
52 fn list(&mut self, dir: &[u8]) -> Outcome<Vec<Listed>> {
53 self.lists += 1;
54 let mut prefix = dir.to_vec();
55 if !prefix.is_empty() {
56 prefix.push(b'/');
57 }
58 let mut out: Vec<Listed> = Vec::new();
59 for path in self.files.keys() {
60 if !path.starts_with(&prefix) {
61 continue;
62 }
63 let rest = &path[prefix.len()..];
64 let (name, is_dir) = match rest.iter().position(|b| *b == b'/') {
65 Some(i) => (rest[..i].to_vec(), true),
66 None => (rest.to_vec(), false),
67 };
68 if !out.iter().any(|e| e.name == name) {
69 out.push(Listed::new(name, is_dir));
70 }
71 }
72 Ok(out)
73 }
74
75 fn read(&mut self, path: &[u8]) -> Outcome<Option<Vec<u8>>> {
76 self.reads.push(String::from_utf8_lossy(path).into_owned());
77 Ok(self.files.get(path).cloned())
78 }
79}
80
81fn walk_all(walk: Walk, src: &mut Mem) -> Outcome<(Vec<String>, WalkEnd)> {
82 let mut found = Vec::new();
83 let end = res!(walk.run(src, |e| {
84 if !e.is_dir {
85 found.push(String::from_utf8_lossy(&e.path).into_owned());
86 }
87 Ok(true)
88 }));
89 Ok((found, end))
90}
91
92pub fn test_walk(filter: &'static str) -> Outcome<()> {
93
94 res!(test_it(filter, &["Defaults keep stores and dependencies out 000", "all", "walk"], || {
95 let mut src = Mem::new(&[
96 (".git/HEAD", ""),
97 (".ore/log", ""),
98 (".claude/worktrees/a/src/x.rs", ""),
99 (".stversions/old.rs", ""),
100 ("web/node_modules/pkg/index.js", ""),
101 ("target/debug/out", ""),
102 ("src/lib.rs", ""),
103 ("target.txt", ""),
104 ]);
105 let (found, end) = res!(walk_all(Walk::new(1000), &mut src));
106 assert_eq!(found, vec!["target.txt", "src/lib.rs"], "a directory's files before its subdirectories");
107 assert_eq!(end, WalkEnd::Complete);
108 assert_eq!(src.lists, 3, "root, src and web: no ignored directory is ever listed");
109 // A repository can take one back.
110 let mut src = Mem::new(&[
111 (".gitignore", "!node_modules/\n"),
112 ("node_modules/pkg/index.js", ""),
113 ]);
114 let (found, _) = res!(walk_all(Walk::new(1000), &mut src));
115 assert_eq!(found, vec![".gitignore", "node_modules/pkg/index.js"]);
116 // Or walk everything.
117 let mut src = Mem::new(&[(".git/HEAD", "")]);
118 let (found, _) = res!(walk_all(Walk::new(1000).with_defaults(IgnoreFile::default()), &mut src));
119 assert_eq!(found, vec![".git/HEAD"]);
120 Ok(())
121 }));
122
123 res!(test_it(filter, &["Nested ignore files speak for their own directory 000", "all", "walk"], || {
124 let mut src = Mem::new(&[
125 (".gitignore", "*.log\n/build/\n"),
126 ("a.log", ""),
127 ("build/out", ""),
128 ("sub/.gitignore", "/local.txt\n!keep.log\n"),
129 ("sub/keep.log", ""),
130 ("sub/drop.log", ""),
131 ("sub/local.txt", ""),
132 ("sub/build/out", ""),
133 ("sub/deep/local.txt", ""),
134 ("other/local.txt", ""),
135 ("other/keep.log", ""),
136 ]);
137 let (found, _) = res!(walk_all(Walk::new(1000), &mut src));
138 assert_eq!(found, vec![
139 ".gitignore",
140 "other/local.txt", // sub's rules do not reach a sibling
141 "sub/.gitignore",
142 "sub/keep.log", // the deeper file's negation outranks the root's *.log
143 "sub/build/out", // /build/ is anchored to the root
144 "sub/deep/local.txt", // /local.txt is anchored to sub
145 ]);
146 Ok(())
147 }));
148
149 res!(test_it(filter, &["Negation, dir-only and the ore file 000", "all", "walk"], || {
150 let mut src = Mem::new(&[
151 (".gitignore", "gen/\nnotes\n*.tmp\n"),
152 (".oreignore", "!x.tmp\n"),
153 ("gen/a.rs", ""),
154 ("x/gen", ""), // a file named like a dir-only pattern
155 ("notes/a.md", ""),
156 ("x.tmp", ""),
157 ("y.tmp", ""),
158 ]);
159 let (found, _) = res!(walk_all(Walk::new(1000), &mut src));
160 assert_eq!(found, vec![".gitignore", ".oreignore", "x.tmp", "x/gen"]);
161 // Nothing inside an ignored directory can be re-included, as in git.
162 let mut src = Mem::new(&[
163 (".gitignore", "dist/\n!dist/keep.js\n"),
164 ("dist/keep.js", ""),
165 ]);
166 let (found, _) = res!(walk_all(Walk::new(1000), &mut src));
167 assert_eq!(found, vec![".gitignore"]);
168 Ok(())
169 }));
170
171 res!(test_it(filter, &["The budget is reported when it runs out 000", "all", "walk"], || {
172 let mut src = Mem::new(&[
173 ("a/1", ""), ("a/2", ""), ("b/1", ""), ("c/1", ""), (".git/x", ""),
174 ]);
175 let end = res!(Walk::new(4).run(&mut src, |_| Ok(true)));
176 match end {
177 WalkEnd::Budget(rep) => {
178 // The root's four entries spend the budget, so the walk stops listing a.
179 assert_eq!(rep.budget, 4);
180 assert_eq!(rep.kept, 3);
181 assert_eq!(rep.ignored, 1, ".git");
182 assert_eq!(rep.unlisted, 2, "b and c were kept but never listed");
183 assert_eq!(rep.at, b"a".to_vec());
184 }
185 other => return Err(err!("Expected a budget stop, got {:?}.", other; Test)),
186 }
187 // A budget the tree fits in completes.
188 let end = res!(Walk::new(8).run(&mut src, |_| Ok(true)));
189 assert_eq!(end, WalkEnd::Complete);
190 // A visitor can halt.
191 let end = res!(Walk::new(100).run(&mut src, |_| Ok(false)));
192 assert_eq!(end, WalkEnd::Halted);
193 Ok(())
194 }));
195
196 res!(test_it(filter, &["Driven by hand, out of order is an error 000", "all", "walk"], || {
197 let mut walk = Walk::new(10);
198 assert_eq!(res!(walk.next()), Step::List(Vec::new()));
199 assert!(walk.next().is_err(), "the listing is owed");
200 assert!(walk.read(None).is_err(), "no file was asked for");
201 res!(walk.listed(vec![Listed::new(".gitignore", false), Listed::new("a", false)]));
202 assert_eq!(res!(walk.next()), Step::Read(b".gitignore".to_vec()));
203 res!(walk.read(Some(b"a\n".to_vec())));
204 match res!(walk.next()) {
205 Step::Entry(e) => assert_eq!(e.path, b".gitignore".to_vec()),
206 other => return Err(err!("Expected an entry, got {:?}.", other; Test)),
207 }
208 assert_eq!(res!(walk.next()), Step::Done);
209 assert_eq!(res!(walk.next()), Step::Done);
210 Ok(())
211 }));
212
213 res!(test_it(filter, &["Disk source walks a real tree 000", "all", "walk"], || {
214 let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("fe2o3_file_walk");
215 let _ = fs::remove_dir_all(&root);
216 for (rel, content) in [
217 (".gitignore", "*.o\n"),
218 ("src/main.rs", ""),
219 ("src/main.o", ""),
220 ("src/.oreignore", "secret\n"),
221 ("src/secret", ""),
222 (".ore/ops/1", ""),
223 ("node_modules/x/y.js", ""),
224 ] {
225 let path = root.join(rel);
226 if let Some(dir) = path.parent() {
227 res!(fs::create_dir_all(dir));
228 }
229 res!(fs::write(&path, content));
230 }
231 let mut found = Vec::new();
232 let end = res!(Walk::new(1000).run(&mut Disk::new(&root), |e| {
233 found.push((String::from_utf8_lossy(&e.path).into_owned(), e.is_dir));
234 Ok(true)
235 }));
236 assert_eq!(end, WalkEnd::Complete);
237 assert_eq!(found, vec![
238 (".gitignore".to_string(), false),
239 ("src".to_string(), true),
240 ("src/.oreignore".to_string(), false),
241 ("src/main.rs".to_string(), false),
242 ]);
243 assert!(Walk::new(10).run(&mut Disk::new(root.join("absent")), |_| Ok(true)).is_err(),
244 "a missing root is an error, not an empty walk");
245 let _ = fs::remove_dir_all(&root);
246 Ok(())
247 }));
248
249 res!(test_it(filter, &["Disk keeps the directories it could not list 000", "all", "walk"], || {
250 use std::os::unix::fs::PermissionsExt;
251 let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("fe2o3_file_walk_unread");
252 let _ = fs::remove_dir_all(&root);
253 for rel in ["open/a", "shut/b", "shut/deep/c"] {
254 res!(fs::create_dir_all(root.join(rel).parent().unwrap_or(&root)));
255 res!(fs::write(root.join(rel), ""));
256 }
257 res!(fs::set_permissions(root.join("shut"), fs::Permissions::from_mode(0o000)));
258 let mut disk = Disk::new(&root);
259 let mut found = Vec::new();
260 let end = Walk::new(1000).run(&mut disk, |e| { found.push(String::from_utf8_lossy(&e.path).into_owned()); Ok(true) });
261 res!(fs::set_permissions(root.join("shut"), fs::Permissions::from_mode(0o700)));
262 assert_eq!(res!(end), WalkEnd::Complete);
263 assert_eq!(found, vec!["open", "shut", "open/a"]);
264 assert_eq!(disk.unread(), &[b"shut".to_vec()], "a directory that could not be listed is not an empty one");
265 let _ = fs::remove_dir_all(&root);
266 Ok(())
267 }));
268
269 res!(test_it(filter, &["A tree with no .git reads only the ignore files it lists 000", "all", "walk"], || {
270 let mut src = Mem::new(&[
271 (".gitignore", "*.log\n"),
272 ("a.log", ""),
273 ("sub/.oreignore", "x\n"),
274 ("sub/x", ""),
275 ("sub/y", ""),
276 ]);
277 let (found, _) = res!(walk_all(Walk::new(1000), &mut src));
278 assert_eq!(found, vec![".gitignore", "sub/.oreignore", "sub/y"]);
279 assert_eq!(src.reads, vec![".gitignore", "sub/.oreignore"], "no new reads for an existing caller");
280 Ok(())
281 }));
282
283 res!(test_it(filter, &["Beneath reads every directory above the start, lists none 000", "all", "walk"], || {
284 let tree: &[(&str, &str)] = &[
285 (".git/HEAD", ""),
286 (".git/info/exclude", "*.bak\n"),
287 (".gitignore", "/drafts/\n"),
288 ("docs/.gitignore", "*.pdf\n"),
289 ("docs/book/.gitignore", "!keep.pdf\n"),
290 ("docs/book/a.md", ""),
291 ("docs/book/a.pdf", ""),
292 ("docs/book/keep.pdf", ""),
293 ("docs/book/x.bak", ""),
294 ("docs/other/x.md", ""),
295 ("drafts/sub/x.md", ""),
296 ];
297 let mut src = Mem::new(tree);
298 let walk = Walk::new(100).beneath(b"/docs/book/");
299 let (found, end) = res!(walk_all(walk, &mut src));
300 assert_eq!(found, vec!["docs/book/.gitignore", "docs/book/a.md", "docs/book/keep.pdf"],
301 "paths stay relative to the root; the root's info/exclude and docs' *.pdf are heard");
302 assert_eq!(end, WalkEnd::Complete);
303 assert_eq!(src.lists, 1, "only the start is listed");
304 assert_eq!(src.reads, vec![
305 ".git/info/exclude", ".gitignore", ".oreignore",
306 "docs/.git/info/exclude", "docs/.gitignore", "docs/.oreignore",
307 "docs/book/.gitignore",
308 ]);
309 // An ignored start, or one beneath an ignored directory, yields nothing.
310 for start in ["drafts", "drafts/sub"] {
311 let mut src = Mem::new(tree);
312 let mut walk = Walk::new(100).beneath(start);
313 loop {
314 match res!(walk.next()) {
315 Step::Read(p) => res!(walk.read(res!(src.read(&p)))),
316 Step::Done => break,
317 other => return Err(err!("Expected only reads, got {:?}.", other; Test)),
318 }
319 }
320 assert!(walk.start_ignored(), "{} is ignored", start);
321 assert_eq!(src.lists, 0);
322 assert!(!src.reads.iter().any(|r| r.starts_with("drafts")), "nothing read inside drafts");
323 }
324 let mut walk = Walk::new(100).beneath("docs");
325 assert!(!walk.start_ignored());
326 assert_eq!(res!(walk.next()), Step::Read(b".git/info/exclude".to_vec()));
327 // An empty start is the root.
328 let mut walk = Walk::new(100).beneath("/");
329 assert_eq!(res!(walk.next()), Step::List(Vec::new()));
330 Ok(())
331 }));
332
333 res!(test_it(filter, &["Info/exclude and global excludes rank where git puts them 000", "all", "walk"], || {
334 // Expected from git ls-files -co --exclude-standard, with core.excludesFile = "*.tmp".
335 let tree: &[(&str, &str)] = &[
336 (".git/HEAD", ""),
337 (".git/info/exclude", "local.txt\n*.bak\n!keep.bak\n!keep.tmp\n"),
338 (".gitignore", "*.log\n/build/\nsecret/\n"),
339 ("a.log", ""),
340 ("build/out", ""),
341 ("global.tmp", ""),
342 ("keep.bak", ""),
343 ("keep.tmp", ""),
344 ("local.txt", ""),
345 ("nestqa/.gitignore", "secret.txt\n"),
346 ("nestqa/ok.txt", ""),
347 ("nestqa/secret.txt", ""),
348 ("other/b.log", ""),
349 ("readme.md", ""),
350 ("secret/key", ""),
351 ("sub/.gitignore", "!local.txt\n!*.log\n"),
352 ("sub/a.log", ""),
353 ("sub/deep/local.txt", ""),
354 ("sub/global.tmp", ""),
355 ("sub/local.txt", ""),
356 ("sub/x.bak", ""),
357 ("x.bak", ""),
358 ];
359 let git = [
360 ".gitignore",
361 "keep.bak",
362 "keep.tmp",
363 "nestqa/.gitignore",
364 "nestqa/ok.txt",
365 "readme.md",
366 "sub/.gitignore",
367 "sub/a.log",
368 "sub/deep/local.txt",
369 "sub/local.txt",
370 ];
371 let mut src = Mem::new(tree);
372 let mut walk = Walk::new(1000).with_excludes(b"*.tmp\n");
373 let mut found = Vec::new();
374 loop {
375 match res!(walk.next()) {
376 Step::List(d) => res!(walk.listed(res!(src.list(&d)))),
377 Step::Read(p) => res!(walk.read(res!(src.read(&p)))),
378 Step::Entry(e) => if !e.is_dir { found.push(String::from_utf8_lossy(&e.path).into_owned()) },
379 other => { assert_eq!(other, Step::Done); break; }
380 }
381 }
382 found.sort();
383 assert_eq!(found, git.to_vec(), "the walk agrees with git");
384 assert_eq!(src.reads[0], ".git/info/exclude", "read before the root's .gitignore");
385 // The rules agree with the walk on every path, directories included.
386 let rules: Rules = walk.rules();
387 for (path, _) in tree {
388 assert_eq!(rules.ignored(path.as_bytes(), false), !git.contains(path), "{}", path);
389 }
390 for (dir, ign) in [("build", true), ("secret", true), (".git", true), ("sub", false), ("sub/deep", false)] {
391 assert_eq!(rules.ignored(dir.as_bytes(), true), ign, "{}", dir);
392 }
393 let why = |p: &str| rules.why(p.as_bytes(), false).map(|w| String::from_utf8_lossy(&w).into_owned());
394 assert_eq!(why("a.log"), Some(".gitignore".to_string()));
395 assert_eq!(why("x.bak"), Some(".git/info/exclude".to_string()));
396 assert_eq!(why("sub/x.bak"), Some(".git/info/exclude".to_string()));
397 assert_eq!(why("nestqa/secret.txt"), Some("nestqa/.gitignore".to_string()));
398 assert_eq!(why("global.tmp"), Some(EXCLUDES_NAME.to_string()));
399 assert_eq!(why("secret/key"), Some(".gitignore".to_string()));
400 assert_eq!(why(".git/HEAD"), Some(String::new()), "the built-in defaults");
401 assert_eq!(why("sub/a.log"), None);
402 assert_eq!(rules.top_ignored(b"secret/a/b", false), Some(b"secret".to_vec()));
403 assert_eq!(rules.top_ignored(b"x.bak", false), Some(b"x.bak".to_vec()));
404 assert_eq!(rules.top_ignored(b"sub/a.log", false), None);
405 Ok(())
406 }));
407
408 res!(test_it(filter, &["Each layer outranks the one below 000", "all", "walk"], || {
409 // A nested `!` over the root; info/exclude over excludes; excludes over the defaults.
410 let tree: &[(&str, &str)] = &[
411 (".git/info/exclude", "!node_modules/\n!x.tmp\n"),
412 (".gitignore", "*.gen\n"),
413 ("a/.gitignore", "!*.gen\n"),
414 ("a/b.gen", ""),
415 ("c.gen", ""),
416 ("node_modules/m.js", ""),
417 ("target/t", ""),
418 ("x.tmp", ""),
419 ("y.tmp", ""),
420 ];
421 let mut src = Mem::new(tree);
422 let walk = Walk::new(1000).with_excludes("!target/\n*.tmp\n");
423 let (mut found, _) = res!(walk_all(walk, &mut src));
424 found.sort();
425 assert_eq!(found, vec![".gitignore", "a/.gitignore", "a/b.gen", "node_modules/m.js", "target/t", "x.tmp"]);
426 Ok(())
427 }));
428
429 res!(test_it(filter, &["A nested repository's info/exclude speaks for it alone 000", "all", "walk"], || {
430 let mut src = Mem::new(&[
431 ("c.dat", ""),
432 ("inner/.git/info/exclude", "*.dat\n"),
433 ("inner/.gitignore", "!keep.dat\n"),
434 ("inner/a.dat", ""),
435 ("inner/keep.dat", ""),
436 ("inner/b.txt", ""),
437 ]);
438 let walk = Walk::new(1000);
439 let (mut found, _) = res!(walk_all(walk, &mut src));
440 found.sort();
441 assert_eq!(found, vec!["c.dat", "inner/.gitignore", "inner/b.txt", "inner/keep.dat"]);
442 assert_eq!(src.reads, vec!["inner/.git/info/exclude", "inner/.gitignore"]);
443 Ok(())
444 }));
445
446 res!(test_it(filter, &["Rules absorb another start's files once 000", "all", "walk"], || {
447 let tree: &[(&str, &str)] = &[
448 (".gitignore", "*.log\n"),
449 ("a/.gitignore", "x\n"),
450 ("a/x", ""),
451 ("b/.gitignore", "y\n"),
452 ("b/y", ""),
453 ("b/z.log", ""),
454 ];
455 let mut walk_a = Walk::new(100).beneath("a");
456 let mut walk_b = Walk::new(100).beneath("b");
457 let mut src = Mem::new(tree);
458 for walk in [&mut walk_a, &mut walk_b] {
459 loop {
460 match res!(walk.next()) {
461 Step::List(d) => res!(walk.listed(res!(src.list(&d)))),
462 Step::Read(p) => res!(walk.read(res!(src.read(&p)))),
463 Step::Entry(_) => (),
464 _ => break,
465 }
466 }
467 }
468 let mut rules = walk_a.rules();
469 assert!(!rules.ignored(b"b/y", false), "a's walk never heard b/.gitignore");
470 rules.absorb(walk_b.rules());
471 rules.absorb(walk_b.rules());
472 assert!(rules.ignored(b"a/x", false));
473 assert!(rules.ignored(b"b/y", false));
474 assert!(rules.ignored(b"b/z.log", false));
475 assert_eq!(rules.why(b"b/z.log", false), Some(b".gitignore".to_vec()));
476 assert!(!Rules::default().ignored(b"anything", false));
477 Ok(())
478 }));
479
480 res!(test_it(filter, &["Git config names the global excludes 000", "all", "walk"], || {
481 assert_eq!(git_config_excludes(b"[core]\n\texcludesFile = ~/.gitignore_global\n"),
482 Some(b"~/.gitignore_global".to_vec()));
483 assert_eq!(git_config_excludes(b"[Core]\nExcludesfile = \"/a b/ign\" # note\n"),
484 Some(b"/a b/ign".to_vec()));
485 assert_eq!(git_config_excludes(b"[core \"x\"]\nexcludesFile = /no\n[user]\nexcludesFile = /no\n"), None);
486 assert_eq!(git_config_excludes(b"[core]\nexcludesFile = /one\n[core]\n excludesfile=/two ; c\n"),
487 Some(b"/two".to_vec()));
488 assert_eq!(git_config_excludes(b"[core] excludesFile = /same\n"), Some(b"/same".to_vec()));
489 assert_eq!(git_config_excludes(b"; [core]\n# excludesFile = /no\n"), None);
490 // The environment decides which config is read.
491 let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("fe2o3_file_walk_gx");
492 let _ = fs::remove_dir_all(&root);
493 res!(fs::create_dir_all(root.join("xdg/git")));
494 res!(fs::write(root.join("ign"), "*.tmp\n"));
495 res!(fs::write(root.join("cfg"), format!("[core]\n\texcludesFile = {}\n", root.join("ign").display())));
496 res!(fs::write(root.join("xdg/git/ignore"), "*.xdg\n"));
497 let saved: Vec<(&str, Option<std::ffi::OsString>)> = ["GIT_CONFIG_GLOBAL", "XDG_CONFIG_HOME", "HOME"]
498 .iter().map(|k| (*k, std::env::var_os(k))).collect();
499 std::env::set_var("HOME", root.join("home"));
500 std::env::set_var("XDG_CONFIG_HOME", root.join("xdg"));
501 std::env::set_var("GIT_CONFIG_GLOBAL", root.join("cfg"));
502 let named = git_global_excludes();
503 std::env::remove_var("GIT_CONFIG_GLOBAL");
504 let default = git_global_excludes();
505 std::env::set_var("XDG_CONFIG_HOME", root.join("none"));
506 let absent = git_global_excludes();
507 for (k, v) in saved {
508 match v {
509 Some(v) => std::env::set_var(k, v),
510 None => std::env::remove_var(k),
511 }
512 }
513 assert_eq!(res!(named), Some(b"*.tmp\n".to_vec()), "core.excludesFile from GIT_CONFIG_GLOBAL");
514 assert_eq!(res!(default), Some(b"*.xdg\n".to_vec()), "XDG git/ignore when unset");
515 assert_eq!(res!(absent), None);
516 let _ = fs::remove_dir_all(&root);
517 Ok(())
518 }));
519
520 res!(test_it(filter, &["The nearest repository top holds the start 000", "all", "walk"], || {
521 let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("fe2o3_file_walk_top");
522 let _ = fs::remove_dir_all(&root);
523 res!(fs::create_dir_all(root.join("outer/.ore")));
524 res!(fs::create_dir_all(root.join("outer/inner/src/deep")));
525 res!(fs::write(root.join("outer/inner/.git"), "gitdir: elsewhere\n"));
526 assert_eq!(repo_top(&root.join("outer/inner/src/deep")), Some(root.join("outer/inner")),
527 "a .git file marks a worktree's top");
528 assert_eq!(repo_top(&root.join("outer/inner")), Some(root.join("outer/inner")));
529 assert_eq!(repo_top(&root.join("outer")), Some(root.join("outer")));
530 let _ = fs::remove_dir_all(&root);
531 Ok(())
532 }));
533
534 Ok(())
535}