|
4 | 4 | import os |
5 | 5 |
|
6 | 6 | from imcflibs.pathtools import ( |
| 7 | + create_directory, |
7 | 8 | derive_out_dir, |
| 9 | + find_dirs_containing_filetype, |
| 10 | + folder_size, |
8 | 11 | gen_name_from_orig, |
9 | 12 | image_basename, |
| 13 | + join_files_with_channel_suffix, |
10 | 14 | jython_fiji_exists, |
11 | 15 | listdir_matching, |
12 | 16 | parse_path, |
@@ -180,5 +184,73 @@ def test_listdir_matching_recursive_regex_fullpath(tmpdir): |
180 | 184 | str(base), r".*\.tif$", regex=True, recursive=True, fullpath=True |
181 | 185 | ) |
182 | 186 | 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")) |
184 | 188 | 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