# Tests of finding what a glob matches in a tree of files: wildcards, `**`, # braces, hidden names, links, roots, directories, repeats, and directories # that can't be read. Every path is listed relative to the tree, with `/` # between names. import std::io; import std::process; import system; import std::io::glob; import std::testing; # A tree of files of a test's own, removed with all it holds where the test # returns: # # a.sakura b.txt .hidden.sakura 名前.sakura Upper.SAKURA src-old.txt # docs/ # src/main.sakura src/notes.txt src/util/text.sakura src/util/deep/x.sakura # src/.git/config.sakura # test/t.sakura # # and on Linux, `link`, a link to `src`, and `loop`, a link to itself. struct tree : fixture, owning { var at: path; } def tree::setup(t: &mut tester): tree { var made := tree { at = made_directory() }; for dir in ["docs", "src", "src/util", "src/util/deep", "src/.git", "test"] { val at := made.at.join(dir); _ = io::create_directory(&at); } for file in [ "a.sakura", "b.txt", ".hidden.sakura", "名前.sakura", "Upper.SAKURA", "src-old.txt", "src/main.sakura", "src/notes.txt", "src/util/text.sakura", "src/util/deep/x.sakura", "src/.git/config.sakura", "test/t.sakura" ] { val at := made.at.join(file); _ = io::write_file(&at, ""); } when os == linux { var link := process::command::new("ln"); link.directory(&made.at); link.arguments(["-s", "src", "link"]); _ = link.run(); var loop := process::command::new("ln"); loop.directory(&made.at); loop.arguments(["-s", "loop", "loop"]); _ = loop.run(); } return made; } def tree::close(&mut this): void { when os == windows { var removing := process::command::new("cmd"); removing.arguments(["/d", "/c", "rmdir", "/s", "/q", this.at.view()]); _ = removing.run(); return; } var remove := process::command::new("rm"); remove.arguments(["-rf", this.at.view()]); _ = remove.run(); } # Makes a directory no other run has, by making it only where nothing is. def made_directory(): path { # Windows keeps its temporary files where `TEMP` says. when os == windows { for n in 0..10000 { val named := path::parse($"{temporary()}\\sakura-glob-{n}") else (_) { break; }; var at := io::absolute(&named) else (_) { break; }; if io::create_directory(&at) is error(_) { continue; } return move(at); } } for n in 0..10000 { var at := path::parse($"/tmp/sakura-glob-{n}") else (_) { break; }; if io::create_directory(&at) is error(_) { continue; } return move(at); } system::abort(); return path::new(); } # A path's text with `/` between its names, as Windows writes `\`. def forward(text: strview): string { var made := string::from(text, nothing {}); for k in 0..made.length { if made[k] == '\\' { made[k] = '/'; } } return made; } # What a glob finds in a tree, read with the options, which by default tell # cases apart on every system: each path relative to the tree, with a space # between them, or why it stopped. def found(planted: &tree, text: strview, settings: glob::options = glob::options { folds_case = false }): string { val made := glob::pattern::parse(text, settings) else (e) { return e.describe(); }; var listed := string::new(nothing {}); var got := made.find(&planted.at); match got { value(&paths) => { for &each in paths { if listed.length != 0 { listed.push(' '); } if each.relative_to(&planted.at) is some(&relative) { listed.append(forward(relative.view()).view()); } else { listed.append(forward(each.view()).view()); } } } error(&why) => listed.append(why.describe().view()); } return listed; } @test def wildcards_find_the_names_of_a_directory(t: &mut tester, planted: tree): void { t.expects(found(&planted, "*.sakura") == "a.sakura 名前.sakura"); t.expects(found(&planted, "*.sakura", glob::options { folds_case = true }) == "Upper.SAKURA a.sakura 名前.sakura"); t.expects(found(&planted, "?.*") == "a.sakura b.txt"); t.expects(found(&planted, "[ab].*") == "a.sakura b.txt"); t.expects(found(&planted, ".*") == ".hidden.sakura"); } @test def two_stars_find_every_directory_below(t: &mut tester, planted: tree): void { t.expects(found(&planted, "**/*.sakura") == "a.sakura src/main.sakura src/util/deep/x.sakura src/util/text.sakura test/t.sakura 名前.sakura"); t.expects(found(&planted, "src/**") == "src src/main.sakura src/notes.txt src/util src/util/deep src/util/deep/x.sakura src/util/text.sakura"); # In order by their bytes, as a shell orders them. t.expects(found(&planted, "**/*.txt") == "b.txt src-old.txt src/notes.txt"); t.expects(found(&planted, "**/deep") == "src/util/deep"); t.expects(found(&planted, "**/util/**/*.sakura") == "src/util/deep/x.sakura src/util/text.sakura"); val hidden := glob::options { hidden = true, folds_case = false }; t.expects(found(&planted, "**/*.sakura", hidden) == ".hidden.sakura a.sakura src/.git/config.sakura src/main.sakura src/util/deep/x.sakura src/util/text.sakura test/t.sakura 名前.sakura"); } @test def braces_find_each_once(t: &mut tester, planted: tree): void { t.expects(found(&planted, "{src,test}/*.sakura") == "src/main.sakura test/t.sakura"); t.expects(found(&planted, "{src/**/x.sakura,**/deep/*.sakura}") == "src/util/deep/x.sakura"); t.expects(found(&planted, "{a,b}.*") == "a.sakura b.txt"); } @test def names_as_written_are_joined_on(t: &mut tester, planted: tree): void { t.expects(found(&planted, "a.sakura") == "a.sakura"); t.expects(found(&planted, "src/util/text.sakura") == "src/util/text.sakura"); t.expects(found(&planted, "nothing.sakura") == "" && found(&planted, "missing/*.sakura") == ""); t.expects(found(&planted, "a.sakura/*") == "" && found(&planted, "b.txt/**") == "b.txt"); t.expects(found(&planted, ".") == "." && found(&planted, "src/../test/*") == "src/../test/t.sakura"); } @test def a_slash_last_finds_directories(t: &mut tester, planted: tree): void { when os == linux { t.expects(found(&planted, "*/") == "docs link src test"); t.expects(found(&planted, "**/") == "docs link src src/util src/util/deep test"); } else { t.expects(found(&planted, "*/") == "docs src test"); t.expects(found(&planted, "**/") == "docs src src/util src/util/deep test"); } t.expects(found(&planted, "src/*/") == "src/util" && found(&planted, "a.sakura/") == ""); } @test def links_are_followed_where_named_but_not_by_two_stars(t: &mut tester, planted: tree): void { when os != linux { return; } t.expects(found(&planted, "*/main.sakura") == "link/main.sakura src/main.sakura"); t.expects(found(&planted, "link/**/*.sakura") == "link/main.sakura link/util/deep/x.sakura link/util/text.sakura"); t.expects(found(&planted, "l*") == "link loop"); t.expects(found(&planted, "**/main.sakura") == "src/main.sakura"); # A link leading around in a circle is passed over. t.expects(found(&planted, "loop/*") == "" && found(&planted, "*/x") == ""); } @test def a_glob_from_a_root_finds_from_there(t: &mut tester, planted: tree): void { var rooted := glob::escape(planted.at.view()); rooted.append("/src/*.sakura"); t.expects(found(&planted, rooted.view()) == "src/main.sakura"); val made := glob::pattern::parse(rooted.view()) else (_) { t.expects(false); return; }; val elsewhere := path::parse("elsewhere") else (_) { t.expects(false); return; }; var got := made.find(&elsewhere); if got is value(&paths) { t.expects(paths.length == 1 && paths[0].is_absolute); } else { t.expects(false); } } @test def a_directory_that_cannot_be_read_stops_the_search(t: &mut tester, planted: tree): void { when os != linux { return; } val locked := planted.at.join("locked"); _ = io::create_directory(&locked); val inside := locked.join("inside.sakura"); _ = io::write_file(&inside, ""); _ = io::set_permissions(&locked, 0); # Where it can be read anyway, as by the superuser, there's nothing to test. if io::directory::open(&locked) is error(_) { t.expects(found(&planted, "locked/*") == $"`{locked.view()}` could not be read: permission_denied"); t.expects(found(&planted, "**/*.sakura").starts_with("`")); t.expects(found(&planted, "*.txt") == "b.txt src-old.txt"); } _ = io::set_permissions(&locked, 0o755); } # Every path of the tree, but through links, relative to it, as `**` finds them # with hidden names. def everything(planted: &tree): vector { var all := vector::::new(); val made := glob::pattern::parse("**", glob::options { hidden = true, folds_case = false }) else (_) { return all; }; var got := made.find(&planted.at); if got is value(&paths) { for &each in paths { if each.relative_to(&planted.at) is some(&relative) { all.append(forward(relative.view())); } } } return all; } @test def what_is_found_is_what_matches(t: &mut tester, planted: tree): void { val all := everything(&planted); t.expects(all.length > 15); for text in ["**/*.sakura", "src/**", "**/util/*", "*.txt", "{src,test}/**/*.sakura", "**/[a-m]*", "?.sakura", "**/.*/*"] { val made := glob::pattern::parse(text, glob::options { folds_case = false }) else (_) { t.expects(false); continue; }; var matched := string::new(nothing {}); for &each in all { continue if !made.matches(each.view()); if matched.length != 0 { matched.push(' '); } matched.append(each.view()); } t.expects(found(&planted, text).view() == matched.view(), text); } }