forked from comfyanonymous/ComfyUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand user directory for basepath in extra_models_paths.yaml (comfya…
…nonymous#4857) * Expand user path. * Add test. * Add unit test for expanding base path. * Simplify unit test. * Remove comment. * Remove comment. * Checkpoints. * Refactor.
- Loading branch information
1 parent
9a7444e
commit 9fa8faa
Showing
4 changed files
with
97 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pytest | ||
import yaml | ||
import os | ||
from unittest.mock import Mock, patch, mock_open | ||
|
||
from utils.extra_config import load_extra_path_config | ||
import folder_paths | ||
|
||
@pytest.fixture | ||
def mock_yaml_content(): | ||
return { | ||
'test_config': { | ||
'base_path': '~/App/', | ||
'checkpoints': 'subfolder1', | ||
} | ||
} | ||
|
||
@pytest.fixture | ||
def mock_expanded_home(): | ||
return '/home/user' | ||
|
||
@pytest.fixture | ||
def mock_add_model_folder_path(): | ||
return Mock() | ||
|
||
@pytest.fixture | ||
def mock_expanduser(mock_expanded_home): | ||
def _expanduser(path): | ||
if path.startswith('~/'): | ||
return os.path.join(mock_expanded_home, path[2:]) | ||
return path | ||
return _expanduser | ||
|
||
@pytest.fixture | ||
def mock_yaml_safe_load(mock_yaml_content): | ||
return Mock(return_value=mock_yaml_content) | ||
|
||
@patch('builtins.open', new_callable=mock_open, read_data="dummy file content") | ||
def test_load_extra_model_paths_expands_userpath( | ||
mock_file, | ||
monkeypatch, | ||
mock_add_model_folder_path, | ||
mock_expanduser, | ||
mock_yaml_safe_load, | ||
mock_expanded_home | ||
): | ||
# Attach mocks used by load_extra_path_config | ||
monkeypatch.setattr(folder_paths, 'add_model_folder_path', mock_add_model_folder_path) | ||
monkeypatch.setattr(os.path, 'expanduser', mock_expanduser) | ||
monkeypatch.setattr(yaml, 'safe_load', mock_yaml_safe_load) | ||
|
||
dummy_yaml_file_name = 'dummy_path.yaml' | ||
load_extra_path_config(dummy_yaml_file_name) | ||
|
||
expected_calls = [ | ||
('checkpoints', os.path.join(mock_expanded_home, 'App', 'subfolder1')), | ||
] | ||
|
||
assert mock_add_model_folder_path.call_count == len(expected_calls) | ||
|
||
# Check if add_model_folder_path was called with the correct arguments | ||
for actual_call, expected_call in zip(mock_add_model_folder_path.call_args_list, expected_calls): | ||
assert actual_call.args == expected_call | ||
|
||
# Check if yaml.safe_load was called | ||
mock_yaml_safe_load.assert_called_once() | ||
|
||
# Check if open was called with the correct file path | ||
mock_file.assert_called_once_with(dummy_yaml_file_name, 'r') |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import yaml | ||
import folder_paths | ||
import logging | ||
|
||
def load_extra_path_config(yaml_path): | ||
with open(yaml_path, 'r') as stream: | ||
config = yaml.safe_load(stream) | ||
for c in config: | ||
conf = config[c] | ||
if conf is None: | ||
continue | ||
base_path = None | ||
if "base_path" in conf: | ||
base_path = conf.pop("base_path") | ||
base_path = os.path.expanduser(base_path) | ||
for x in conf: | ||
for y in conf[x].split("\n"): | ||
if len(y) == 0: | ||
continue | ||
full_path = y | ||
if base_path is not None: | ||
full_path = os.path.join(base_path, full_path) | ||
logging.info("Adding extra search path {} {}".format(x, full_path)) | ||
folder_paths.add_model_folder_path(x, full_path) |