Skip to content

Commit e517aed

Browse files
committed
Enhance ModelPathStore path normalization tests
Update tests in tests/utils/test_settings_store.py to exercise ModelPathStore normalization more robustly: use tmp_path to create real directories and files, assert _norm_existing_dir and _norm_existing_path return existing absolute paths (and that they are dir/file respectively), and handle None via _norm_existing_* helpers. Also adjust suggest_start_dir test to make cwd invalid by monkeypatching Path.cwd so the fallback to home is exercised. Removed the previous unreliable expanduser assertion.
1 parent 31e09d8 commit e517aed

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

tests/utils/test_settings_store.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,29 @@ def model_validate_json(raw: str):
9898
# -----------------------------
9999
# ModelPathStore helpers
100100
# -----------------------------
101-
def test_model_path_store_norm_handles_none_and_invalid(monkeypatch):
101+
def test_model_path_store_norm_handles_none_and_invalid(tmp_path: Path):
102102
s = InMemoryQSettings()
103103
mps = store.ModelPathStore(settings=s)
104104

105-
assert mps._norm(None) is None # type: ignore[arg-type]
105+
# None should normalize to None
106+
assert mps._norm_existing_path(None) is None # type: ignore[arg-type]
107+
assert mps._norm_existing_dir(None) is None # type: ignore[arg-type]
106108

107-
# Force Path.expanduser() to raise by passing something weird? Hard to do reliably.
108-
# Instead just assert normal path expands/returns str.
109-
assert mps._norm("~/somewhere") is not None
109+
# Existing dir should normalize to an absolute path
110+
d = tmp_path / "models"
111+
d.mkdir()
112+
norm_dir = mps._norm_existing_dir(str(d))
113+
assert norm_dir is not None
114+
assert Path(norm_dir).exists()
115+
assert Path(norm_dir).is_dir()
116+
117+
# Existing file should normalize as existing path
118+
f = d / "net.pt"
119+
f.write_text("x")
120+
norm_file = mps._norm_existing_path(str(f))
121+
assert norm_file is not None
122+
assert Path(norm_file).exists()
123+
assert Path(norm_file).is_file()
110124

111125

112126
# -----------------------------
@@ -289,7 +303,12 @@ def test_model_path_store_suggest_start_dir_falls_back_to_home(tmp_path: Path, m
289303
fake_home = tmp_path / "home"
290304
fake_home.mkdir()
291305

306+
# Make cwd "invalid" so suggest_start_dir can't use it
307+
fake_cwd = tmp_path / "does_not_exist"
308+
assert not fake_cwd.exists()
309+
292310
monkeypatch.setattr(store.Path, "home", lambda: fake_home)
311+
monkeypatch.setattr(store.Path, "cwd", lambda: fake_cwd)
293312

294313
assert mps.suggest_start_dir(fallback_dir=None) == str(fake_home)
295314

0 commit comments

Comments
 (0)