Skip to content

Commit b70ff13

Browse files
committed
test: add tests for fullpath option in listdir_matching
This covers both recursive and non-recursive scenarios.
1 parent a9539a1 commit b70ff13

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

tests/test_pathtools.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
folder_size,
1111
gen_name_from_orig,
1212
image_basename,
13+
join2,
1314
join_files_with_channel_suffix,
1415
jython_fiji_exists,
1516
listdir_matching,
@@ -249,6 +250,65 @@ def test_join_files_with_channel_suffix():
249250
# nchannels as string
250251
assert join_files_with_channel_suffix(["a.tif"], "2") == ["a.tif", "a_0.tif"]
251252

253+
# nchannels as invalid string (fall back to [0])
254+
assert join_files_with_channel_suffix(["a.tif"], "foo") == ["a.tif", "a_0.tif"]
255+
256+
257+
def test_create_directory(tmpdir):
258+
"""Test create_directory function."""
259+
new_dir = tmpdir.join("new_dir")
260+
assert not os.path.exists(str(new_dir))
261+
create_directory(str(new_dir))
262+
assert os.path.exists(str(new_dir))
263+
# Test creating existing directory (should not fail)
264+
create_directory(str(new_dir))
265+
assert os.path.exists(str(new_dir))
266+
267+
268+
def test_join2():
269+
"""Test join2 function."""
270+
assert join2("/foo", "bar") == "/foo/bar"
271+
assert join2("/foo/", "bar") == "/foo/bar"
272+
assert join2("/foo", "/bar") == "/foo/bar"
273+
# test with double backslashes which should be sanitized
274+
assert join2("C:\\Temp", "file.txt") == "C:/Temp/file.txt"
275+
276+
277+
def test_listdir_matching_recursive_with_subfolders(tmpdir):
278+
"""Test recursive listdir_matching ensures paths are correctly combined."""
279+
base = tmpdir.mkdir("base_rec_sf")
280+
sub = base.mkdir("subfolder")
281+
sub.join("test.tif").write("x")
282+
283+
# non-recursive path join (uses path + candidate)
284+
res = listdir_matching(str(base), ".tif", fullpath=True, recursive=False)
285+
assert res == []
286+
287+
# recursive path join (uses dirpath + candidate)
288+
res_rec = listdir_matching(str(base), ".tif", fullpath=True, recursive=True)
289+
expected = os.path.abspath(os.path.join(str(sub), "test.tif"))
290+
assert expected in res_rec
291+
292+
# recursive path join with regex and fullpath
293+
res_rec_regex = listdir_matching(
294+
str(base), r".*\.tif$", fullpath=True, recursive=True, regex=True
295+
)
296+
assert expected in res_rec_regex
297+
298+
# recursive path join with regex and NOT fullpath
299+
res_rec_regex_rel = listdir_matching(
300+
str(base), r".*\.tif$", fullpath=False, recursive=True, regex=True
301+
)
302+
assert "subfolder/test.tif" in [p.replace(os.sep, "/") for p in res_rec_regex_rel]
303+
304+
# non-recursive path join with regex and fullpath
305+
sub_file = sub.join("test2.tif").write("x")
306+
res_nonrec_regex = listdir_matching(
307+
str(sub), r".*\.tif$", fullpath=True, recursive=False, regex=True
308+
)
309+
expected_nonrec = os.path.abspath(os.path.join(str(sub), "test2.tif"))
310+
assert expected_nonrec in res_nonrec_regex
311+
252312

253313
def test_create_directory(tmpdir):
254314
"""Test create_directory function."""

0 commit comments

Comments
 (0)