Path to searched file, if such file was found. Otherwise return null Path.
Example of searching configuration file, when you are somewhere inside project.
1 import dshould; 2 Path root = createTempPath(); 3 scope(exit) root.remove(); 4 5 // Save current directory 6 auto cdir = std.file.getcwd; 7 scope(exit) std.file.chdir(cdir); 8 9 // Create directory structure 10 root.join("dir1", "dir2", "dir3").mkdir(true); 11 root.join("dir1", "my-conf.conf").writeFile("hello!"); 12 root.join("dir1", "dir4", "dir8").mkdir(true); 13 root.join("dir1", "dir4", "my-conf.conf").writeFile("Hi!"); 14 root.join("dir1", "dir5", "dir6", "dir7").mkdir(true); 15 16 // Change current working directory to dir7 17 root.join("dir1", "dir5", "dir6", "dir7").chdir; 18 19 // Find config file. It sould be dir1/my-conf.conf 20 auto p1 = Path.current.searchFileUp("my-conf.conf"); 21 p1.isNull.should.be(false); 22 version(OSX) { 23 p1.get.toString.should.equal( 24 root.join("dir1", "my-conf.conf").realPath.toString); 25 } else { 26 p1.get.toString.should.equal( 27 root.join("dir1", "my-conf.conf").toAbsolute.toString); 28 } 29 30 // Try to get config, related to "dir8" 31 auto p2 = root.join("dir1", "dir4", "dir8").searchFileUp( 32 "my-conf.conf"); 33 p2.isNull.should.be(false); 34 p2.get.should.equal( 35 root.join("dir1", "dir4", "my-conf.conf")); 36 37 // Test searching for some path (instead of simple file/string) 38 auto p3 = root.join("dir1", "dir2", "dir3").searchFileUp( 39 Path("dir4", "my-conf.conf")); 40 p3.isNull.should.be(false); 41 p3.get.should.equal( 42 root.join("dir1", "dir4", "my-conf.conf")); 43 44 // One more test 45 auto p4 = root.join("dir1", "dir2", "dir3").searchFileUp( 46 "my-conf.conf"); 47 p4.isNull.should.be(false); 48 p4.get.should.equal(root.join("dir1", "my-conf.conf")); 49 50 // Try to find up some unexisting file 51 auto p5 = root.join("dir1", "dir2", "dir3").searchFileUp( 52 "i-am-not-exist.conf"); 53 p5.isNull.should.be(true); 54 55 import core.exception: AssertError; 56 p5.get.should.throwA!AssertError;
Search file by name in current directory and parent directories. Usually, this could be used to find project config, when current directory is somewhere inside project.
If no file with specified name found, then return null path.