Skip to content

Commit e2d6fa1

Browse files
lguerardehrenfeu
authored andcommitted
test(listdir): add tests for listdir_matching function
* Implement tests for non-recursive and recursive matching. * Validate fullpath and regex matching functionality. * Ensure correct sorting behavior for file names.
1 parent 5f21448 commit e2d6fa1

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

tests/test_pathtools.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""Tests for `imcflibs.pathtools`."""
22
# -*- coding: utf-8 -*-
33

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+
)
914

1015

1116
def test_parse_path():
@@ -114,3 +119,41 @@ def test_derive_out_dir():
114119
assert derive_out_dir("/foo", "none") == "/foo"
115120
assert derive_out_dir("/foo", "NONE") == "/foo"
116121
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

Comments
 (0)