Skip to content

Commit 0be95a9

Browse files
Laurent Guerardehrenfeu
authored andcommitted
test(pathtools): add tests for find_dirs_containing_filetype, folder_size, join_files_with_channel_suffix, and create_directory functions
1 parent 4f28517 commit 0be95a9

1 file changed

Lines changed: 73 additions & 1 deletion

File tree

tests/test_pathtools.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import os
55

66
from imcflibs.pathtools import (
7+
create_directory,
78
derive_out_dir,
9+
find_dirs_containing_filetype,
10+
folder_size,
811
gen_name_from_orig,
912
image_basename,
13+
join_files_with_channel_suffix,
1014
jython_fiji_exists,
1115
listdir_matching,
1216
parse_path,
@@ -180,5 +184,73 @@ def test_listdir_matching_recursive_regex_fullpath(tmpdir):
180184
str(base), r".*\.tif$", regex=True, recursive=True, fullpath=True
181185
)
182186
assert any(os.path.isabs(x) for x in res)
183-
expected = os.path.join(str(sub), "s.tif")
187+
expected = os.path.abspath(os.path.join(str(sub), "s.tif"))
184188
assert expected in res
189+
190+
191+
def test_find_dirs_containing_filetype(tmpdir):
192+
"""Test find_dirs_containing_filetype function."""
193+
base = tmpdir.mkdir("find_dirs")
194+
sub1 = base.mkdir("sub1")
195+
sub2 = base.mkdir("sub2")
196+
sub1.join("file1.tif").write("x")
197+
sub2.join("file2.png").write("x")
198+
sub2.join("file3.tif").write("x")
199+
200+
res = find_dirs_containing_filetype(str(base), ".tif")
201+
# find_dirs_containing_filetype appends a "/" to the dirname
202+
expected_sub1 = str(sub1) + "/"
203+
expected_sub2 = str(sub2) + "/"
204+
assert expected_sub1 in res
205+
assert expected_sub2 in res
206+
assert len(res) == 2
207+
208+
209+
def test_folder_size(tmpdir):
210+
"""Test folder_size function."""
211+
base = tmpdir.mkdir("folder_size")
212+
base.join("file1.txt").write("123") # 3 bytes
213+
sub = base.mkdir("sub")
214+
sub.join("file2.txt").write("12345") # 5 bytes
215+
# Total should be 8 bytes
216+
217+
assert folder_size(str(base)) == 8
218+
219+
220+
def test_join_files_with_channel_suffix():
221+
"""Test join_files_with_channel_suffix function."""
222+
files = ["file1.tif", "file2.tif"]
223+
224+
# nchannels = 1 (no suffixed copies added)
225+
assert join_files_with_channel_suffix(files, 1) == files
226+
227+
# nchannels = 3 (original then _0 and _1 copies)
228+
res = join_files_with_channel_suffix(files, 3)
229+
expected = [
230+
"file1.tif",
231+
"file2.tif",
232+
"file1_0.tif",
233+
"file2_0.tif",
234+
"file1_1.tif",
235+
"file2_1.tif",
236+
]
237+
assert res == expected
238+
239+
# Empty files list
240+
assert join_files_with_channel_suffix([], 3) == ""
241+
242+
# nchannels as string
243+
assert join_files_with_channel_suffix(["a.tif"], "2") == ["a.tif", "a_0.tif"]
244+
245+
246+
def test_create_directory(tmpdir):
247+
"""Test create_directory function."""
248+
new_dir = os.path.join(str(tmpdir), "new_dir")
249+
assert not os.path.exists(new_dir)
250+
create_directory(new_dir)
251+
assert os.path.exists(new_dir)
252+
assert os.path.isdir(new_dir)
253+
254+
# Calling again should not raise (exist_ok behavior)
255+
create_directory(new_dir)
256+
assert os.path.exists(new_dir)

0 commit comments

Comments
 (0)