|
1 | 1 | """Tests for `imcflibs.pathtools`.""" |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -from imcflibs.pathtools import parse_path |
5 | | -from imcflibs.pathtools import jython_fiji_exists |
6 | | -from imcflibs.pathtools import image_basename |
7 | | -from imcflibs.pathtools import gen_name_from_orig |
8 | | -from imcflibs.pathtools import derive_out_dir |
| 4 | +import os |
| 5 | + |
| 6 | +from imcflibs.pathtools import ( |
| 7 | + derive_out_dir, |
| 8 | + gen_name_from_orig, |
| 9 | + image_basename, |
| 10 | + jython_fiji_exists, |
| 11 | + listdir_matching, |
| 12 | + parse_path, |
| 13 | +) |
9 | 14 |
|
10 | 15 |
|
11 | 16 | def test_parse_path(): |
@@ -114,3 +119,41 @@ def test_derive_out_dir(): |
114 | 119 | assert derive_out_dir("/foo", "none") == "/foo" |
115 | 120 | assert derive_out_dir("/foo", "NONE") == "/foo" |
116 | 121 | assert derive_out_dir("/foo", "/bar") == "/bar" |
| 122 | + |
| 123 | + |
| 124 | +def test_listdir_matching_various(tmpdir): |
| 125 | + """Test non-recursive, recursive, fullpath, regex and sorting behaviour.""" |
| 126 | + base = tmpdir.mkdir("base") |
| 127 | + |
| 128 | + # create mixed files |
| 129 | + base.join("a.TIF").write("x") |
| 130 | + base.join("b.tif").write("x") |
| 131 | + base.join("c.png").write("x") |
| 132 | + |
| 133 | + # non-recursive, suffix match (case-insensitive) |
| 134 | + res = listdir_matching(str(base), ".tif") |
| 135 | + assert set(res) == {"a.TIF", "b.tif"} |
| 136 | + |
| 137 | + # fullpath returns absolute paths |
| 138 | + res_full = listdir_matching(str(base), ".tif", fullpath=True) |
| 139 | + assert all(os.path.isabs(x) for x in res_full) |
| 140 | + |
| 141 | + # recursive with relative paths |
| 142 | + sub = base.mkdir("sub") |
| 143 | + sub.join("s.TIF").write("x") |
| 144 | + res_rec = listdir_matching(str(base), ".tif", recursive=True) |
| 145 | + # should include the file from subdir as a relative path |
| 146 | + assert "sub/" in "/".join(res_rec) or any(p.startswith("sub/") for p in res_rec) |
| 147 | + |
| 148 | + # regex matching |
| 149 | + res_regex = listdir_matching(str(base), r".*\.tif$", regex=True) |
| 150 | + assert set(res_regex) >= {"a.TIF", "b.tif"} |
| 151 | + |
| 152 | + # sorting: create names that sort differently lexicographically |
| 153 | + base.join("img2.tif").write("x") |
| 154 | + base.join("img10.tif").write("x") |
| 155 | + base.join("img1.tif").write("x") |
| 156 | + res_sorted = listdir_matching(str(base), ".tif", sort=True) |
| 157 | + # expected alphanumeric order |
| 158 | + assert res_sorted.index("img1.tif") < res_sorted.index("img2.tif") |
| 159 | + assert res_sorted.index("img2.tif") < res_sorted.index("img10.tif") |
0 commit comments