feat(settings): add LORA_MANAGER_PORTABLE env var for per-instance settings isolation (#1018)

This commit is contained in:
Will Miao
2026-07-12 07:43:21 +08:00
parent 4f016a8024
commit 71658ab37b
3 changed files with 65 additions and 1 deletions

View File

@@ -152,6 +152,9 @@ class SettingsManager:
self._check_environment_variables()
self._collect_configuration_warnings()
if os.environ.get("LORA_MANAGER_PORTABLE", "0") == "1":
self.settings.setdefault("use_portable_settings", True)
if self._needs_initial_save:
self._save_settings()
self._needs_initial_save = False

View File

@@ -12,6 +12,7 @@ from platformdirs import user_config_dir
APP_NAME = "ComfyUI-LoRA-Manager"
_LM_PORTABLE_ENV = "LORA_MANAGER_PORTABLE"
_LOGGER = logging.getLogger(__name__)
@@ -100,7 +101,11 @@ def ensure_settings_file(logger: Optional[logging.Logger] = None) -> str:
def _should_use_portable_settings(path: str, logger: logging.Logger) -> bool:
"""Return ``True`` when the repository settings file enables portable mode."""
"""Return ``True`` when the env var forces it or the settings file enables it."""
if os.environ.get(_LM_PORTABLE_ENV, "0") == "1":
logger.debug("Portable mode enabled via %s", _LM_PORTABLE_ENV)
return True
if not os.path.exists(path):
return False

View File

@@ -0,0 +1,56 @@
"""Tests for settings path resolution."""
import json
import logging
import os
import pytest
from py.utils.settings_paths import _should_use_portable_settings
class TestShouldUsePortableSettings:
"""Tests for _should_use_portable_settings()."""
@pytest.mark.parametrize(
"env_value, settings_flag, expected",
[
("1", False, True), # env = 1 overrides settings.json false
("1", True, True), # env = 1 matches settings.json true
("0", False, False), # env = 0 → rely on settings.json
("0", True, True), # env = 0 → rely on settings.json
("", False, False), # unset → rely on settings.json
("", True, True), # unset → rely on settings.json
],
)
def test_env_var_overrides_settings(self, tmp_path, env_value, settings_flag, expected):
"""The LORA_MANAGER_PORTABLE env var takes precedence over settings.json."""
settings_file = tmp_path / "settings.json"
settings_file.write_text(
json.dumps({"use_portable_settings": settings_flag})
)
with pytest.MonkeyPatch.context() as mp:
if env_value:
mp.setenv("LORA_MANAGER_PORTABLE", env_value)
else:
mp.delenv("LORA_MANAGER_PORTABLE", raising=False)
result = _should_use_portable_settings(str(settings_file), logging.getLogger())
assert result == expected
def test_missing_file_without_env(self, tmp_path):
"""Without env var, missing settings file returns False."""
missing = tmp_path / "nonexistent.json"
result = _should_use_portable_settings(str(missing), logging.getLogger())
assert result is False
def test_missing_file_with_env(self, tmp_path):
"""With env var, even a missing settings file returns True."""
missing = tmp_path / "nonexistent.json"
with pytest.MonkeyPatch.context() as mp:
mp.setenv("LORA_MANAGER_PORTABLE", "1")
result = _should_use_portable_settings(str(missing), logging.getLogger())
assert result is True