fix(config): handle windows paths in invokeai.yaml migration for legacy_conf_dir

The logic incorrectly set the `legacy_conf_dir` on windows, where the slashes go the other direction. Handle this case and update tests to catch it.
This commit is contained in:
psychedelicious 2024-04-02 17:15:05 +11:00 committed by Kent Keirsey
parent f75de8a35c
commit e655399324
2 changed files with 34 additions and 5 deletions

View File

@ -373,13 +373,16 @@ def migrate_v3_config_dict(config_dict: dict[str, Any]) -> InvokeAIAppConfig:
if k == "conf_path": if k == "conf_path":
parsed_config_dict["legacy_models_yaml_path"] = v parsed_config_dict["legacy_models_yaml_path"] = v
if k == "legacy_conf_dir": if k == "legacy_conf_dir":
# The old default for this was "configs/stable-diffusion". If if the incoming config has that as the value, we won't set it. # The old default for this was "configs/stable-diffusion" ("configs\stable-diffusion" on Windows).
# Else if the path ends in "stable-diffusion", we assume the parent is the new correct path. if v == "configs/stable-diffusion" or v == "configs\\stable-diffusion":
# Else we do not attempt to migrate this setting # If if the incoming config has the default value, skip
if v != "configs/stable-diffusion": continue
parsed_config_dict["legacy_conf_dir"] = v
elif Path(v).name == "stable-diffusion": elif Path(v).name == "stable-diffusion":
# Else if the path ends in "stable-diffusion", we assume the parent is the new correct path.
parsed_config_dict["legacy_conf_dir"] = str(Path(v).parent) parsed_config_dict["legacy_conf_dir"] = str(Path(v).parent)
else:
# Else we do not attempt to migrate this setting
parsed_config_dict["legacy_conf_dir"] = v
elif k in InvokeAIAppConfig.model_fields: elif k in InvokeAIAppConfig.model_fields:
# skip unknown fields # skip unknown fields
parsed_config_dict[k] = v parsed_config_dict[k] = v

View File

@ -98,6 +98,32 @@ def test_migrate_v3_config_from_file(tmp_path: Path, patch_rootdir: None):
assert not hasattr(config, "esrgan") assert not hasattr(config, "esrgan")
@pytest.mark.parametrize(
"legacy_conf_dir,expected_value,expected_is_set",
[
# not set, expected value is the default value
("configs/stable-diffusion", Path("configs"), False),
# not set, expected value is the default value
("configs\\stable-diffusion", Path("configs"), False),
# set, best-effort resolution of the path
("partial_custom_path/stable-diffusion", Path("partial_custom_path"), True),
# set, exact path
("full/custom/path", Path("full/custom/path"), True),
],
)
def test_migrate_v3_legacy_conf_dir_defaults(
tmp_path: Path, patch_rootdir: None, legacy_conf_dir: str, expected_value: Path, expected_is_set: bool
):
"""Test reading configuration from a file."""
config_content = f"InvokeAI:\n Paths:\n legacy_conf_dir: {legacy_conf_dir}"
temp_config_file = tmp_path / "temp_invokeai.yaml"
temp_config_file.write_text(config_content)
config = load_and_migrate_config(temp_config_file)
assert config.legacy_conf_dir == expected_value
assert ("legacy_conf_dir" in config.model_fields_set) is expected_is_set
def test_migrate_v3_backup(tmp_path: Path, patch_rootdir: None): def test_migrate_v3_backup(tmp_path: Path, patch_rootdir: None):
"""Test the backup of the config file.""" """Test the backup of the config file."""
temp_config_file = tmp_path / "temp_invokeai.yaml" temp_config_file = tmp_path / "temp_invokeai.yaml"