From caf20747736dc8d9ad39ccc1a84539ee1fc58304 Mon Sep 17 00:00:00 2001 From: Alexander Piskun <13381981+bigcat88@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:19:22 +0300 Subject: [PATCH] add_model_folder_path: ensure unique paths by removing duplicates (#5998) * add_model_folder_path: ensure unique paths by removing duplicates Signed-off-by: bigcat88 * refactored "add_model_folder_path" and added tests --------- Signed-off-by: bigcat88 --- folder_paths.py | 13 ++++++-- tests-unit/comfy_test/folder_path_test.py | 38 +++++++++++++++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/folder_paths.py b/folder_paths.py index 577a7bc6..61de5120 100644 --- a/folder_paths.py +++ b/folder_paths.py @@ -200,10 +200,17 @@ def add_model_folder_path(folder_name: str, full_folder_path: str, is_default: b global folder_names_and_paths folder_name = map_legacy(folder_name) if folder_name in folder_names_and_paths: - if is_default: - folder_names_and_paths[folder_name][0].insert(0, full_folder_path) + paths, _exts = folder_names_and_paths[folder_name] + if full_folder_path in paths: + if is_default and paths[0] != full_folder_path: + # If the path to the folder is not the first in the list, move it to the beginning. + paths.remove(full_folder_path) + paths.insert(0, full_folder_path) else: - folder_names_and_paths[folder_name][0].append(full_folder_path) + if is_default: + paths.insert(0, full_folder_path) + else: + paths.append(full_folder_path) else: folder_names_and_paths[folder_name] = ([full_folder_path], set()) diff --git a/tests-unit/comfy_test/folder_path_test.py b/tests-unit/comfy_test/folder_path_test.py index 0bbec593..55613505 100644 --- a/tests-unit/comfy_test/folder_path_test.py +++ b/tests-unit/comfy_test/folder_path_test.py @@ -7,6 +7,14 @@ from unittest.mock import patch import folder_paths +@pytest.fixture() +def clear_folder_paths(): + # Clear the global dictionary before each test to ensure isolation + original = folder_paths.folder_names_and_paths.copy() + folder_paths.folder_names_and_paths.clear() + yield + folder_paths.folder_names_and_paths = original + @pytest.fixture def temp_dir(): with tempfile.TemporaryDirectory() as tmpdirname: @@ -30,9 +38,33 @@ def test_get_annotated_filepath(): assert folder_paths.get_annotated_filepath("test.txt", default_dir) == os.path.join(default_dir, "test.txt") assert folder_paths.get_annotated_filepath("test.txt [output]") == os.path.join(folder_paths.get_output_directory(), "test.txt") -def test_add_model_folder_path(): - folder_paths.add_model_folder_path("test_folder", "/test/path") - assert "/test/path" in folder_paths.get_folder_paths("test_folder") +def test_add_model_folder_path_append(clear_folder_paths): + folder_paths.add_model_folder_path("test_folder", "/default/path", is_default=True) + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=False) + assert folder_paths.get_folder_paths("test_folder") == ["/default/path", "/test/path"] + + +def test_add_model_folder_path_insert(clear_folder_paths): + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=False) + folder_paths.add_model_folder_path("test_folder", "/default/path", is_default=True) + assert folder_paths.get_folder_paths("test_folder") == ["/default/path", "/test/path"] + + +def test_add_model_folder_path_re_add_existing_default(clear_folder_paths): + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=False) + folder_paths.add_model_folder_path("test_folder", "/old_default/path", is_default=True) + assert folder_paths.get_folder_paths("test_folder") == ["/old_default/path", "/test/path"] + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=True) + assert folder_paths.get_folder_paths("test_folder") == ["/test/path", "/old_default/path"] + + +def test_add_model_folder_path_re_add_existing_non_default(clear_folder_paths): + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=False) + folder_paths.add_model_folder_path("test_folder", "/default/path", is_default=True) + assert folder_paths.get_folder_paths("test_folder") == ["/default/path", "/test/path"] + folder_paths.add_model_folder_path("test_folder", "/test/path", is_default=False) + assert folder_paths.get_folder_paths("test_folder") == ["/default/path", "/test/path"] + def test_recursive_search(temp_dir): os.makedirs(os.path.join(temp_dir, "subdir"))