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 <bigcat88@icloud.com>

* refactored "add_model_folder_path" and added tests

---------

Signed-off-by: bigcat88 <bigcat88@icloud.com>
This commit is contained in:
Alexander Piskun 2024-12-14 02:19:22 +03:00 committed by GitHub
parent bdf393792d
commit caf2074773
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 6 deletions

View File

@ -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())

View File

@ -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"))